Find the transition point in a binary array
Given a sorted array, arr[], containing only 0s and 1s, find the transition point, i.e., the first index where 1 was observed, and before that, only 0 was observed. If arr does not have any 1, return -1. If the array does not have any 0, return 0.
Examples :
Input: 0 0 0 1 1
Output: 3
Explanation: Index 3 is the transition point where first 1 was observed.Input: arr[] = [0, 0, 0, 0]
Output: -1
Explanation: Since, there is no "1", the answer is -1.Input: 0 0 0 0 1 1 1 1
Output: 4
Explanation: Index 4 is the transition point where first 1 was observed.Input: arr[] = [1, 1, 1]
Output: 0
Explanation: There are no 0s in the array, so the transition point is 0.
[Naive Approach] Using a for Loop - O(n) Time and O(1) Space
The idea for this approach is to use a loop to traverse the array and print the index of the first 1.
#include<bits/stdc++.h>
using namespace std;
// Function to find the transition point
int transitionPoint(vector<int> arr)
{
int n = arr.size();
//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;
}
int main()
{
vector<int> arr = {0, 0, 0, 0, 1, 1};
cout<<transitionPoint(arr);
return 0;
}
import java.util.*;
class GfG
{
// Function to find the transition point
static int transitionPoint(int arr[])
{
int n = arr.length;
// 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;
}
public static void main (String[] args)
{
int arr[] = {0, 0, 0, 0, 1, 1};
System.out.print(transitionPoint(arr));
}
}
def transitionPoint(arr):
n = len(arr)
# 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
arr = [0, 0, 0, 0, 1, 1]
print(transitionPoint(arr))
using System;
class GfG
{
// Function to find the transition point
static int transitionPoint(int []arr)
{
int n = arr.Length;
// 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;
}
public static void Main()
{
int []arr = {0, 0, 0, 0, 1, 1};
Console.Write(transitionPoint(arr));
}
}
function transitionPoint(arr)
{
let n = arr.length;
// 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];
console.log(transitionPoint(arr));
Output
4
[Expected Approach] - Using Binary Search - O(log n) Time and O(1) Space
The idea is to use Binary Search, and find the smallest index of 1 in the array. As the array is sorted.
- Use two variables (l, r) to point left end and right end of the array and 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 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.
#include<bits/stdc++.h>
using namespace std;
// Function to find the transition point
int findTransitionPoint(vector<int> arr)
{
int n = arr.size();
// 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;
}
int main()
{
vector<int> arr = {0, 0, 0, 0, 1, 1};
cout<<findTransitionPoint(arr);
return 0;
}
class GfG {
// Method to find the transition point
static int transitionPoint(int arr[])
{
int n = arr.length;
// 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;
}
public static void main(String args[])
{
int arr[] = { 0, 0, 0, 0, 1, 1 };
System.out.println(transitionPoint(arr));
}
}
def transitionPoint(arr):
n = len(arr)
# 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
elif(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
arr = [0, 0, 0, 0, 1, 1]
print(transitionPoint(arr));
using System;
class GfG
{
// Method to find the transition point
static int transitionPoint(int []arr)
{
int n = arr.Length;
// 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;
}
public static void Main()
{
int []arr = {0, 0, 0, 0, 1, 1};
Console.Write(transitionPoint(arr));
}
}
function transitionPoint(arr)
{
let n = arr.length
// 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];
console.log(transitionPoint(arr));
Output
4