Given an array of N elements and you can perform two operations on it:
- Increase any of the array element by X once.
- Decrease any of the array element by X once.
The task is to find the minimum most value of X such that all array elements are equal by either applying operation 1, 2 or not applying any operation. If all the array elements cannot be made equal, then print -1.
Examples:
Input: a[] = {1, 4, 4, 7, 4, 1}
Output: 3
Increase the first and last element by 3.
Decrease the fourth element by 3.
Input: {1, 5, 7, 9, 1}
Output: -1
- 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
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinimumX( int a[], int n)
{
set< int > st;
for ( int i = 0; i < n; i++)
st.insert(a[i]);
if (st.size() == 1)
return 0;
if (st.size() == 2) {
int el1 = *st.begin();
int el2 = *st.rbegin();
if ((el2 - el1) % 2 == 0)
return (el2 - el1) / 2;
else
return (el2 - el1);
}
if (st.size() == 3) {
auto it = st.begin();
int el1 = *it;
it++;
int el2 = *it;
it++;
int el3 = *it;
if ((el2 - el1) == (el3 - el2))
return el2 - el1;
else
return -1;
}
return -1;
}
int main()
{
int a[] = { 1, 4, 4, 7, 4, 1 };
int n = sizeof (a) / sizeof (a[0]);
cout << findMinimumX(a, n);
return 0;
}
|
Java
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
class GFG
{
static int findMinimumX( int a[], int n)
{
Set<Integer> st = new HashSet<>();
for ( int i = 0 ; i < n; i++)
st.add(a[i]);
if (st.size() == 1 )
return 0 ;
if (st.size() == 2 )
{
Iterator<Integer> it = st.iterator();
int el1 = it.next();
int el2 = it.next();
if ((el2 - el1) % 2 == 0 )
return (el2 - el1) / 2 ;
else
return (el2 - el1);
}
if (st.size() == 3 )
{
Iterator<Integer> it = st.iterator();
int el1 = it.next();
int el2 = it.next();
int el3 = it.next();
if ((el2 - el1) == (el3 - el2))
return el2 - el1;
else
return - 1 ;
}
return - 1 ;
}
public static void main(String[] args)
{
int a[] = { 1 , 4 , 4 , 7 , 4 , 1 };
int n = a.length;
System.out.println(findMinimumX(a, n));
}
}
|
Python3
def findMinimumX(a, n):
st = set ()
for i in range (n):
st.add(a[i])
if ( len (st) = = 1 ):
return 0
if ( len (st) = = 2 ):
st = list (st)
el1 = st[ 0 ]
el2 = st[ 1 ]
if ((el2 - el1) % 2 = = 0 ):
return int ((el2 - el1) / 2 )
else :
return (el2 - el1)
if ( len (st) = = 3 ):
st = list (st)
el1 = st[ 0 ]
el2 = st[ 1 ]
el3 = st[ 2 ]
if ((el2 - el1) = = (el3 - el2)):
return el2 - el1
else :
return - 1
return - 1
if __name__ = = '__main__' :
a = [ 1 , 4 , 4 , 7 , 4 , 1 ]
n = len (a)
print (findMinimumX(a, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int findMinimumX( int []a, int n)
{
List< int > st = new List< int >();
for ( int i = 0; i < n; i++)
if (!st.Contains(a[i]))
st.Add(a[i]);
if (st.Count == 1)
return 0;
if (st.Count == 2)
{
int el1 = st[0];
int el2 = st[1];
if ((el2 - el1) % 2 == 0)
return (el2 - el1) / 2;
else
return (el2 - el1);
}
if (st.Count == 3)
{
int el1 = st[0];
int el2 = st[1];
int el3 = st[2];
if ((el2 - el1) == (el3 - el2))
return el2 - el1;
else
return -1;
}
return -1;
}
public static void Main(String[] args)
{
int []a = {1, 4, 4, 7, 4, 1};
int n = a.Length;
Console.WriteLine(findMinimumX(a, n));
}
}
|
Javascript
function findMinimumX(a, n)
{
let st = new Set()
for ( var i = 0; i < n; i++)
st.add(a[i])
st = Array.from(st)
st.sort()
if (st.length == 1)
return 0
if (st.length == 2)
{
let el1 = st[0]
let el2 = st[1]
if ((el2 - el1) % 2 == 0)
return Math.floor((el2 - el1) / 2)
else
return (el2 - el1)
}
if (st.length == 3)
{
let el1 = st[0]
let el2 = st[1]
let el3 = st[2]
if ((el2 - el1) == (el3 - el2))
return el2 - el1
else
return -1
}
return -1
}
let a = [1, 4, 4, 7, 4, 1]
let n = a.length
console.log(findMinimumX(a, n))
|
Time complexity O(nlogn).
Space complexity O(n),because it creates a set of unique elements that could potentially contain all n elements in the input array.