Maximize

Easy
0/40
Average time to solve is 25m
profile
Contributed by
1 upvote
Asked in company
Intuit

Problem statement

You have an integer ‘N’ and an array ‘X’ of ‘N’ integers. You need to maximize the value of the array, which is equal to ⅀ '(X[i] - i)^2' from ‘i’ in the range ‘[0, N-1]’. To do this, you can rearrange the elements of the given array.

Determine the maximum value of the array you can get after rearranging the array ‘X’ elements.

Example:
'N' = 3, ‘X' = [1,2,1] 
If we rearrange our array 'X' to '[2, 1, 1]' .
Then our answer will be (0-2)^2 + (1-1)^2 + (1-2)^2 = 4 + 0 + 1 = 5.
For array ‘[1, 1, 2]’ value will be equal to ‘1 + 0 + 0 = 1’.
For array ‘[1, 2, 1]’ value will be equal to ‘1 + 1 + 1 = 3’.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line 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 an integer 'N’.
The second line contains N integers, denoting the array ‘X’ value.
Output format:
For each test case, return the maximum array value for the given array ‘X’. 
Note:
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 10^4
1<= X[i] <= 10^5

Time Limit: 1 sec
Sample Input 1 :
2
2
1 2  
3
1 1 1 
Sample Output 1 :
4
2
Explanation Of Sample Input 1 :
For test case 1: 

From this array, we can have 2 arrays i.e. ‘[1,2]’ and ‘[2,1]’
For ‘[1,2]’ the value will be equal to ‘ (1-0)^2 + (2-1)^2’ which is equal to 2.
For ‘[2,1]’ the value will be equal to ‘ (2-0)^2 + (1-1)^2’ which is equal to 4.
Hence answer is ‘4’             

For test case 2:
In this case, there is only one array you can get after rearranging elements of ‘X’.
For array ‘[1,1,1]’ answer will be ‘1+0+1=2’.
Hence the answer is ‘2’.
Sample Input 2:
2
3
1 2 3
3
10 12 3
Sample Output 2:
11
226
Hint

'(X[i] - i)' ^ 2 = 'X[i]*X[i]' + 'i*i' - '2*X[i]*i', now solve the summation of these separately. 

Approaches (1)
Maths + Greedy

Approach:

 

  • Now (X[i]-i)*2 = X[i]*X[i] + i*i - 2*X[i]*i.
  • We will solve the summation of all these terms separately.
  • For ‘X[i]*X[i]’ and ‘i*i’ we can easily compute these terms using one loop(As the elements will remain the same after rearranging).
  • Now, to maximize the value of the array, we need to minimize the value summation of ‘X[i]*i’.
  • So we need to choose an order of elements that minimize the summation of ‘X[i]*i’.
  • So let’s say after rearranging we have a pair of the index (i,j) such that ‘X[i]>X[j]’ where 'i' > 'j', then we will surely swap their values as it minimizes our value of summation of ‘X[i]*i’, so by this observation, we can say our final array will be sorted array of the array ‘X’ in non-increasing order.
  • So now we can just sort our array in non-increasing order and calculate the summation of ‘(X[i]-i)*(X[i]-i)’.


 

Algorithm:

 

  • Initialize a 64-bit integer variable ‘ans’ = 0.
  • Sort the array X and reverse it.
  • Iterate our array ‘X’ for ‘i’ in the range ‘[0, N-1]’ both inclusive
    • Add value (X[i]-i)*(X[i]-i) to ans.
  • Return ‘ans’ as the final output.
Time Complexity

O(N*logN), where ‘N’ is the length of the array ‘X’.

 

Since we are sorting an array of length ‘N’, which takes O(NlogN) time, the rest operations are performed in O(N) or O(1) time. Hence, our time complexity is O(NlogN).

Space Complexity

O(1)

 

Since we are only using constant extra space, the Space Complexity is O(1).

Code Solution
(100% EXP penalty)
Maximize
Full screen
Console