17. Sort the given array after applying the given equation
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given a sorted array arr[]
in ascending order and integers A
, B
, and C
, apply the quadratic transformation f(x) = Aยทxยฒ + Bยทx + C
to each element of the array. Then return the transformed array in sorted order.
๐ Examples
Example 1:
Input:
arr = [-4, -2, 0, 2, 4]
, A = 1
, B = 3
, C = 5
Output:
[3, 5, 9, 15, 33]
Explanation:
Transformed values: [9, 3, 5, 15, 33]
Sorted: [3, 5, 9, 15, 33]
Example 2:
Input: arr = [-3, -1, 2, 4]
, A = -1
, B = 0
, C = 0
Output:
[-16, -9, -4, -1]
Explanation:
Transformed values: [-9, -1, -4, -16]
Sorted: [-16, -9, -4, -1]
๐ Constraints
$1 \leq \text{arr.size()} \leq 10^6$
$-10^3 \leq \text{arr}[i], A, B, C \leq 10^3$
โ
My Approach
Two-Pointer Approach
We take advantage of the original array being sorted and the fact that a quadratic function opens upwards (when A > 0
) or downwards (when A < 0
). The key observation is that:
For
A >= 0
: Largest transformed values appear at both ends of the original array.For
A < 0
: Smallest transformed values appear at both ends.
We use two pointers from left and right, and fill the result from either start or end depending on the sign of A
.
Algorithm Steps:
Define the quadratic function
f(x) = A*x*x + B*x + C
.Use two pointers:
l
at start andr
at end of the array.Maintain an index
i
that fills the result:From end if
A >= 0
(placing larger values last).From start if
A < 0
(placing smaller values first).
Compare
f(arr[l])
andf(arr[r])
, and insert the appropriate value into result.
๐งฎ Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. We process each element once in a linear two-pointer sweep.
Expected Auxiliary Space Complexity: O(n), as we use a new result array to store the transformed elements.
๐ง Code (C++)
class Solution {
public:
vector<int> sortArray(vector<int> &arr, int A, int B, int C) {
int n = arr.size(), l = 0, r = n - 1, i = A >= 0 ? n - 1 : 0;
vector<int> res(n);
auto f = [&](int x) { return A * x * x + B * x + C; };
while (l <= r) {
int lv = f(arr[l]), rv = f(arr[r]);
if (A >= 0) res[i--] = lv > rv ? lv : rv, lv > rv ? ++l : --r;
else res[i++] = lv < rv ? lv : rv, lv < rv ? ++l : --r;
}
return res;
}
};
๐งโ๐ป Code (Java)
class Solution {
public ArrayList<Integer> sortArray(int[] arr, int A, int B, int C) {
int n = arr.length, l = 0, r = n - 1, i = A >= 0 ? n - 1 : 0;
ArrayList<Integer> res = new ArrayList<>(Collections.nCopies(n, 0));
while (l <= r) {
int lv = A * arr[l] * arr[l] + B * arr[l] + C;
int rv = A * arr[r] * arr[r] + B * arr[r] + C;
if (A >= 0) {
res.set(i--, lv > rv ? lv : rv);
if (lv > rv) l++; else r--;
} else {
res.set(i++, lv < rv ? lv : rv);
if (lv < rv) l++; else r--;
}
}
return res;
}
}
๐ Code (Python)
class Solution:
def sortArray(self, arr, A, B, C):
n, l, r, i = len(arr), 0, len(arr) - 1, len(arr) - 1 if A >= 0 else 0
f = lambda x: A * x * x + B * x + C
res = [0] * n
while l <= r:
lv, rv = f(arr[l]), f(arr[r])
if A >= 0:
res[i] = lv if lv > rv else rv
i -= 1
l += lv > rv
r -= lv <= rv
else:
res[i] = lv if lv < rv else rv
i += 1
l += lv < rv
r -= lv >= rv
return res
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Letโs make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated