# Binary Search

## Rank It

You have been given an array _A_ consisting of _N_ integers. All the elements in this array _A_ are unique. You have to answer some queries based on the elements of this array. Each query will consist of a single integer _x_. You need to print the rank based position of this element in this array considering that the array is _1_ indexed. The rank based position of an element in an array is its position in the array when the array has been sorted in ascending order.

Note: It is guaranteed that all the elements in this array are unique and for each _x_ belonging to a query, value _x_ shall exist in the array

**Input Format**

The first line consists of a single integer _N_ denoting the size of array _A_. The next line contains _N_ unique integers, denoting the content of array _A_. The next line contains a single integer _q_ denoting the number of queries. Each of the next _q_ lines contains a single integer _x_ denoting the element whose rank based position needs to be printed.

**Output Format**

You need to print _q_ integers denoting the answer to each query.

{% tabs %}
{% tab title="C++" %}
```cpp
#include <iostream>

#include <bits/stdc++.h>

using namespace std;
int binarySearch(int A[], int low, int high, int x) {
    int mid;
    while (low <= high) {
        mid = (low + high) / 2;
        if (A[mid] < x)
            low = mid + 1;
        else if (A[mid] > x)
            high = mid - 1;
        else if (A[mid] == x)
            break;
    }
    return mid;
}
int main() {
    int size;
    cin >> size;
    int A[size];
    for (int i = 0; i < size; i++) {
        cin >> A[i];
    }
    sort(A, A + size);
    int q;
    cin >> q;
    while (q--) {
        int x, rank;
        cin >> x;
        rank = binarySearch(A, 0, size - 1, x);
        cout << rank + 1 << endl;
    }
    return 0;
}
```
{% endtab %}
{% endtabs %}
