Concise Problem Specification
Given an integer \$n\$ and a function \$f : X \to X \$ where \$X = \{1,2,3,..,n\}\$ Determine whether the given function is a bijective function or not.
Definition:
According to Wikipedia:
In mathematics, a bijection, bijective function or one-to-one correspondence is a function between the elements of two sets, where each element of one set is paired with exactly one element of the other set, and each element of the other set is paired with exactly one element of the first set.
Constraints
\$1\le n \le 20 \$
Input format
There are lines in the input.
The first line contains a single positive integer \$n\$.
The second line contains space separated integers, the values of \$f(1)\$ , \$f(2)\$ ... , \$f(n)\$, respectively.
Output Format
On a single line, output "YES" if is bijective. Otherwise, output "NO".
Solution
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Set<Integer> codomain = new HashSet<Integer>();
for (int i = 0; i < n; i++) {
int y = in.nextInt();
codomain.add(y);
}
System.out.println(codomain.size() == n ? "YES" : "NO");
in.close();
}
}
Comments
I am still relatively new to Java and would appreciate any comments on the above regarding improvements to my code.