A Binary Search Problem

View as PDF

Submit solution

Points: 3
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type

You are given a sorted with N distinct positive integers. Harry has been looking for some integers that may or may not be in the array, and asks you Q questions about the index of these number.

Please write a program to help out Harry answer his Q questions.

Input Specifications

The first line will contain a single integer \(N\:(1\le N \le 10^5)\), representing the number of integers in the array.

The next line will contain N distinct spaced positive integers, with the i-th integer is \(m_i\:(1\le m_i \le 10^9)\).

The next line will contain a single integer \(Q\:(1\le Q \le 10^5)\), representing the Q questions.

The next Q lines will contain 1 integer each, representing an integer Harry is looking for, where the i-th integer is \(q_i\:(1\le q_i \le 10^9)\)

Output Specifications

For each of the Q questions, output the index of the number given. If the number is not within the array, then simply output -1.

Sample Input

8
2 3 5 7 11 13 17 19
5
5
9
1
17
25

Sample Output

2
-1
-1
6
-1

Explanation for Sample Output

The integers 5 and 3 are within the array, and so their indices are outputted. The integers 9, 1, and 25 are not within the array, and so -1 is outputted.


Comments

There are no comments at the moment.