Open In App

Find the transition point in a binary array

Improve
Improve
Like Article
Like
Save
Share
Report

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

Examples : 

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

Input: 0 0 0 0 1 1 1 1
Output: 4
Explanation: Index of first 1 is 4
Recommended Practice

Naive Approach: Traverse the array and print the index of the first 1.  

  1. Traverse the array from the start to the end of the array.
  2. If the current element is 1, print the index and terminate the program.

Below is the implementation of the above approach:

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)
{
    //perform a linear search and
    // return the index of 
    //first 1
    for(int i=0; i<n ;i++)
      if(arr[i]==1)
        return i;
  
    //if no element is 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
import java.util.*;
  
class GFG
{
  
// Function to find the transition point
static int findTransitionPoint(int arr[], int n)
{
    // perform a linear search and return the index of 
    // first 1
    for(int i = 0; i < n ; i++)
    if(arr[i] == 1)
        return i;
  
    // if no element is 1
    return -1;
}
  
// Driver code
public static void main (String[] args)
{
    int arr[] = {0, 0, 0, 0, 1, 1};
    int n = arr.length;
      
    int point = findTransitionPoint(arr, n);
      
    if (point >= 0)
        System.out.print("Transition point is " + point);
    else
        System.out.print("There is no transition point");
}
}
  
// This code is contributed by shivanisinghss2110


Python3




# Python3 implementation to find the transition point
  
# Function to find the transition point
def findTransitionPoint(arr, n):
      
    # perform a linear search and return the index of 
    # first 1
    for i in range(n):
        if(arr[i] == 1):
            return i
      
    # if no element is 1
    return -1
  
# Driver code
arr = [0, 0, 0, 0, 1, 1]
n = len(arr)
  
point = findTransitionPoint(arr, n)
  
if point >= 0:
    print("Transition point is", point)
else
    print("There is no transition point")
      
# This code is contributed by shubhamsingh10


C#




// C# implementation to find the transition point
using System;
  
class GFG 
{
  
// Function to find the transition point
static int findTransitionPoint(int []arr ,int n)
{
    // perform a linear search and return the index of 
    // first 1
    for(int i = 0; i < n ; i++)
    if(arr[i] == 1)
        return i;
  
    // if no element is 1
    return -1;
}
  
 // Driver method
    public static void Main() 
    {
        int []arr = {0, 0, 0, 0, 1, 1};
        int point = findTransitionPoint(arr, arr.Length);
       
        Console.Write(point >= 0 ? "Transition point is " +
                   point : "There is no transition point");
    }
}
   
  
// This code is contributed by shivanisinghss2110


Javascript




<script>
  
    // Javascript implementation to 
    // find the transition point
      
    // Function to find the transition point
    function findTransitionPoint(arr, n)
    {
        // perform a linear search and 
        // return the index of
        // first 1
        for(let i = 0; i < n ; i++)
            if(arr[i] == 1)
                return i;
  
        // if no element is 1
        return -1;
    }
      
    let arr = [0, 0, 0, 0, 1, 1];
    let point = findTransitionPoint(arr, arr.length);
  
    document.write(point >= 0 ? "Transition point is " +
                  point : "There is no transition point");
      
</script>


Output

Transition point is 4

Complexity Analysis: 

  • Time Complexity: O(n), Only one traversal is needed, so the time complexity is O(n)
  • Auxiliary Space: O(1), No extra space is required.

Efficient Approach: The idea is to use Binary Search, and find the smallest index of 1 in the array. As the array is sorted, binary search can be performed.

  1. Create two variables, l and r, initialize l = 0 and r = n-1 and a variable ans = -1 to store the answer.
  2. Iterate the steps below till l <= r, the lower-bound is less than the upper-bound.
  3. Check if the element at middle index mid = (l+r)/2, is one or not.
  4. If the element is one, then check for the least index of 1 element on the left side of the middle element, i.e. update r = mid – 1 and update ans = mid.
  5. If the element is zero, then check for the least index of 1 element on the right side of the middle element, i.e. update l = mid + 1.

Below is the implementation of the above approach:

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 bounds
    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 (mid == 0
                    || (mid > 0 && 
                       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 bounds
        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 (mid == 0
                    || (mid > 0 && 
                       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");
    }
}


Python3




# python implementation to find the
# transition point
  
# Function to find the transition
# point
def findTransitionPoint(arr, n):
    # Initialise lower and upper
    # bounds
    lb = 0
    ub = n - 1
  
    # Perform Binary search
    while (lb <= ub):
        # Find mid
        mid = (int)((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 (mid == 0 \
                or (mid > 0 and\
                arr[mid - 1] == 0)):
                return mid
  
            # Else update 
            # upper_bound
            ub = mid-1
      
    return -1
  
# Driver code
arr = [0, 0, 0, 0, 1, 1]
n = len(arr)
point = findTransitionPoint(arr, n);
if(point >= 0):
    print("Transition point is ", point)
else:
    print("There is no transition point")
  
# This code is contributed by Sam007


C#




// C# implementation to find the transition point
using System;
          
class GFG 
{
    // Method to find the transition point
    static int findTransitionPoint(int []arr, int n)
    {
        // Initialise lower and upper bounds
        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 (mid == 0
                    || (mid > 0 && 
                       arr[mid - 1] == 0))
                    return mid;
      
                // Else update upper_bound
                ub = mid-1;
            }
        }
        return -1;
    }
      
      
    // Driver method
    public static void Main() 
    {
        int []arr = {0, 0, 0, 0, 1, 1};
        int point = findTransitionPoint(arr, arr.Length);
      
        Console.Write(point >= 0 ? "Transition point is " +
                   point : "There is no transition point");
    }
}
  
// This code is contributed by Sam007


PHP




<?php
// PHP implementation to find
// the transition point
  
// Function to find the 
// transition point
function findTransitionPoint($arr, $n)
{
      
    // Initialise lower and
    // upper bounds
    $lb = 0; $ub = $n-1;
  
    // Perform Binary search
    while ($lb <= $ub)
    {
          
        // Find mid
        $mid = floor(($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 ($mid == 0 or 
                ($mid > 0 and 
                 $arr[$mid - 1] == 0))
                return $mid;
  
            // Else update upper_bound
            $ub = $mid - 1;
        }
    }
    return -1;
}
  
    // Driver Code
    $arr = array(0, 0, 0, 0, 1, 1);
    $n = sizeof($arr);
    $point = findTransitionPoint($arr, $n);
  
    if($point >= 0) 
        echo "Transition point is " , $point;
    else
        echo"There is no transition point";
    return 0;
  
// This code is contributed by nitin mittal.
?>


Javascript




<script>
    // Javascript implementation to find the transition point
      
    // Method to find the transition point
    function findTransitionPoint(arr, n)
    {
        // Initialise lower and upper bounds
        let lb = 0, ub = n-1;
       
        // Perform Binary search
        while (lb <= ub)
        {
            // Find mid
            let mid = parseInt((lb+ub)/2, 10);
       
            // 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 (mid == 0
                    || (mid > 0 &&
                       arr[mid - 1] == 0))
                    return mid;
       
                // Else update upper_bound
                ub = mid-1;
            }
        }
        return -1;
    }
      
    let arr = [0, 0, 0, 0, 1, 1];
    let point = findTransitionPoint(arr, arr.length);
  
    document.write(point >= 0 ? "Transition point is " +
                  point : "There is no transition point");
        
</script>


Output

Transition point is 4

Complexity Analysis: 

  • Time Complexity: O(log n). The time complexity for binary search is O(log n).
  • Auxiliary Space: O(1). No extra space is required.


Last Updated : 15 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads