Minimum Operations to Equalize Array

Given an integer array ARR of length N where ARR[i] = (2*i + 1), determine the minimum number of operations required to make all the elements of ARR equal. In a single operation, you can pick two elements i and j, increment ARR[i] by 1, and decrement ARR[j] by 1.

Input:

The first line contains an integer T, the number of test cases. 
For each test case, there is a single integer N, the length of the array ARR.

Output:

Output the minimum number of operations required for each test case on a separate line.

Example:

Input:
T = 2 
N = 3
N = 4
Output:
2 
4
Explanation:

For N = 3, the array is [1, 3, 5]. Two operations are needed to make all elements equal to 3.
For N = 4, the array is [1, 3, 5, 7]. Four operations are needed to make all elements equal to 4.

Constraints:

  • 1 ≤ T ≤ 1000
  • 1 ≤ N ≤ 109

Note:

You are not required to print anything; just implement the provided function.

Follow-Up:

Consider solving this in constant time and space complexity.

AnswerBot
4mo

The minimum number of operations required to make all elements of the given array equal.

  • Calculate the target value as the sum of all elements divided by the length of the array.

  • Find the absolute diffe...read more

Help your peers!
Select
Add answer anonymously...

Procol Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Power of a Number Problem You are given two integers, X and N. Your task is to c...read more
A Software Developer Intern was asked Q. Minimum Operations to Equalize Array Given an integer array ARR of length N wher...read more
A Software Developer Intern was asked Q. Search in Infinite Sorted 0-1 Array Problem Statement You are provided with an i...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Power of a Number Problem You are given two integers, X and N. Your task is to c...read more
A Software Developer Intern was asked Q2. Minimum Operations to Equalize Array Given an integer array ARR of length N wher...read more
A Software Developer Intern was asked Q3. Pair Sum Problem Statement You are given an integer array 'ARR' of size 'N' and ...read more
Procol Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits