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.
- Find the number of operations required to make all array elements Equal
- Find the minimum number of elements that should be removed to make an array good
- Find the minimum number of operations required to make all array elements equal
- Minimum number of operations on an array to make all elements 0
- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- Minimum number of increment-other operations to make all array elements equal.
- Find minimum number of merge operations to make an array palindrome
- Value to be subtracted from array elements to make sum of all elements equals K
- Find the number of elements greater than k in a sorted array
- Find a number that divides maximum array elements
- Given an array of size n and a number k, find all elements that appear more than n/k times
- Find integers that divides maximum number of elements of the array
- Make alphabets using the elements of an array
- Minimum gcd operations to make all array elements one
- Make all elements of an array equal with the given operation
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”
Input : [1, 1, 3, 5, 5] Output : YES, x = 2 Explanation : 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 array Input : [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.
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 firstname.lastname@example.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.