forked from maiquynhtruong/algorithms-and-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetting-a-different-number.java
43 lines (39 loc) · 1006 Bytes
/
getting-a-different-number.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int[] arr = new int[];
System.out.println(getAnotherNumber1(arr));
System.out.println(getAnotherNumber2(arr));
}
public static int getAnotherNumber1(int[] arr) {
int n = arr.length;
if (n == Integer.MAX_VALUE+1) return null;
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < n; i++) {
set.offer(arr[i]);
}
for (int i = 0; i <= n; i++) {
if (!set.contains(i))
return i;
}
return null;
}
public static int getAnotherNumber2(int[] arr) {
int n = arr.length;
if (n == Integer.MAX_VALUE+1) return null;
int[] arr2 = new int[n+1];
for (int i = 0; i < n; i++) {
arr2[arr[i] % (n+1)] = arr[i];
}
for (int i = 0; i < n+1; i++) {
if (arr2[i] == 0)
return i;
}
}
}