Find the transition point in a binary array

1.3

Given a sorted array containing only numbers 0 and 1, the task is to find the transition point efficiently. Transition point is a point where “0” ends and “1” begins.

Examples:

Input : 0 0 0 1 1
Output : 3
Index of first 1 is 3

Input : 0 0 0 0 1 1 1 1
Output : 4
Index of first 1 is 4

A naive approach is to do a linear scan of an array till you get the first 1 i.e. transition point.
Time Complexity : O(n) as we are performing Linear Search.

An efficient method is to use Binary Search.

Implementation:
1- Initialize lower_bound = 0 and upper_bound = n-1 
   (n is the no. of elements).
2- Traverse until lower_bound is lesser than or equal
   to upper_bound.
    a) Find mid = (lower_bound +upper_bound)/2
    b) Check if(arr[mid] == 0 )
       Then update lower_bound = mid+1 and search
      in the right part.
    c) Else if (arr[mid] == 1)
        i) If (arr[mid-1] == 0)
          return mid;
        ii) Else update upper_bound = mid-1 and 
            search in the left part.
3- Return -1 if 1 doesn't exist.

C++

// C++ implementation to find the transition point
#include<iostream>
using namespace std;

// Function to find the transition point
int findTransitionPoint(int arr[], int n)
{
    // Initialise lower and upper bounnds
    int lb = 0, ub = n-1;

    // Perform Binary search
    while (lb <= ub)
    {
        // Find mid
        int mid = (lb+ub)/2;

        // update lower_bound if mid contains 0
        if (arr[mid] == 0)
            lb = mid+1;

        // If mid contains 1
        else if (arr[mid] == 1)
        {
            // Check if it is the left most 1
            // Return mid, if yes
            if (arr[mid-1]==0)
                return mid;

            // Else update upper_bound
            ub = mid-1;
        }
    }
    return -1;
}

// Driver code
int main()
{
    int arr[] = {0, 0, 0, 0, 1, 1};
    int n = sizeof(arr) / sizeof(arr[0]);

    int point = findTransitionPoint(arr, n);

    point >= 0 ? cout<<"Transition point is " << point
               : cout<<"There is no transition point";
    return 0;
}

Java

// Java implementation to find the transition point

class Test
{
	// Method to find the transition point
	static int findTransitionPoint(int arr[], int n)
	{
	    // Initialise lower and upper bounnds
	    int lb = 0, ub = n-1;
	 
	    // Perform Binary search
	    while (lb <= ub)
	    {
	        // Find mid
	        int mid = (lb+ub)/2;
	 
	        // update lower_bound if mid contains 0
	        if (arr[mid] == 0)
	            lb = mid+1;
	 
	        // If mid contains 1
	        else if (arr[mid] == 1)
	        {
	            // Check if it is the left most 1
	            // Return mid, if yes
	            if (arr[mid-1]==0)
	                return mid;
	 
	            // Else update upper_bound
	            ub = mid-1;
	        }
	    }
	    return -1;
	}
	 
	
	// Driver method
	public static void main(String args[]) 
	{
		int arr[] = {0, 0, 0, 0, 1, 1};
	 
	    int point = findTransitionPoint(arr, arr.length);
	 
	    System.out.println(point >= 0 ? "Transition point is " + point
	                                   : "There is no transition point");
	}
}


Output:

Transition point is 4

Time Complexity: O(log n)

Asked in: Amazon

This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1.3 Average Difficulty : 1.3/5.0
Based on 8 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.