BSEARCH1 - Binary search
You are given a sorted array of numbers, and followed by number of queries, for each query if the queried number is present in the array print its position, else print -1.
First line contains N Q, number of elements in the array and number of queries to follow,
Second line contains N numbers, elements of the array, each number will be -10^9<= ai <= 10^9, 0 < N <= 10^5, 0 < Q <= 5*10^5
For each element in the query, print the elements 0 based location of its first occurence, if present, otherwise print -1.
Input: 5 4
2 4 7 7 9
2 Output: 2
Make sure to use scanf and printf for C++ users. The questions asks for the first occurrence of the interger make sure to read the question properly.
Turns out vedudx is incorrect. This site DOES accept cin and cout, however you need flush your stream. For example, in this problem you could cout <<endl instead of cout << '\n' to flush stuff every time you make a newline. The best option is to use cout.flush() at the end of the program.
Make I/O fast, it cost me some WAs.
Pythonists submit in PyPy, it's possible to get AC here. The input is huge, 500000 integers, so ensure your I/O is up to the task as well -> https://www.spoj.com/ranks/INTEST/lang=PYTH%203.2.3
TLE for Python. Don't waste your time.
this website does'nt accepts cin & cout.
read the comments till end.
hint : lower_bound gives the iterator to the first element in the array that is equal or GREATER than the number being searched . ( so you could get a valid index in return even though the element would not be present in the array, so you need to check if the iterator points to the number actually being searched to validate the answer )
Go to hell this stupid TLE thing, just because I didn't write scanf and printf.