Company: Nexcus
Difficulty: medium
Second Next Greater Element Given the following: An array A of size N An integer X For each element, A[i] , find the value of the second next greater element by X to the right. Notes: 1-based indexing is followed. Second next greater element by X for any element A[i] is defined as the value of the element A[j] ( i ) such that A[i] + X and there exists exactly one index k such that A[i] + X where i . Input Format The first line contains an integer N - the size of the array. The second line contains N space-separated integers representing the elements of the array A . The third line contains the integer X . Output Format For each element in the array, print the second next greater element by X to the right. If it does not exist, print -1 . Constraints 1 ≤ N ≤ 10 5 0 ≤ A[i] ≤ 10 9 0 ≤ X ≤ 10 9 Examples Input: 5 1 2 3 4 5 1 Output: 3 4 5 -1 -1 For the first element 1 , the second next greater element by 1 is 3 . For 2 , it is 4 , and so on. The last two elements do not have a second next g