problem no 25: finding the transition point in the sorted binary array.

 QUESTION:

Find Transition Point


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

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.