How to make all elements equal from a list in minimum operations in Python using math.ceil()

Tejas Khartude
1 min readJan 15, 2023

Today, we are going to learn how to find minimum operations required to make all the elements equal from the list using Python as a programming language. I am using PyCharm as a IDE. We are going to use math.ceil() , it rounds a number to the nearest integer, if necessary, and returns the result. We are going to follow these following steps to get our intended output.

Steps :-

  1. Find the maximum and minimum elements in the array.
  2. Calculate the difference between the maximum and minimum elements.
  3. Divide the difference by the number of elements in the array (minus one, since you don’t need to change the last element)
  4. Round up the result to the nearest integer using the math.ceil() function

Below is the code that I have written.

import math


class Solution(object):

def make_idential(self, arr):
# [1,2,3,4]
minimum = min(arr)
maximum = max(arr)
diff = maximum - minimum
operations = math.ceil(diff / (len(arr) - 1))
return operations


if __name__ == "__main__":
st = [1, 3, 5, 7, 9]
print(Solution().make_idential(st)) # Output will be 2

Tip:- To be a better developer , I recommend you to always debug the code for better understanding of the flow as they say it’s 20% writing the code and 80% debugging. :)

I have added a Github repository here about the interview questions asked in Google interviews, please do check it :) https://github.com/klaus19/Google-Leetcode-Questions

https://github.com/klaus19/LeetcodeinPython

--

--

Tejas Khartude

Well, I work with Android using Kotlin and Java. I have developed keen interest in Python.