Problem title
Difficulty
Avg time to solve

Exploring Restaurants
Moderate
35 mins
Encode and Decode
Easy
15 mins
Closest Coprime Ancestor
Hard
50 mins
Find the total number of good Triplets
Moderate
30 mins
Minimum Operations To Make Array Equal
Easy
10 mins
Lexicographically Smallest Subsequence of Distinct Characters.
Easy
20 mins
Remove K Digits
Moderate
25 mins
Can Place Books
Easy
15 mins
Rectangle Area
Ninja
55 mins
NINJA'S INTERVAL
Easy
15 mins
4

Minimum Operations To Make Array Equal

Difficulty: EASY
Contributed By
Avg. time to solve
10 min
Success Rate
90%

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.

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
Follow Up:
Can you do this in constant time and space?
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
Reset Code
Full screen
copy-code
Console