We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8e079e0 commit 2fa579bCopy full SHA for 2fa579b
missing-number/ekgns33.java
@@ -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