POTD Solutions | 4 Nov’ 23 | Find Transition Point
Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Binary Search algorithm but will also help you build up problem-solving skills.
POTD 4 November: Find Transition Point:
Given a sorted array 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.
Examples:
Input: N = 5
arr[] = {0,0,0,1,1}
Output: 3
Explanation: Index 3 is the transition point where 1 beginsInput: N = 4
arr[] = {0,0,0,0}
Output: -1
Explanation: Since, there is no “1”,
the answer is -1.
Find Transition Point Using Binary Search:
The idea is to use Binary Search since the array is sorted. Iteratively check the middle element of the array, if the element encountered is 1 store its index and then repeat the same procedure for left part of the array else if the element encountered is 1 repeat the same procedure for right part of the array.
Follow the steps to solve the problem:
- Create two variables, l and r, initialize l = 0 and r = n-1 and a variable index= -1 to store the answer.
- Iterate the steps below till l <= r, the lower-bound is less than the upper-bound.
- Set mid is equal to (l+r)/2 and check the element at position mid,
- If the element is 1, set index to mid and h (upper bound) to mid-1.
- If the element is 0, l (lower bound) to mid+1.
- Return the index
Below is the implementation of above approach:
C++
class Solution { public : int transitionPoint( int arr[], int n) { // Initialize the left pointer to the beginning of the array. int l = 0; // Initialize the right pointer to the end of the array. int h = n - 1; // Initialize the index variable to -1, in case there are no '1's in the array. int index = -1; while (l <= h) { // Calculate the middle index. int mid = (l + h) / 2; if (arr[mid] == 1) { // Update the 'index' if '1' is found. index = mid; // Adjust 'h' to search in the left subarray for the first '1'. h = mid - 1; } else { // Adjust 'l' to search in the right subarray for '1'. l = mid + 1; } } // Return the index of the first '1' or -1 if no '1' is present. return index; } }; |
Java
class Solution { int transitionPoint( int arr[], int n) { // Initialize the left pointer to the beginning of the array. int l = 0 ; // Initialize the right pointer to the end of the array. int h = n - 1 ; // Initialize the index variable to -1, in case there are no '1's in the array. int index = - 1 ; while (l <= h) { // Calculate the middle index. int mid = (l + h) / 2 ; if (arr[mid] == 1 ) { // Update the 'index' if '1' is found. index = mid; // Adjust 'h' to search in the left subarray for the first '1'. h = mid - 1 ; } else { // Adjust 'l' to search in the right subarray for '1'. l = mid + 1 ; } } // Return the index of the first '1' or -1 if no '1' is present. return index; } } |
Python3
class Solution: def transitionPoint( self , arr, n): # Initialize the left pointer to the beginning of the array. l = 0 # Initialize the right pointer to the end of the array. h = n - 1 # Initialize the index variable to -1, in case there are no '1's in the array. index = - 1 while l < = h: # Calculate the middle index. mid = (l + h) / / 2 if arr[mid] = = 1 : # Update the 'index' if '1' is found. index = mid # Adjust 'h' to search in the left subarray for the first '1'. h = mid - 1 else : # Adjust 'l' to search in the right subarray for '1'. l = mid + 1 # Return the index of the first '1' or -1 if no '1' is present. return index |
Time Complexity: O(log n). The time complexity for binary search is O(log n).
Auxiliary Space: O(1). No extra space is required.