Minimum value of X to make all array elements equal by either decreasing or increasing by X
- 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.