forked from super30admin/Competitive-Coding-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem1.java
More file actions
36 lines (31 loc) · 772 Bytes
/
Problem1.java
File metadata and controls
36 lines (31 loc) · 772 Bytes
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
/*
Problem :
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
Example 1:
Input: [3,0,1]
Output: 2
Example 2:
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
time Complexity : O(N)
Space Complexity : O(N)
Is run on leetcode : YES
*/
class Problem1 {
public int missingNumber(int[] nums) {
HashSet<Integer> s= new HashSet();
int count = nums.length;
// add in hashset
for(int i=0;i<count;i++){
s.add(nums[i]);
}
// check if int contains in hashset
// if not return integer
for(int i=0;i<count+1;i++){
if (!s.contains((Integer)i)){
return i;
}
}
return -1;
}
}