Minimum Operations To Make Array Equal

Easy
0/40
Average time to solve is 10m
profile
Contributed by
11 upvotes
Asked in companies
WalmartAmazonArcesium

Problem statement

You are given an array ‘ARR’ of length ‘N’ which is filled with the values such that ARR[i] = (2*i + 1). You have to perform operations on the ‘ARR’ to make all elements of the array equal. In one operation, you can choose two elements from the array ‘ARR’ say ‘i’ and ‘j’, and can increment the value of ‘ARR[i]’ by one and decrement the value of ‘ARR[j]’ by one.

You have to find the minimum number of operations to make all the elements of the array ‘ARR’ equal. It is guaranteed that all elements of the array can be made equal using some operations.

Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer ‘T’, denoting the number of test cases. 

The first line of each test case contains an integer ‘N’, denoting the length of the array.
Output format:
For each test case, print the minimum number of operations to make all the elements of the array equal.

Output for each test case is printed on a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 1000
1 <= N <= 10^9

Time Limit: 1 sec
Sample Input 1:
2
1
3
Sample Output 1:
0
2
Explanation For Sample Input 1:
For the first test case, 

Initially, the array ‘ARR’ is [1]. Since the array has only a single element therefore it will require 0 operations to make all array elements equal. So the output is 0.

For the second test case, 

Initially, the array ‘ARR’ is [1, 3, 5]. So in the first operation, we will increment element 1 by one and decrement element 5 by one and the updated array will look like [2, 3, 4]. 

Then we perform a second operation in which we will increment 2 by one and decrement element 4 by one and the updated array will look like [3, 3, 3]. So finally all the elements of the array become equal in 2 operations. So the output is 2.   
Sample Input 2:
2
6
5
Sample Output 2:
9
6
Hint

All elements of the array will be equal to the average value of the array.

Approaches (2)
Brute Force
  • The idea behind this approach is we have to calculate the average of all the elements of the array ‘ARR’ because the sum of all elements of the array always remains constant.
  • So calculating the average will help us to virtually divide the elements into two halves around the average i.e left and the right. So calculating the answer for the left half only will be our required answer. It is because in one operation we will choose one element from the left half and one element from the right half and therefore performing operation only left will give us the answer. We will choose the elements which are at equal distance from their average i.e (0 and N-1) or (1 and N-2) etc. therefore if the 0'th element requires ‘K’ operations to become equal to average then the (N-1)'th element will also require ‘K’ operations to become equal to the average and this whole operation to convert 0'th element and the (N-1)'th element will be considered as one single operation.

 

Algorithm:

 

  • Construct an array ARR  of size N such that ARR[i] = (2*i + 1) where 0 <= i < N.
  • Now create a variable Sum initialized to 0 and while constructing the above array add all the elements and store the result in Sum.
  • Now create a variable Avg which denotes the average of all array elements. Set Avg as Sum / N.
  • Create a variable minOperations initialized to 0.
  • Since we have to traverse over only on the left half of the array ARR, So we will traverse over the array ARR till N/2 and for each element find the cost to make it equal to the average i.e (Avg - ARR[i]]) and add this cost to the variable minOperations.
  • Finally, return the minOperations which is our required answer.
Time Complexity

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

 

Constructing the array ARR will take O(N) time. And then traversing over half of the array will also takes O(N) time. Therefore, the overall time complexity will be O(N).

Space Complexity

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

 

Since we are constructing an array ARR, which takes O(N) space. So, overall space complexity will be O(N).

Code Solution
(100% EXP penalty)
Minimum Operations To Make Array Equal
Full screen
Console