Given an array arr[] of only 0's and 1's. The array is sorted in descending order. Find the count of all the 0's.
Examples:
Input: arr[] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]
Output: 3
Explanation: There are 3 0's in the given array.Input: arr[] = [0, 0, 0, 0, 0]
Output: 5
Explanation: There are 5 0's in the array.
Table of Content
Using Linear Traversal - O(n) Time and O(1) Space
The idea is to traverse the array from left to right and count every element equal to 0. Since each element is checked exactly once, the total count obtained is the number of 0s in the array.
Working of Approach:
- Traverse the array from the first element to the last.
- If the current element is 0, increment the count.
- Continue until all elements are processed.
- Return the final count of 0s.
#include <iostream>
#include <vector>
using namespace std;
int countZeroes(vector<int> &arr)
{
int cnt = 0;
// Traverse the array
for (int x : arr)
{
// Count every 0
if (x == 0)
cnt++;
}
return cnt;
}
int main()
{
vector<int> arr = {0, 0, 0, 0, 0};
cout << countZeroes(arr);
return 0;
}
import java.util.Arrays;
public class GFG {
static int countZeroes(int[] arr)
{
int cnt = 0;
// Traverse the array
for (int x : arr) {
// Count every 0
if (x == 0)
cnt++;
}
return cnt;
}
public static void main(String[] args)
{
int[] arr = { 0, 0, 0, 0, 0 };
System.out.println(countZeroes(arr));
}
}
def countZeroes(arr):
cnt = 0
# Traverse the array
for x in arr:
# Count every 0
if x == 0:
cnt += 1
return cnt
if __name__ == "__main__":
arr = [0, 0, 0, 0, 0]
print(countZeroes(arr))
using System;
class GFG {
static int countZeroes(int[] arr)
{
int cnt = 0;
// Traverse the array
foreach(int x in arr)
{
// Count every 0
if (x == 0)
cnt++;
}
return cnt;
}
static void Main()
{
int[] arr = { 0, 0, 0, 0, 0 };
Console.WriteLine(countZeroes(arr));
}
}
function countZeroes(arr)
{
let cnt = 0;
// Traverse the array
for (let x of arr) {
// Count every 0
if (x === 0)
cnt++;
}
return cnt;
}
// Driver Code
let arr = [ 0, 0, 0, 0, 0 ];
console.log(countZeroes(arr));
Output
5
Using Binary Search for First Occurrence of 0 - O(log n) Time and O(1) Space
The idea is to use binary search to find the first occurrence of 0. Since the array is sorted in descending order, all the 1s appear before all the 0s. Once the first 0 is found, the count of 0s is simply the number of elements from that index to the end of the array.
Working of Approach:
- Perform binary search on the sorted array.
- Whenever a 0 is found, store its index and continue searching on the left.
- If 1 is found, search on the right half.
- After the search, return n - firstZeroIndex as the answer.
Let us understand with an example:
Input: arr[] = [0, 0, 0, 0, 0]
- Initially, low = 0, high = 4, and firstZero = 5.
- The middle element is 0, so store its index in firstZero and continue searching in the left half.
- Again, the middle element is 0, so update firstZero and move further left.
- The search ends with firstZero = 0, which is the index of the first 0.
- The number of 0s is n - firstZero = 5 - 0 = 5, so the answer is 5.
#include <iostream>
#include <vector>
using namespace std;
int countZeroes(vector<int> &arr)
{
int n = arr.size();
// Initialize binary search range
int low = 0, high = n - 1;
// Stores index of first 0, defaults to n if no 0 exists
int firstZero = n;
while (low <= high)
{
int mid = low + (high - low) / 2;
// If current element is 0, it can be the first 0
if (arr[mid] == 0)
{
firstZero = mid;
high = mid - 1;
}
// First 0 must lie on the right side
else
{
low = mid + 1;
}
}
// Number of 0s = elements from first 0 to end
return n - firstZero;
}
int main()
{
vector<int> arr = {0, 0, 0, 0, 0};
cout << countZeroes(arr);
return 0;
}
import java.util.Arrays;
public class GFG {
static int countZeroes(int[] arr)
{
int n = arr.length;
// Initialize binary search range
int low = 0, high = n - 1;
// Stores index of first 0, defaults to n if no 0
// exists
int firstZero = n;
while (low <= high) {
int mid = low + (high - low) / 2;
// If current element is 0, it can be the first
// 0
if (arr[mid] == 0) {
firstZero = mid;
high = mid - 1;
}
// First 0 must lie on the right side
else {
low = mid + 1;
}
}
// Number of 0s = elements from first 0 to end
return n - firstZero;
}
public static void main(String[] args)
{
int[] arr = { 0, 0, 0, 0, 0 };
System.out.println(countZeroes(arr));
}
}
def countZeroes(arr):
n = len(arr)
# Initialize binary search range
low = 0
high = n - 1
# Stores index of first 0, defaults to n if no 0 exists
firstZero = n
while low <= high:
mid = low + (high - low) // 2
# If current element is 0, it can be the first 0
if arr[mid] == 0:
firstZero = mid
high = mid - 1
# First 0 must lie on the right side
else:
low = mid + 1
# Number of 0s = elements from first 0 to end
return n - firstZero
if __name__ == '__main__':
arr = [0, 0, 0, 0, 0]
print(countZeroes(arr))
using System;
class GFG {
static int countZeroes(int[] arr)
{
int n = arr.Length;
// Initialize binary search range
int low = 0, high = n - 1;
// Stores index of first 0, defaults to n if no 0
// exists
int firstZero = n;
while (low <= high) {
int mid = low + (high - low) / 2;
// If current element is 0, it can be the first
// 0
if (arr[mid] == 0) {
firstZero = mid;
high = mid - 1;
}
// First 0 must lie on the right side
else {
low = mid + 1;
}
}
// Number of 0s = elements from first 0 to end
return arr.Length - firstZero;
}
static void Main()
{
int[] arr = { 0, 0, 0, 0, 0 };
Console.WriteLine(countZeroes(arr));
}
}
function countZeroes(arr)
{
let n = arr.length;
// Initialize binary search range
let low = 0, high = n - 1;
// Stores index of first 0, defaults to n if no 0 exists
let firstZero = n;
while (low <= high) {
let mid = Math.floor(low + (high - low) / 2);
// If current element is 0, it can be the first 0
if (arr[mid] === 0) {
firstZero = mid;
high = mid - 1;
}
// First 0 must lie on the right side
else {
low = mid + 1;
}
}
// Number of 0s = elements from first 0 to end
return n - firstZero;
}
// Driver Code
let arr = [ 0, 0, 0, 0, 0 ];
console.log(countZeroes(arr));
Output
5