
Asked in SPRINKLR
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.
Be the first one to answer
Add answer anonymously...
Top Full Stack Engineer Interview Questions Asked at SPRINKLR
Q. Rank from Stream Problem Statement Given an array of integers ARR and an integer...read more
Q. Smaller Elements Count Problem Statement Given an array of size N, return a coun...read more
Q. Edit Distance Problem Statement Given two strings S and T with lengths N and M r...read more
Interview Questions Asked to Full Stack Engineer at Other Companies
Top Skill-Based Questions for SPRINKLR Full Stack Engineer
Algorithms Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

