Ninja has been given a sorted array/list βARRβ of integers of size βNβ and 3 integer coefficients βXβ, βYβ, and βZβ. Ninja needs to sort the βARRβ by applying the quadratic equation βX(ARR[i] * ARR[i]) + Y(ARR[i]) + Zβ to each element of βARRβ.
For Example :
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].
As Ninja is weak with Maths, he asks you for help. Can you help Ninja sort the given βARRβ after applying the quadratic equation to each element?
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β.
Output Format :
For each test case, print the sorted βARRβ after applying the given equation.
Print the output of each test case in a separate line.
Note :
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
3
2 1 1 1
1 2
4 0 0 -1
1 2 3 4
4 0 0 0
2 4 8 9
3 7
-1 -1 -1 -1
0 0 0 0
For sample test case 1:
Given βXβ, βYβ and βZβ are 1, 1 and 1 respectively. For each element at index βiβ in the βARRβ following are the values after applying the equation:
For βiβ = 0, βARR[0]β = 1.After applying the equation, βARR[0]β becomes β1 * (1 * 1) + 1 * (1) + 1β which is equal to 3.
For βiβ = 1, βARR[1]β = 2. After applying the equation, βARR[1]β becomes β1 * (2 * 2) + 1 * (2) + 1β which is equal to 7.And after sorting, βARRβ becomes [3,7].
For sample test case 2:
Given βXβ, βYβ and βZβ are 0, 0 and -1 respectively. For each element at index βiβ in the βARRβ following are the values after applying the equation:
For βiβ = 0, βARR[0]β = 1. After applying the equation, βARR[0]β becomes β0 * (1 * 1) + 0 * (1) + (-1)β which is equal to -1.
For βiβ = 1, βARR[1]β = 2. After applying the equation, βARR[1]β becomes β0 * (2 * 2) + 0 * (2) + (-1)β which is equal to -1.
For βiβ = 2, βARR[2]β = 3. After applying the equation, βARR[2]β becomes β0 * (3 * 3) + 0 * (3) + (-1)β which is equal to -1.
For βiβ = 3, βARR[3]β = 4. After applying the equation, βARR[3]β becomes β0 * (4 * 4) + 0 * (4) + (-1)β which is equal to -1.And after sorting, βARRβ becomes [-1,-1,-1,-1].
For sample test case 3:
Given βXβ, βYβ and βZβ are 0, 0 and 0 respectively. So every element in the βARRβ becomes 0 after applying the given equation.And after sorting, βARRβ becomes [0,0,0,0].
2
3 -1 1 -1
1 3 5
3 1 2 1
2 3 6
-21 -7 -1
9 16 49
For sample test case 1:
Given βXβ, βYβ and βZβ are -1, 1 and -1 respectively. For each element at index βiβ in the βARRβ following are the values after applying the equation:
For βiβ = 0, βARR[0]β = 1. After applying the equation, βARR[0]β becomes β-1 * (1 * 1) + 1 * (1) + (-1)β which is equal to -1.
For βiβ = 1, βARR[1]β = 3. After applying the equation, βARR[1]β becomes β-1 * (3 * 3) + 1 * (3) + (-1)β which is equal to -7.
For βiβ = 2, βARR[2]β = 5. After applying equation, βARR[2]β becomes β-1 * (5 * 5) + 1 * (5) + (-1)β which is equal to -21.And after sorting, βARRβ becomes [-21,-7,-1].
For sample test case 2:
Given βXβ, βYβ and βZβ are 1, 2 and 1 respectively. For each element at index βiβ in the βARRβ following are the values after applying the equation.
For βiβ = 0, βARR[0]β = 2. After applying the equation, βARR[0]β becomes β1 * (2 * 2) + 2 * (2) + 1β which is equal to 9.
For βiβ = 1, βARR[1]β = 3. After applying the equation, βARR[1]β becomes β1 * (3 * 3) + 2 * (3) + 1β which is equal to 16.
For βiβ = 2, βARR[2]β = 6. After applying the equation βARR[2]β becomes β1 * (6 * 6) + 2 * (6) + 1β which is equal to 49.And after sorting, βARRβ becomes [9, 16, 49].
Think of the Brute-Force approach.
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:
O(N log N) where βNβ represents the number of elements in the array 'ARR'.
We are iterating the βARRβ only once and finally sorting the βARRβ so overall time complexity will be O(N log N).
O(1)
Because we are not using any extra space for finding our resultant answer.