FastPrepSort a Quadratic-Transformed Array

Sort a Quadratic-Transformed Array

LinkedIn logoLinkedIn● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given an integer array nums sorted in nondecreasing order and three integer coefficients a, b, and c. Apply f(x) = a * x * x + b * x + c to every element and return all resulting values in nondecreasing order.

  • Retain every occurrence: equal inputs or equal transformed values must not be removed.
  • For this exercise, an empty input returns an empty array, and repeated input values are allowed.
  • Your solution must run in O(n) time, where n = nums.length.

For this exercise, the numeric limits below ensure every transformed value fits in a signed 32-bit integer. The coefficients may be positive, negative, or zero.

Function

sortQuadraticTransform(nums: int[], a: int, b: int, c: int) → int[]

Examples

Example 1

nums = [-4,-2,2,4]a = 1b = 3c = 5return = [3,9,15,33]

The transformed values in input order are [9,3,15,33]. Returning them in sorted order gives [3,9,15,33].

Example 2

nums = [-4,-2,2,4]a = -1b = 3c = 5return = [-23,-5,1,7]

The transformed values are [-23,-5,7,1]. Sorting retains all four occurrences and gives [-23,-5,1,7].

Example 3

nums = []a = 0b = 0c = 8return = []

There are no elements to transform, so the result is empty.

Constraints

  • 0 <= nums.length <= 2000.
  • -1000 <= nums[i] <= 1000; nums is sorted in nondecreasing order.
  • -100 <= a, b, c <= 100.
  • All inputs are integers. Under these practice bounds, |f(x)| <= 100100100, so exact signed 32-bit arithmetic is sufficient.

More LinkedIn problems

See LinkedIn hiring insights
public int[] sortQuadraticTransform(int[] nums, int a, int b, int c) {
    // Write your code here.
}
nums[-4,-2,2,4]
a1
b3
c5
expected[3,9,15,33]
Checking account…