VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sort-array-applying-given-equation/

⇱ Sort an array after applying the given equation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sort an array after applying the given equation

Last Updated : 16 May, 2025

Given an integer array arr[] sorted in ascending order, along with three integers: A, B, and C. The task is to transform each element x in the array using the quadratic function A*(x^2) + B*x + C. After applying this transformation to every element, return the modified array in sorted order.

Examples:

Input: arr[] = [-4, -2, 0, 2, 4], A = 1, B = 3, C = 5
Output: [3, 5, 9, 15, 33]
Explanation: After applying f(x) = 1*x2+ 3*x + 5 to each x, we get [9, 3, 5, 15, 33]. After sorting this array, the array becomes [3, 5, 9, 15, 33].

Input: arr[] = [-3, -1, 2, 4], A = -1, B = 0, C = 0
Output: [-16, -9, -4, -1]
Explanation: After applying f(x) = -1*x2 to each x, we get [-9, -1, -4, -16 ]. After sorting this array, the array becomes [-16, -9, -4, -1].

Input: arr[] = [-1, 0, 1, 2, 3, 4], A = -1, B = 2, C = -1
Output: [-9, -4, -4, -1, -1, 0]

[Brute Force Approach] Apply the Equation and Sort - O(n*log(n)) Time and O(1) Space

The idea is to directly transform each element in the array, i.e. for each element x in arr[], apply the given equation A*x2 + B*x + C in-place and then simply sort the array in ascending order.


Output
3 5 9 15 33 

[Expected Approach] Using Two Pointers - O(n) Time and O(n) Space

The idea is to use the fact that input array is sorted and apply a Two-Pointer approach. After applying the quadratic transformation to each element, the smallest and largest values will always be at the ends of the array (Why? The equation given is parabolic. So the result of applying it to a sorted array will result in an array that will have a maximum/minimum with the sub-arrays to its left and right sorted)

We process from both ends using two pointers (left and right) moving towards each other. By comparing the transformed values of arr[left] and arr[right], we can fill a new array (newArr) starting either from the front or the back depending on whether the coefficient A is positive or negative. The observation here is:

  • if A >= 0, the largest values will be at the end
  • while if A < 0, the largest values will be at the beginning.

There are two distinct cases when working with a quadratic transformation of a sorted array, and the nature of the function depends on the coefficient A.

Case 1: A > 0 – Convex parabola (opens upwards) → Has a valley (minimum point)

  • The largest transformed values lie at the ends of the array.
  • We fill the result array from end to start using two pointers.
👁 a

Case 2: A < 0 – Concave parabola (opens downwards) → Has a peak (maximum point)

  • The smallest transformed values lie at the ends.
  • We fill the result array from start to end using two pointers.
👁 b

Step-by-Step Implementation:

  • Start by initializing two pointers: left at the beginning and right at the end of the array.
  • Create a new array newArr of the same size to store the transformed and sorted values.
  • Determine the index where you will start filling newArr based on whether A is positive or negative.
  • Loop through the array using the left and right pointers, comparing the transformed values of arr[left] and arr[right].
  • Depending on the sign of A, place the larger of the two values at the appropriate position in newArr.
  • After processing all elements, copy the sorted values from newArr back to the original arr.
  • Finally, return the modified arr containing the transformed and sorted values.

Illustration:


Output
3 5 9 15 33 
Comment