Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
692 B
6
Indexable
Never
#include<stdio.h>

long long int n,q;
long long int ary[10000000];

long long int binary_search(long long int find){
	long long int mid;
    long long int low = 0;
    long long int high = n-1;
    while (low <=high) {
        mid =low + (high - low) / 2;
        if(find==ary[mid]){
        	return mid;
        }
        else if (find<ary[mid]) {
            high=mid-1;
        }
        else {
            low=mid+1;
        }
    }
    return -2;
}

int main(){
	scanf("%lld",&n);
	for(int i=0;i<n;i++){
		scanf("%lld",&ary[i]);
	}
	scanf("%lld",&q);
	long long int tmp;
	for(int i=0;i<q;i++){
		scanf("%lld",&tmp);
		long long int ans=binary_search(tmp);
		printf("%lld\n",ans+1);
	}

}