Java Program to Count 1's in a sorted binary array Last Updated : 27 Dec, 2021 Comments Improve Suggest changes Like Article Like Report 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! Comment More infoAdvertise with us Next Article Java Program to Count 1's in a sorted binary array kartik Follow Improve Article Tags : Java Searching Java Programs DSA Binary Search binary-string +2 More Practice Tags : Binary SearchJavaSearching Similar Reads Java Program for Counting sets of 1s and 0s in a binary matrix Given a n à m binary matrix, count the number of sets where a set can be formed one or more same values in a row or column. Examples: Input: 1 0 1 0 1 0 Output: 8 Explanation: There are six one-element sets (three 1s and three 0s). There are two two- element sets, the first one consists of the first 3 min read Java Program to Convert a Decimal Number to Binary Number using Arrays as Stacks Given an Integer number convert into Binary Number using arrays as a stack. Example: Input : 10 Output: 1010 Input : 16 Output: 10000 Approach: Divide the number by 2 and store the remainder of the number in the array.Divide the number by 2.Repeat the process until the number becomes zero.Print the 1 min read Java Program for Counting Sort Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Java // Java implementation of Counting So 2 min read Java Program to Convert a Decimal Number to Binary & Count the Number of 1s As per the number system, default computations are carried over decimal numbers whose base is standardized as 10. Machine computes all the execution at the physical layer in 0s and 1s. So arises a need for a number system with base 2 known as a binary number system. A binary number can be converted 4 min read Java Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0, 6 min read Java Program to Convert Binary String to Decimal Using Wrapper Class Given a binary string as input, we need to write a program to convert the given binary string into its equivalent decimal number. Examples: Input : 1111 Output : 15 Input : 1001101 Output : 77 Input : 1101 Output : 13 The idea is to extract each character of a given binary string using charAt() and 2 min read Java Program to Segregate 0s on Left Side & 1s on Right Side of the Array You are given an array of 0s and 1s in random order. Segregate 0s on the left side and 1s on the right side of the array. The basic goal is to traverse array elements and sort in segregating 0s and 1s. Illustration: Input array = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] Output array = [0, 0, 0, 0, 0, 1, 1, 1, 4 min read Sort and Search an Element in Java In Java sorting and searching an element in an array is very easy. Unlike C, where we have to make all the functions to work, Java has inbuilt functions to do the same work. To sort an array there is a sort function and to search an element in a sorted array there is a binarySearch() function. To le 3 min read Check if a number is binary or not in Java Given a number N, the task is to check first whether the given number is binary or not and its value should be greater than 1. print true if N is the binary representation else print false. Examples: Input: N = 1010 Output: true Explanation: Given number is greater than 1 and none of its digits is g 3 min read Binary Search in Java Binary search is a highly efficient searching algorithm used when the input is sorted. It works by repeatedly dividing the search range in half, reducing the number of comparisons needed compared to a linear search. Here, we are focusing on finding the middle element that acts as a reference frame t 6 min read Like