-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPermMissingElem.java
52 lines (45 loc) · 1.31 KB
/
PermMissingElem.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
44
45
46
47
48
49
50
51
52
package lessons.timecomplexity;
import java.util.HashSet;
import java.util.Set;
/**
* Permutation Missing Element (PermMissingElem)
* https://app.codility.com/programmers/lessons/3-time_complexity/perm_missing_elem/
* Difficulty : Easy
* Related Topics : Time Complexity
*
* created by cenkc on 4/5/2020
*/
public class PermMissingElem {
public static void main(String[] args) {
int[] A = new int[]{2, 3, 1, 5};
PermMissingElem permMissingElem = new PermMissingElem();
System.out.println(permMissingElem.solution(A));
}
public int solution(int[] A) {
Set incomplete = new HashSet<>();
for (int i = 0; i < A.length; i++) {
incomplete.add(A[i]);
}
for (int i = 1; i <= A.length + 1; i++) {
if (!incomplete.contains(i)) {
return i;
}
}
throw new RuntimeException("exception");
}
/*
// score below 80%
public int solution(int[] A) {
final int n = A.length + 1; // item count of complete array
final int completeArraySum = (n * (n +1 ))/2;
return (completeArraySum - getSumOfArray(A));
}
private int getSumOfArray(int[] A) {
int r = 0;
for (int i = 0; i < A.length; i++) {
r += A[i];
}
return r;
}
*/
}