Skip to content

Commit 2fa579b

Browse files
committed
feat : missing-number
1 parent 8e079e0 commit 2fa579b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

missing-number/ekgns33.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
input : integer of array
3+
output : return the only missing number
4+
constraints:
5+
1) is there any duplicate?
6+
no.
7+
2) range of element
8+
[0, n]
9+
10+
solution 1)
11+
iterate through the array
12+
save to hash set
13+
iterate i from 0 to n
14+
check if exists
15+
16+
tc : O(n) sc : O(n)
17+
18+
solution 2)
19+
iterate throught the array
20+
add all the elements
21+
get sum of integer sequence 0 .. n with. n(n+1)/2
22+
get subtraction
23+
tc : O(n) sc : O(1)
24+
*/
25+
class Solution {
26+
public int missingNumber(int[] nums) {
27+
int sum = 0;
28+
int n = nums.length;
29+
for(int num : nums) {
30+
sum += num;
31+
}
32+
int totalSum = (n+1) * n / 2;
33+
return totalSum - sum;
34+
}
35+
}

0 commit comments

Comments
 (0)