-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMissingNumber.java
50 lines (34 loc) · 933 Bytes
/
MissingNumber.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
package Dsa;
import java.util.HashMap;
import java.util.Map;
public class MissingNumber {
public static void main(String[] args) {
int[] inputArr = new int[] {9,6,4,2,3,5,7,0,1};
System.out.println(missingNumber(inputArr));
}
//Brute-Force Approach
private static int getMissingNumber(int[] inputArr) {
int range = inputArr.length;
Map<Integer,Integer> tmap = new HashMap<>();
for(int i=0; i <= range; i++) {
tmap.put(i, 0);
}
for(int i=0; i < range; i++) {
tmap.put(inputArr[i], 1);
}
return tmap.entrySet().stream()
.filter(entry -> entry.getValue() == 0)
.map(Map.Entry::getKey)
.findFirst().get();
}
//Optimal Solution
private static int missingNumber(int[] nums) {
int n = nums.length;
int sum = (n*(n+1))/2;
int arraySum=0;
for(int i=0; i< n; i++) {
arraySum+=nums[i];
}
return sum-arraySum;
}
}