Wednesday, May 4, 2022

Question 29 : Count 1’s in sorted Binary Array.

Print number of 1’s in a given sorted Binary Array.
For example :

Input : int[] arr = {0,0,0,1,1,1,1}; output : 4 int[] arr = {0,0,1}; output : 1

Solution

The Naive solution to solve this problem would be looping in the array and keep a count of 1’s occurring in the array.

But as the array is sorted we can stop at the very first one encountered, because as the array is sorted we can be sure that the element after the first one would all be greater than or equal to ones, but as our array contains zeroes and ones only, therefore all elements after first one would be one. 

So our answer would be (array.length – currentPointer). This will handle all the test cases and works in linear O(n) time.

package Arrays; import java.util.Scanner; public class num1sInsortedbinaryArray { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int[] arr = new int[scn.nextInt()]; for (int i = 0; i < arr.length; i++) { arr[i] = scn.nextInt(); } System.out.println(solve(arr)); } public static int solve(int[] arr) { int currPointer = 0; while (currPointer < arr.length) { if (arr[currPointer] == 1) { // as we have found our first one, we will stop here and // result would be (arr.length-currPinter) break; } // we will keep on increasing currPoniter as long as we are //encountering zeroes currPointer++; } return (arr.length - currPointer); } }


We can still solve the problem more efficiently in logarithmic time i.e. with the worst time complexity of O(log n)

Efficient Approach :

We can use divide and conquer approach, and move recursively at every step dividing the array into two subarrays virtually by keeping our start and end pointers.

  1.  If the element at end pointer of a subarray is zero this means that all the elements in that array would be zeroes and we know the array is already sorted.
  2. What if the first element that is the value at start pointer of the array? this means that all the elements in that subarray are ones again keeping in mind that array is sorted.

Now let’s discuss the time complexity.
At every call we are basically dividing the number of elements to work upon into two halves.
Initially we had N elements, then N/2, then N/4 and so on until we have one element left to work with.

if we denote T(n) for the time taken to process n elements.
Mathematically, we can write the equations as:

T(n) = T(n/2) + T(n/2) T(n/2) = T(n/4) + T(n/4) . . . T(2) = T(1) + T(1)

Say we have x such equations and as we move up in equations, the number of elements are getting doubled, so eventually,
n = 2^x

taking log on both sides,
x = log(n) {to the base 2}
Hence the complexity of our algorithm comes out to be O(log(n)).


import java.util.Scanner; public class Num1sInSortedBinaryArray { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int[] arr = new int[scn.nextInt()]; for (int i = 0; i < arr.length; i++) { arr[i] = scn.nextInt(); } System.out.print("arr[]: {"); for (int i = 0; i < arr.length; i++) { System.out.print(" "+arr[i]); } System.out.println(" }"); System.out.println("Number of 1 in array :"+solveEfficient(0, arr.length-1, arr)); } public static int solveEfficient(int start, int end, int[] arr) { if (arr[start] == 1) { // start elem is one, hence all other elements will be one in // virtual subarr. return end - start + 1; } if (arr[end] == 0) { // end elem is zero this means, all previous elements of //subarr will be zeroes. return 0; } int mid = (start + end) / 2; int leftResult = solveEfficient(start, mid, arr); int rightResult = solveEfficient(mid + 1, end, arr); // divide the array into two virtual subHalves return leftResult + rightResult; } }

When you run above program, you will get below output
7 0 0 0 1 1 1 1 arr[]: { 0 0 0 1 1 1 1 } Number of 1 in array :4

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS