

Let ‘ARR[]’ = [1, 2, -1] and ‘X’ = 1, ’Y’ =2 and ‘Z’ = 1. Then, for each element at index ‘i’ in the ‘ARR’:
For ‘i’ = 0, ‘ARR[0]’ = 1 and after applying the equation as ‘1 * (1 * 1) + 2 * (1) + 1‘ ‘ARR[0]’ becomes 4.
For ‘i’ = 1, ‘ARR[1]’ = 2 and after applying the equation as ‘1 * (2 * 2) + 2 * (2) + 1‘ ‘ARR[1]’ becomes 9 .
For ‘i’ = 2, ‘ARR[2]’ = -1 and after applying the equation as ‘1 * (-1 * -1) + 2 * (-1) + 1‘ ‘ARR[2]’ becomes 0.
So, ‘ARR’ after modification [4, 9, 0]. The final ‘ARR’ after sorting is [0, 4, 9].
The first line of input contains an integer ‘T’ which denotes the number of test cases to be run. Then the test cases follow.
The first line of each test case contains four single space-separated integers ‘N’, ’X’, ’Y’, and ‘Z’ representing the number of elements in the array/list ‘ARR’ and three coefficients of a quadratic equation.
The next line of each test case contains ‘N’ single space-separated integers denoting the elements of ‘ARR’.
For each test case, print the sorted ‘ARR’ after applying the given equation.
Print the output of each test case 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’ <= 100
1 <= ‘N’ <= 5000
-10^5 <= ‘X’, ‘Y’ and ‘Z’ <= 10^5
-10^5 <= ‘ARR[i]’ <= 10^5
Where 'ARR[i]' denotes the 'ith' element of the array.
Time Limit: 1 sec
The idea behind this approach is to modify the ‘ARR’ by applying the equation on each element and then finally sort the ‘ARR’.
Here is the complete algorithm:
We can optimize our solution. If we observe carefully, because this is a quadratic equation, we will always get an array/list that follows the following pattern:
So we will iterate through the modified ‘ARR’ and find out the position of the maximum element in the ‘ARR’. Now all the elements before the maximum element will be sorted in increasing order and all the elements after that will be in decreasing order. So we will simply merge these two sub-arrays to get sorted ‘ARR’.
Here is the complete Algorithm: