Anyone recognizes this exercise, closest element

this is the exercise

Input: arr[] = { 2, 1, 5, 8, 3 }
Output: -1 2 2 5 2

Explanation:
[2], it is the only number in this prefix. Hence, answer is -1. 
[2, 1], the closest number to 1 is 2
[2, 1, 5], the closest number to 5 is 2
[2, 1, 5, 8], the closest number to 8 is 5 
[2, 1, 5, 8, 3], the closest number to 3 is 2

Comments (4)