Anyone seen or know how to solve this problem with dynamic programming? Prefix array
Given input array
And list queries [ {position, index} ..]
Return result list of closest index for each query
Ex: [1,2,6,7,9]
Queries:[{6, 0}, {10, 2}, {3, 3}]
Return: [2, 4, 2]
Example {6,0}
we are looking for where 6 would go in the array from index 0
it is actually in the array, return 2
Example {10,2}
Looking for 10 from index 2, the closest to that number is index 4
Example {3,3}
Looking for 3 from position 3
Decide between 2 or 6
6 is closer to position 3 so return index 3
My approach in the interview was to do binary search for each query but interviewer mentioned there was a dynamic programming solution.
Edit:
To me this seems pretty similar to the following problem:https://leetcode.com/problems/plates-between-candles/