- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find minimum operations to make array equal using Python

Suppose we have a value n, consider an array nums with n elements, where arr[i] = (2*i)+1 for all i. Now in one operation, we can choose two indices x and y where 0 <= x, y < n and subtract 1 from nums[x] and add 1 to nums[y]. We have to make all the elements of the array same. So if we have n we have to find the minimum number of operations required to make all the elements of nums same.

So, if the input is like n = 4, then the output will be 4 as for n the array is [1,3,5,7], now after first operation we can make array like [2,3,5,6], after second operation we can make [3,3,5,5],then after third [4,3,4,5] and finally [4,4,4,4]. So now all are equal.

To solve this, we will follow these steps −

ans:= 0

if n is same as 1, then

return ans

q:= quotient of (n/2) -1

j:= 1

while q>=0, do

ans:= ans + (n-j)

q := q - 1

j := j + 2

return ans

Let us see the following implementation to get better understanding −

## Example

def solve(n): ans=0 if n==1: return ans q=(n//2)-1 j=1 while q>=0: ans=ans+(n-j) q-=1 j+=2 return ans n = 4 print(solve(n))

## Input

4

## Output

4

- Related Articles
- Program to find minimum operations to make the array increasing using Python
- Program to find minimum operations needed to make two arrays sum equal in Python
- Minimum operations required to make all the array elements equal in C++
- Program to find minimum one bit operations to make integers zero in Python
- Program to find minimum number of operations to make string sorted in Python
- Program to find equal sum arrays with minimum number of operations in Python
- Program to find minimum numbers of function calls to make target array using Python
- Minimum number of given operations required to make two strings equal using C++.
- Find minimum operations needed to make an Array beautiful in C++
- Program to find minimum moves to make array complementary in Python
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to find minimum number of operations required to make one number to another in Python
- Minimum move to end operations to make all strings equal in C++
- Number of operations required to make all array elements Equal in Python
- C++ program to find minimum how many operations needed to make number 0