problem no 25: finding the transition point in the sorted binary array.
QUESTION:
Given a sorted array containing only 0s and 1s, find the transition point.
Example 1:
Input:
N = 5
arr[] = {0,0,0,1,1}
Output: 3
Explanation: index 3 is the transition
point where 1 begins.
Example 2:
Input:
N = 4
arr[] = {0,0,0,0}
Output: -1
Explanation: Since, there is no "1",
the answer is -1.
ANSWER:
int transitionPoint(int arr[], int n) {
int onefound=0;
for(int i=0;i<n;i++)
{
if(arr[i]==1)
{
onefound=1;
return i;
}
}
if(onefound==0)
{
return -1;
}
}
Comments
Post a Comment