
Give houses location, ‘HOUSES’ : {2, 4, 2}
Given shops location, ‘SHOPS’ : {5, 1, 2, 3}
The shops closest to each houses are : {2, 3, 2}
The first line of input contains an integer ‘T’, the number of test cases.
The first line of each test case contains a single integer ‘H’ representing the number of the houses.
The second line of each test case contains ‘H’ single space-separated integers representing the location of the houses.
The third line of each test case contains a single integer ‘S’ representing the number of the shops.
The fourth line of each test case contains ‘S’ single space-separated integers representing the location of the shops.
For each test case, print ‘H’ space separated integers denoting the closest shop to the ith house.
Output for each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= H <= 10^2
1 <= S <= 10^2
0 <= HOUSE[i], SHOP[i] <= 10^5
Where HOUSE[i], SHOP[i] represent the ith house and ith shop respectively.
Time Limit : 1 sec
The basic idea is to traverse the ‘SHOP’ array for each house to find the shop nearest to a house.
Here is the algorithm :
The idea is to sort the ‘SHOP’ array and then find the shop nearest to a house using binary search.
Here is the algorithm :