Java Program to Count 1's in a sorted binary array
Last Updated :
27 Dec, 2021
Given a binary array sorted in non-increasing order, count the number of 1's in it.
Examples:
Input: arr[] = {1, 1, 0, 0, 0, 0, 0}
Output: 2
Input: arr[] = {1, 1, 1, 1, 1, 1, 1}
Output: 7
Input: arr[] = {0, 0, 0, 0, 0, 0, 0}
Output: 0
A simple solution is to linearly traverse the array. The time complexity of the simple solution is O(n). We can use Binary Search to find count in O(Logn) time. The idea is to look for last occurrence of 1 using Binary Search. Once we find the index last occurrence, we return index + 1 as count.
The following is the implementation of above idea.
Java
// Java program to count 1's in a sorted array
class CountOnes
{
/* Returns counts of 1's in arr[low..high]. The
array is assumed to be sorted in non-increasing
order */
int countOnes(int arr[], int low, int high)
{
if (high >= low)
{
// get the middle index
int mid = low + (high - low) / 2;
// check if the element at middle index is last
// 1
if ((mid == high || arr[mid + 1] == 0)
&& (arr[mid] == 1))
return mid + 1;
// If element is not last 1, recur for right
// side
if (arr[mid] == 1)
return countOnes(arr, (mid + 1), high);
// else recur for left side
return countOnes(arr, low, (mid - 1));
}
return 0;
}
/* Driver code */
public static void main(String args[])
{
CountOnes ob = new CountOnes();
int arr[] = { 1, 1, 1, 1, 0, 0, 0 };
int n = arr.length;
System.out.println("Count of 1's in given array is "
+ ob.countOnes(arr, 0, n - 1));
}
}
/* This code is contributed by Rajat Mishra */
OutputCount of 1's in given array is 4
Time complexity of the above solution is O(Logn)
Space complexity o(log n) (function call stack)
The same approach with iterative solution would be
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
static int countOnes(int arr[], int n)
{
int ans;
int low = 0, high = n - 1;
while (low <= high) { // get the middle index
int mid = (low + high) / 2;
// else recur for left side
if (arr[mid] < 1)
high = mid - 1;
// If element is not last 1, recur for right side
else if (arr[mid] > 1)
low = mid + 1;
else
// check if the element at middle index is last 1
{
if (mid == n - 1 || arr[mid + 1] != 1)
return mid + 1;
else
low = mid + 1;
}
}
return 0;
}
// Driver code
public static void main (String[] args) {
int arr[] = { 1, 1, 1, 1, 0, 0, 0 };
int n = arr.length;
System.out.println("Count of 1's in given array is "+ countOnes(arr, n));
}
}
// This code is contributed by patel2127.
OutputCount of 1's in given array is 4
Time complexity of the above solution is O(Logn)
Space complexity is O(1)
Please refer complete article on
Count 1's in a sorted binary array for more details!
Similar Reads
Javascript Program to Count 1's in a sorted binary array Given a binary array sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = {1, 1, 0, 0, 0, 0, 0} Output: 2 Input: arr[] = {1, 1, 1, 1, 1, 1, 1} Output: 7 Input: arr[] = {0, 0, 0, 0, 0, 0, 0} Output: 0A simple solution is to traverse the array linearly. The time comp
3 min read
Count 1's in a sorted binary array Given a binary array arr[] of size n, which is sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = [1, 1, 0, 0, 0, 0, 0]Output: 2Explanation: Count of the 1's in the given array is 2.Input: arr[] = [1, 1, 1, 1, 1, 1, 1]Output: 7Input: arr[] = [0, 0, 0, 0, 0, 0, 0]
7 min read
Java Program to Count set bits in an integer Write an efficient program to count number of 1s in binary representation of an integer. Examples :Input : n = 6Output : 2Binary representation of 6 is 110 and has 2 set bitsInput : n = 13Output : 3Binary representation of 11 is 1101 and has 3 set bits1. Simple Method Loop through all bits in an int
2 min read
Java Program to Count number of binary strings without consecutive 1's Write a Java program for a given positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1s. Examples: Input: N = 2Output: 3// The 3 strings are 00, 01, 10 Input: N = 3Output: 5// The 5 strings are 000, 001, 010, 100, 101 Recommend
4 min read
Find the transition point in a binary array Given a sorted array, arr[], containing only 0s and 1s, find the transition point, i.e., the first index where 1 was observed, and before that, only 0 was observed. If arr does not have any 1, return -1. If the array does not have any 0, return 0.Examples : Input: 0 0 0 1 1Output: 3Explanation: Inde
7 min read