pranay_teja
6/5/2018 - 12:05 PM

GFG Finding Number

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

// #Searching #Problem #GFG
//  https://practice.geeksforgeeks.org/problems/finding-number/0

int bin_inc(vector<int> a,int l,int r,int x){
    if(l<=r){
        int n=l+((r-l)/2);
        if(a[n]==x){
            return n;
        }if(a[n]>x){
            return bin_inc(a,l,n-1,x);
        }else{
            return bin_inc(a,n+1,r,x);
        }
    }
    return -1;
}
int bin_dec(vector<int> a,int l,int r,int x){
    if(l<=r){
        int n=l+((r-l)/2);
        if(a[n]==x){
            return n;
        }if(a[n]<x){
            return bin_dec(a,l,n-1,x);
        }else{
            return bin_dec(a,n+1,r,x);
        }
    }
    return -1;
}

int main(){
    int t;
    cin>>t;
    while(t--){
        int n,x;
        cin>>n>>x;
        int k=-1;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
            if(k==-1 && i>0 && a[i]<a[i-1]){
                k=i;
            }
        }
        int ans=bin_inc(a,0,k-1,x);

        if(ans!=-1){
            cout<<ans<<endl;
        }else{
            ans=bin_dec(a,k,n-1,x);
            if(ans!=-1){
                cout<<ans<<endl;
            }else{
                cout<<"OOPS! NOT FOUND"<<endl;
            }
        }
    }
    return 0;
}