
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to make all elements equal by performing given operation in Python
Suppose we have given a list of numbers called nums, we want to make the values equal. Now let an operation where we pick one element from the list and increment every other value. We have to find the minimum number of operations required to make element values equal.
So, if the input is like [2, 4, 5], then the output will be 5.
To solve this, we will follow these steps −
- min_val := minimum of nums
- s := 0
- for each num in nums, do
- s := s + (num - min_val)
- return s
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): min_val = min(nums) s = 0 for num in nums: s += num - min_val return s ob = Solution() nums = [2, 4, 5] print(ob.solve(nums))
Input
[2, 4, 5]
Output
5
- Related Articles
- Minimum operation to make all elements equal in array in C++
- Number of operations required to make all array elements Equal in Python
- Program to check final answer by performing given stack operations in Python
- Minimum operations of given type to make all elements of a matrix equal in C++
- Program to make the XOR of all segments equal to zero in Python
- Minimum operations required to make all the array elements equal in C++
- Minimum number of moves to make all elements equal using C++.
- Program to reduce list by given operation and find smallest remaining number in Python
- Check if at least half array is reducible to zero by performing some operation in Python
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- How to make all the elements in a list of equal size in R?
- Program to find number of elements can be removed to make odd and even indexed elements sum equal in Python
- Find the index which is the last to be reduced to zero after performing a given operation in Python
- Write a Python program to shuffle all the elements in a given series
- Find the number of operations required to make all array elements Equal in C++

Advertisements