forked from NKaty/Algorithms-and-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-zeroes.js
30 lines (24 loc) · 947 Bytes
/
count-zeroes.js
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
// Divide and Conquer - countZeroes
// Given an array of 1s and 0s which has all 1s first followed by all 0s,
// write a function called countZeroes, which returns the number of zeroes in the array.
// Time Complexity - O(log n)
function countZeroes(arr) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const middle = Math.floor((left + right) / 2);
if (arr[middle] === 1) left = middle + 1;
else right = middle - 1;
}
return arr.length - left;
}
console.log(countZeroes([1, 1, 1, 1, 1, 0])); // 1
console.log(countZeroes([1, 1, 1, 1, 0, 0])); // 2
console.log(countZeroes([1, 1, 1, 0, 0, 0])); // 3
console.log(countZeroes([1, 1, 0, 0, 0, 0])); // 4
console.log(countZeroes([1, 0, 0, 0, 0, 0])); // 5
console.log(countZeroes([0, 0, 0])); // 3
console.log(countZeroes([1, 1, 1, 1])); // 0
console.log(countZeroes([1, 0])); // 1
console.log(countZeroes([0])); // 1
console.log(countZeroes([])); // 0