Sort an array after applying the given equation

3.6

We have an integer array that is sorted in ascending order. We also have 3 integers A, B and C. We need to apply A*x*x + B*x + C for each element x in the array and sort the modified array.

Input : arr[] = {-1, 0, 1, 2, 3, 4} 
       A = -1, B = 2, C = -1
Output : {-9, -4, -4, -1, -1, 0}
Input array is {-1, 0, 1, 2, 3, 4}. After
applying the equation A*x*x + B*x + C on
every element x we get, {-4,-1, 0, -1, -4, -9}
After sorting, we get {-9, -4, -4, -1, -1, 0}

Asked in : Adobe

Method 1 (Simple) :

1- Apply the given equation on all the elements. O(n)
2- Sort the modified array. O(n Log n)

Time complexity of O(n log n)

Method 2(Efficient): Parabolic Property

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.

In the above example, maximum is 0 and the sub array to its left {-4, -1} is sorted in ascending order and the sub-array to its right {-1, -4, -9} is sorted in descending order.
All we need to do is merge these sorted arrays which is linear in time.

So the algorithm is:

  1. Apply equation on each element.
  2. Find maximum/minimum.
  3. Merge sub-arrays.

Note : The below code assumes that the modified array is first increasing then decreasing.

C++

// C program to sort an array after applying equation
// A*x*x + B*x + C
#include<bits/stdc++.h>
using namespace std;

// Function to sort an array after applying given
// equation.
void sortArray(int arr[], int n, int A, int B, int C)
{
   // Apply equation on all elements
    for (int i = 0; i < n; i++)
        arr[i] = A*arr[i]*arr[i] + B*arr[i] + C;

    // Find maximum element in resultant array
    int index, maximum = INT_MIN;
    for (int i = 0; i< n; i++)
    {
        if (maximum < arr[i])
        {
            index = i;
            maximum = arr[i];
        }
    }

    // Use maximum element as a break point
    // and merge both subarrays usin simple
    // merge function of merge sort
    int i = 0, j = n-1;
    int new_arr[n], k = 0;
    while (i < index && j > index)
    {
        if (arr[i] < arr[j])
            new_arr[k++] = arr[i++];
        else
            new_arr[k++] = arr[j--];
    }

    // Merge remaining elements
    while (i < index)
        new_arr[k++] = arr[i++];
    while (j > index)
        new_arr[k++] = arr[j--];

    new_arr[n-1] = maximum;

    // Modify original array
    for (int i = 0; i < n ; i++)
        arr[i] = new_arr[i];
}

// Driver code
int main()
{
    int arr[] = {-21 ,-15, 12, 13, 14 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int A = -6, B =-7, C = 2;

    sortArray(arr, n, A, B, C);

    cout << "Array after sorting is : n";
    for (int i=0; i<n; i++)
       cout << arr[i] << " ";

    return 0;
}

Java

// Java program to sort an array after applying equation
// A*x*x + B*x + C

class Main
{
    // Function to sort an array after applying given
    // equation.
    static void sortArray(int arr[], int n, int A, int B, int C)
    {
       // Apply equation on all elements
        for (int i = 0; i < n; i++)
            arr[i] = A*arr[i]*arr[i] + B*arr[i] + C;
     
        // Find maximum element in resultant array
        int index=-1;
        int maximum = -999999;
        for (int i = 0; i< n; i++)
        {
            if (maximum < arr[i])
            {
                index = i;
                maximum = arr[i];
            }
        }
     
        // Use maximum element as a break point
        // and merge both subarrays usin simple
        // merge function of merge sort
        int i = 0, j = n-1;
        int[] new_arr = new int[n];
        int k = 0;
        while (i < index && j > index)
        {
            if (arr[i] < arr[j])
                new_arr[k++] = arr[i++];
            else
                new_arr[k++] = arr[j--];
        }
     
        // Merge remaining elements
        while (i < index)
            new_arr[k++] = arr[i++];
        while (j > index)
            new_arr[k++] = arr[j--];
     
        new_arr[n-1] = maximum;
     
        // Modify original array
        for (int p = 0; p < n ; p++)
            arr[p] = new_arr[p];
    }
    
    // main function
    public static void main (String[] args) 
    {
        int arr[] = {-21 ,-15, 12, 13, 14 };
        int n = arr.length;
        int A = -6, B =-7, C = 2;
     
        sortArray(arr, n, A, B, C);
     
        System.out.println("Array after sorting is : ");
        for (int i=0; i<n; i++)
           System.out.print(arr[i]+" ");
    }
}

/* This code is contributed by Harsh Agarwal */


Output:

Array after sorting is : 
-2497 -1272 -1243 -1103 -946 

Time Complexity : O(n)
Auxiliary Space : O(n)

Reference:
http://stackoverflow.com/questions/4551599/sorting-result-array

This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3.6 Average Difficulty : 3.6/5.0
Based on 12 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.