Maximise K that can be reduced from Array to make all elements equal
Given an array arr[] of size N, the task is to make all elements equal by applying the operation given below any number of times (possibly zero) to any of the elements in the given array.
- Select an element in the array.
- Reduce it by a positive integer K.
Among all such positive k’s, print the maximum K.
Examples:
Input: arr[] = {3, 7, 5, 3, 3, 7}
Output: 2
Explanation: Choose K = 2, decrement both 7s twice and one 5 once. to get all the elements equal to 3
Input: arr[] = {100, -2000, -2000, -2000}
Output: 2100
Input: arr[] = {2, 2, 2}
Output: -1
Explanation: As all the elements are already equal hence there can be infinite number of such K possible.
Approach: The task can be solved on the basis of some observations. All the array elements can be made equal to the minimum element of the array. The maximum K can be obtained by finding the greatest common divisor of the adjacent elements in sorted order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxValue( int arr[], int N)
{
sort(arr, arr + N);
int ans = 0;
for ( int i = 1; i < N; i++) {
ans = __gcd(ans, arr[i] - arr[i - 1]);
}
return ans;
}
int main()
{
int arr[] = { 3, 7, 5, 3, 3, 7 };
int N = sizeof (arr) / sizeof ( int );
int ans = maxValue(arr, N);
if (ans > 0)
cout << ans;
else
cout << "-1" ;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.BigInteger;
class GFG {
static int gcd( int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
static int maxValue( int arr[], int N)
{
Arrays.sort(arr);
int ans = 0 ;
for ( int i = 1 ; i < N; i++) {
ans = gcd(ans, arr[i] - arr[i - 1 ]);
}
return ans;
}
public static void main (String[] args) {
int arr[] = { 3 , 7 , 5 , 3 , 3 , 7 };
int N = arr.length;
int ans = maxValue(arr, N);
if (ans > 0 )
System.out.println(ans);
else
System.out.println( "-1" );
}
}
|
Python3
def gcd(a, b):
return a if b = = 0 else gcd(b, a % b);
def maxValue(arr, N):
arr.sort();
ans = 0 ;
for i in range ( 1 ,N):
ans = gcd(ans, arr[i] - arr[i - 1 ]);
return ans;
if __name__ = = '__main__' :
arr = [ 3 , 7 , 5 , 3 , 3 , 7 ];
N = len (arr);
ans = maxValue(arr, N);
if (ans > 0 ):
print (ans);
else :
print ( "-1" );
|
C#
using System;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int maxValue( int [] arr, int N)
{
Array.Sort(arr);
int ans = 0;
for ( int i = 1; i < N; i++) {
ans = __gcd(ans, arr[i] - arr[i - 1]);
}
return ans;
}
public static void Main()
{
int [] arr = { 3, 7, 5, 3, 3, 7 };
int N = arr.Length;
int ans = maxValue(arr, N);
if (ans > 0)
Console.Write(ans);
else
Console.Write(-1);
}
}
|
Javascript
<script>
function __gcd(a, b) {
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function maxValue(arr, N)
{
arr.sort( function (a, b) { return a - b })
let ans = 0;
for (let i = 1; i < N; i++) {
ans = __gcd(ans, arr[i] - arr[i - 1]);
}
return ans;
}
let arr = [3, 7, 5, 3, 3, 7];
let N = arr.length;
let ans = maxValue(arr, N);
if (ans > 0)
document.write(ans);
else
document.write(-1)
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Last Updated :
31 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...