Find the transition point in a binary array
Last Updated :
15 Sep, 2023
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
Naive Approach: Traverse the array and print the index of the first 1.
- Traverse the array from the start to the end of the array.
- If the current element is 1, print the index and terminate the program.
Below is the implementation of the above approach:
C++
#include<iostream>
using namespace std;
int findTransitionPoint(int arr[], int n)
{
for(int i=0; i<n ;i++)
if(arr[i]==1)
return i;
return -1;
}
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
import java.util.*;
class GFG
{
static int findTransitionPoint(int arr[], int n)
{
for(int i = 0; i < n ; i++)
if(arr[i] == 1)
return i;
return -1;
}
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");
}
}
|
Python3
def findTransitionPoint(arr, n):
for i in range(n):
if(arr[i] == 1):
return i
return -1
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")
|
C#
using System;
class GFG
{
static int findTransitionPoint(int []arr ,int n)
{
for(int i = 0; i < n ; i++)
if(arr[i] == 1)
return i;
return -1;
}
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");
}
}
|
Javascript
<script>
function findTransitionPoint(arr, n)
{
for(let i = 0; i < n ; i++)
if(arr[i] == 1)
return i;
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.
- Create two variables, l and r, initialize l = 0 and r = n-1 and a variable ans = -1 to store the answer.
- Iterate the steps below till l <= r, the lower-bound is less than the upper-bound.
- Check if the element at middle index mid = (l+r)/2, is one or not.
- 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.
- 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++
#include<iostream>
using namespace std;
int findTransitionPoint(int arr[], int n)
{
int lb = 0, ub = n-1;
while (lb <= ub)
{
int mid = (lb+ub)/2;
if (arr[mid] == 0)
lb = mid+1;
else if (arr[mid] == 1)
{
if (mid == 0
|| (mid > 0 &&
arr[mid - 1] == 0))
return mid;
ub = mid-1;
}
}
return -1;
}
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
class Test {
static int findTransitionPoint(int arr[], int n)
{
int lb = 0, ub = n - 1;
while (lb <= ub) {
int mid = (lb + ub) / 2;
if (arr[mid] == 0)
lb = mid + 1;
else if (arr[mid] == 1) {
if (mid == 0
|| (mid > 0 &&
arr[mid - 1] == 0))
return mid;
ub = mid - 1;
}
}
return -1;
}
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
def findTransitionPoint(arr, n):
lb = 0
ub = n - 1
while (lb <= ub):
mid = (int)((lb + ub) / 2)
if (arr[mid] == 0):
lb = mid + 1
else if (arr[mid] == 1):
if (mid == 0 \
or (mid > 0 and\
arr[mid - 1] == 0)):
return mid
ub = mid-1
return -1
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")
|
C#
using System;
class GFG
{
static int findTransitionPoint(int []arr, int n)
{
int lb = 0, ub = n-1;
while (lb <= ub)
{
int mid = (lb+ub)/2;
if (arr[mid] == 0)
lb = mid+1;
else if (arr[mid] == 1)
{
if (mid == 0
|| (mid > 0 &&
arr[mid - 1] == 0))
return mid;
ub = mid-1;
}
}
return -1;
}
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");
}
}
|
PHP
<?php
function findTransitionPoint($arr, $n)
{
$lb = 0; $ub = $n-1;
while ($lb <= $ub)
{
$mid = floor(($lb + $ub) / 2);
if ($arr[$mid] == 0)
$lb = $mid + 1;
else if ($arr[$mid] == 1)
{
if ($mid == 0 or
($mid > 0 and
$arr[$mid - 1] == 0))
return $mid;
$ub = $mid - 1;
}
}
return -1;
}
$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;
?>
|
Javascript
<script>
function findTransitionPoint(arr, n)
{
let lb = 0, ub = n-1;
while (lb <= ub)
{
let mid = parseInt((lb+ub)/2, 10);
if (arr[mid] == 0)
lb = mid+1;
else if (arr[mid] == 1)
{
if (mid == 0
|| (mid > 0 &&
arr[mid - 1] == 0))
return mid;
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.