Given an Array, three operations can be performed using any external number x.

- Add x to an element once
- Subtract x from an element once
- Perform no operation on the element
- Count of unique elements is 1. Answer is YES with x = 0
- Count of unique elements is 2. Answer is YES with x = Difference of two unique elements.
- Count of unique elements is 3.
- If difference between mid and max is same as difference between mid and min, answer is YES with x = difference between mid and max or mid and min.
- Otherwise answer is NO.

- Length of array pair formed where one contains all distinct elements and other all same elements
- Maximum possible sum of a window in an array such that elements of same window in other array are unique
- Find if it is possible to make all elements of an array equal by the given operations
- Check whether it is possible to make both arrays equal by modifying a single element
- External Sorting
- Make all elements zero by decreasing any two elements by one at a time
- Find smallest possible Number from a given large Number with same count of digits
- Possible to make a divisible by 3 number using all digits in an array
- Minimum delete operations to make all elements of array same
- Minimum replacements to make elements of a ternary array same
- Minimum Group Flips to Make Binary Array Elements Same
- Minimize steps required to make all array elements same by adding 1, 2 or 5
- Count of all possible pairs of array elements with same parity
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Minimum gcd operations to make all array elements one
- Check whether an array can be fit into another array rearranging the elements in the array
- Maximize product of same-indexed elements of same size subsequences
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Check if it is possible to make array increasing or decreasing by rotating the array
- Find whether an array is subset of another array using Map

Find whether there exists a number X, such that if the above operations are performed with the number X, the resulting array has equal elements.

If the number exists, print “YES” and the value, space separated, else print “NO”

**Examples:**

Input :[1, 1, 3, 5, 5]Output :YES, x = 2Explanation :The number 2 can be added to the first two elements and can be subtracted from the last two elements to obtain a common element 3 throughout the arrayInput :[1, 3, 5, 7, 9]Output :NO

The idea is to form groups of unique elements from given array. Following cases arise :

In Python, we can quickly find unique elements using set in Python.

`# Program in python 2.x to find an element X ` `# that can be used to operate on an array and ` `# get equal elements ` ` ` `# Prints "YES" and an element x if we can ` `# equalize array using x. Else prints "NO" ` `def` `canEqualise(array): ` ` ` ` ` `# We all the unique elements (using set ` ` ` `# function). Then we sort unique elements. ` ` ` `uniques ` `=` `sorted` `(` `set` `(array)) ` ` ` ` ` `# if there are only 1 or 2 unique elements, ` ` ` `# then we can add or subtract x from one of them ` ` ` `# to get the other element ` ` ` `if` `len` `(uniques) ` `=` `=` `1` `: ` ` ` `print` `(` `"YES "` `+` `"0"` `) ` ` ` `elif` `len` `(uniques) ` `=` `=` `2` `: ` ` ` `print` `(` `"YES "` `+` `str` `(uniques[` `1` `] ` `-` `uniques[` `0` `])) ` ` ` ` ` `# If count of unique elements is three, then ` ` ` `# difference between the middle and minimum ` ` ` `# should be same as difference between maximum ` ` ` `# and middle ` ` ` `elif` `len` `(uniques) ` `=` `=` `3` `: ` ` ` `if` `uniques[` `2` `] ` `-` `uniques[` `1` `] ` `=` `=` `uniques[` `1` `] ` `-` `uniques[` `0` `]: ` ` ` `X ` `=` `uniques[` `2` `] ` `-` `uniques[` `1` `] ` ` ` `print` `(` `"YES "` `+` `str` `(X)) ` ` ` `else` `: ` ` ` `print` `(` `"NO"` `) ` ` ` ` ` `# if there are more than three unique elements, then ` ` ` `# we cannot add or subtract the same value from all ` ` ` `# the elements. ` ` ` `else` `: ` ` ` `print` `(` `"NO"` `) ` ` ` `# Driver code ` `array ` `=` `[` `55` `, ` `52` `, ` `52` `, ` `49` `, ` `52` `] ` `canEqualise(array) ` |

*chevron_right*

*filter_none*

Output:

YES 3

This code has complexity **O(n log n)**

The same problem could be extended to ask for two numbers required to equalize the array. Following the same process, we would require 5 unique elements in the array to require two numbers to equalize the array. So to require n numbers to equalize an array, we would require (2n + 1) unique elements in the array.

This article is contributed by **Deepak Srivatsav**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.