- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 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 −
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))
[2, 4, 5]
- 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 number of moves to make all elements equal using C++.
- Minimum operations required to make all the array elements equal in C++
- Program to reduce list by given operation and find smallest remaining number in Python
- How to make all the elements in a list of equal size in R?
- Check if at least half array is reducible to zero by performing some operation in Python
- Find the number of operations required to make all array elements Equal in C++
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Program to find number of elements can be removed to make odd and even indexed elements sum equal in Python
- Reduce a number to 1 by performing given operations in C++
- Python program to print all distinct elements of a given integer array.