Skip to content

Commit 690420b

Browse files
authored
Create MissingNumber.js
1 parent 3423829 commit 690420b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* [MissingNumber](https://www.geeksforgeeks.org/find-the-missing-number/) is an algorithm to find missing number in array.
3+
*
4+
* Notes:
5+
* The numbers will be in the range (1, N), an array of size N can be maintained to keep record of the elements present in the given array
6+
*
7+
* @complexity: O(log(n)) (on average )
8+
* @complexity: O(log(n)) (worst case)
9+
* @flow
10+
*/
11+
12+
// Function to find the missing number
13+
function findMissing(arr,N){
14+
let i;
15+
let const = [];
16+
for (i = 0; i <= N; i++) {
17+
const[i] = 0;
18+
}
19+
20+
for (i = 0; i < N; i++) {
21+
const[arr[i] - 1] = 1;
22+
}
23+
24+
let ans = 0;
25+
for (i = 0; i <= N; i++) {
26+
if (const[i] == 0)
27+
ans = i + 1;
28+
}
29+
console.log(ans);
30+
}
31+
32+
// Driver code
33+
let arr = [ 1, 3, 7, 5, 6, 2 ];
34+
let n = arr.length;
35+
36+
// Function call
37+
findMissing(arr,n);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy