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.
In Python, we can quickly find unique elements using set in Python.
C++
#include<bits/stdc++.h>
using namespace std;
void canEqualise( int array[], int n)
{
set< int > s;
for ( int i=0;i<n;i++)
{
s.insert(array[i]);
}
if (s.size() == 1)
cout<< "YES " << "0" ;
else if (s.size() == 2)
{
auto x = s.begin();
s.erase(x);
auto y = s.begin();
s.erase(y);
cout<< "YES " << (*y-*x);
}
else if (s.size() == 3)
{
auto x = s.begin();
s.erase(x);
auto y = s.begin();
s.erase(y);
auto z = s.begin();
s.erase(z);
if ((*z-*y)==(*y-*x))
cout<< "YES " << (*z-*y);
else
cout<< "NO" ;
}
else
cout<< "NO" ;
}
int main()
{
int array[] = {55, 52, 52, 49, 52};
int n = sizeof (array) / sizeof (array[0]);
canEqualise(array,n);
}
|
Java
import java.util.*;
public class GFG {
static void canEqualise( int array[], int n)
{
Set<Integer> s = new HashSet<Integer>();
for ( int i = 0 ; i < n; i++) {
s.add(array[i]);
}
if (s.size() == 1 )
System.out.println( "YES 0" );
else if (s.size() == 2 ) {
int x = s.stream().findFirst().get();
s.remove(x);
int y = s.stream().findFirst().get();
s.remove(y);
System.out.println( "YES " + (y - x));
}
else if (s.size() == 3 ) {
int x = s.stream().findFirst().get();
s.remove(x);
int y = s.stream().findFirst().get();
s.remove(y);
int z = s.stream().findFirst().get();
s.remove(z);
if ((z - y) == (y - x))
System.out.println( "YES " + (z - y));
else
System.out.println( "NO" );
}
else
System.out.println( "NO" );
}
public static void main(String[] args)
{
int array[] = { 55 , 52 , 52 , 49 , 52 };
int n = array.length;
canEqualise(array, n);
}
}
|
Python
def canEqualise(array):
uniques = sorted ( set (array))
if len (uniques) = = 1 :
print ( "YES " + "0" )
elif len (uniques) = = 2 :
print ( "YES " + str (uniques[ 1 ] - uniques[ 0 ]))
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" )
else :
print ( "NO" )
array = [ 55 , 52 , 52 , 49 , 52 ]
canEqualise(array)
|
C#
using System;
using System.Collections.Generic;
public static class GFG {
public static void canEqualise( int [] array, int n)
{
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < n; i++) {
s.Add(array[i]);
}
if (s.Count == 1) {
Console.Write( "YES " );
Console.Write( "0" );
}
else if (s.Count == 2) {
int x = 0;
int y = 0;
int m = 0;
Console.Write( "YES " );
foreach ( var i in s)
{
if (m == 0) {
x = i;
}
else {
y = i;
}
m++;
}
Console.Write((y - x));
}
else if (s.Count == 3) {
int x = 0;
int y = 0;
int z = 0;
int m = 0;
foreach ( var i in s)
{
if (m == 0) {
x = i;
}
else if (m == 1) {
y = i;
}
else {
z = i;
}
m++;
}
if ((z - y) == (y - x)) {
Console.Write( "YES " );
Console.Write((z - y));
}
else {
Console.Write( "NO" );
}
}
else {
Console.Write( "NO" );
}
}
public static void Main()
{
int [] array = { 55, 52, 52, 49, 52 };
int n = array.Length;
canEqualise(array, n);
}
}
|
Javascript
function canEqualise(array, n) {
let s = new Set(array);
if (s.size === 1)
console.log( "YES 0" );
else if (s.size === 2) {
let x = s.values().next().value;
s. delete (x);
let y = s.values().next().value;
s. delete (y);
console.log(`YES ${y-x}`);
}
else if (s.size === 3) {
let x = s.values().next().value;
s. delete (x);
let y = s.values().next().value;
s. delete (y);
let z = s.values().next().value;
s. delete (z);
if ((z - y) === (y - x))
console.log(`YES ${z-y}`);
else
console.log( "NO" );
}
else
console.log( "NO" );
}
let array = [55, 52, 52, 49, 52];
let n = array.length;
canEqualise(array, n);
|
OUTPUT
YES 3
Time Complexity: O(n logn)//we are running a for loop for n times and inside the loop the set insert operation is taking place which takes log n time to compute
Auxiliary space: O(n). //since we are using a set to store all the elements of the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Mar, 2023
Like Article
Save Article