Embercode
✕
Divide and ConquerCoding problems

Divide and Conquer: coding problems

Write a program that reads the input and prints the answer. Run checks it against every test, including hidden ones.

0 of 8 solved

  • The class keeps a sorted list of unique test scores on the wall. A teacher calls out a score, and the program must say where it sits in the list, counting from 0. If the score is not on the list, the answer is -1.

    Because the list is sorted, you do not need to look at every item. Use binary search to answer each query in about log n steps.

    Constraints: 1 <= n <= 500, 1 <= q <= 100, every value is between -1000000000 and 1000000000, and no value repeats.

    Input. Line 1: `n`. Line 2: the `n` sorted values. Line 3: `q`. The next `q` lines each hold one query value.

    Output. `q` lines. For each query, print its 0-based index in the list, or `-1` if the value is not there.

    Example 1 input

    5
    -4 1 3 8 10
    4
    8
    2
    -4
    10
    

    Output

    3
    -1
    0
    4
    

    Example 2 input

    3
    5 6 7
    1
    6
    

    Output

    1