Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimum value of X to make all array elements equal by either decreasing or increasing by X

  • Last Updated : 11 Nov, 2021
  • Increase any of the array element by X once.
  • Decrease any of the array element by X once.
  • If there are 3 unique elements, if abs(el2-el1) == abs(el3-el2), then the answer is abs(el2-el1). If they are not equal then the answer is -1.
  • If there are 2 unique elements, the answer is (el2 – el1) / 2, if el2 – el1 is even, else the answer is (el2 – el1)
  • If there is a single unique element, then the answer is 0

Time Complexity: O(N)

Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!