-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissingNumber.java
More file actions
31 lines (27 loc) · 917 Bytes
/
missingNumber.java
File metadata and controls
31 lines (27 loc) · 917 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
class Solution {
// Function to find the missing number
public int missingNumber(int[] nums) {
// Calculate N from the length of nums
int N = nums.length;
// Outer loop that runs from 0 to N
for (int i = 0; i <= N; i++) {
/* Flag variable to check
if an element exists*/
int flag = 0;
/* Search for the element
using linear search*/
for (int j = 0; j < N; j++) {
if (nums[j] == i) {
// i is present in the array
flag = 1;
break;
}
}
// Check if the element is missing (flag == 0)
if (flag == 0) return i;
}
/* The following line will never
execute, it is just to avoid warnings*/
return -1;
}
}