Open In App

Maximise K that can be reduced from Array to make all elements equal

Last Updated : 31 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum value of K
int maxValue(int arr[], int N)
{
    // Sorting array of integers
    sort(arr, arr + N);
 
    // Initializing a variable
    int ans = 0;
 
    // Iterating using a for loop
    for (int i = 1; i < N; i++) {
 
        // Find the gcd of ans and
        // (arr[i] - arr[i - 1])
        ans = __gcd(ans, arr[i] - arr[i - 1]);
    }
 
    // Return the answer
    return ans;
}
 
// Driver code
int main()
{
    // Initializing an array of integers
    int arr[] = { 3, 7, 5, 3, 3, 7 };
 
    // Number of elements in the array
    int N = sizeof(arr) / sizeof(int);
 
    int ans = maxValue(arr, N);
    if (ans > 0)
        cout << ans;
    else
        cout << "-1";
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.BigInteger;
 
class GFG {
 
  //calculate gcd
  static int gcd(int a, int b)
  {
    return b == 0 ? a : gcd(b, a % b);  
  }
 
  // Function to find the maximum value of K
  static int maxValue(int arr[], int N)
  {
    // Sorting array of integers
    Arrays.sort(arr);
 
    // Initializing a variable
    int ans = 0;
 
    // Iterating using a for loop
    for (int i = 1; i < N; i++) {
 
      // Find the gcd of ans and
      // (arr[i] - arr[i - 1])
      ans = gcd(ans, arr[i] - arr[i - 1]);
    }
 
    // Return the answer
    return ans;
  }
 
  // Driver code
  public static void main (String[] args) {
    int arr[] = { 3, 7, 5, 3, 3, 7 };
 
    // Number of elements in the array
    int N = arr.length;
 
    int ans = maxValue(arr, N);
    if (ans > 0)
      System.out.println(ans);
    else
      System.out.println("-1");
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3




# Python program for the above approach
 
# calculate gcd
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b);
 
# Function to find the maximum value of K
def maxValue(arr, N):
    # Sorting array of integers
    arr.sort();
 
    # Initializing a variable
    ans = 0;
 
    # Iterating using a for loop
    for i in range(1,N):
       
        # Find the gcd of ans and
        # (arr[i] - arr[i - 1])
        ans = gcd(ans, arr[i] - arr[i - 1]);
 
    # Return the answer
    return ans;
 
# Driver code
if __name__ == '__main__':
    arr = [3, 7, 5, 3, 3, 7];
 
    # Number of elements in the array
    N = len(arr);
 
    ans = maxValue(arr, N);
    if (ans > 0):
        print(ans);
    else:
        print("-1");
 
# This code is contributed by shikhasingrajput


C#




// C# program for the above approach
using System;
class GFG {
 
  // JavaScript code for the above approach
  static int __gcd(int a, int b)
  {
    // Everything divides 0
    if (a == 0)
      return b;
    if (b == 0)
      return a;
 
    // base case
    if (a == b)
      return a;
 
    // a is greater
    if (a > b)
      return __gcd(a - b, b);
    return __gcd(a, b - a);
  }
 
  // Function to find the maximum value of K
  static int maxValue(int[] arr, int N)
  {
 
    // Sorting array of integers
    Array.Sort(arr);
 
    // Initializing a variable
    int ans = 0;
 
    // Iterating using a for loop
    for (int i = 1; i < N; i++) {
 
      // Find the gcd of ans and
      // (arr[i] - arr[i - 1])
      ans = __gcd(ans, arr[i] - arr[i - 1]);
    }
 
    // Return the answer
    return ans;
  }
  public static void Main()
  {
    // Initializing an array of integers
    int[] arr = { 3, 7, 5, 3, 3, 7 };
 
    // Number of elements in the array
    int N = arr.Length;
 
    int ans = maxValue(arr, N);
    if (ans > 0)
      Console.Write(ans);
    else
      Console.Write(-1);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
       // JavaScript code for the above approach
       function __gcd(a, b) {
           // Everything divides 0
           if (a == 0)
               return b;
           if (b == 0)
               return a;
 
           // base case
           if (a == b)
               return a;
 
           // a is greater
           if (a > b)
               return __gcd(a - b, b);
           return __gcd(a, b - a);
       }
 
       // Function to find the maximum value of K
       function maxValue(arr, N)
       {
        
           // Sorting array of integers
           arr.sort(function (a, b) { return a - b })
 
           // Initializing a variable
           let ans = 0;
 
           // Iterating using a for loop
           for (let i = 1; i < N; i++) {
 
               // Find the gcd of ans and
               // (arr[i] - arr[i - 1])
               ans = __gcd(ans, arr[i] - arr[i - 1]);
           }
 
           // Return the answer
           return ans;
       }
 
       // Driver code
       // Initializing an array of integers
       let arr = [3, 7, 5, 3, 3, 7];
 
       // Number of elements in the array
       let N = arr.length;
 
       let ans = maxValue(arr, N);
       if (ans > 0)
           document.write(ans);
       else
           document.write(-1)
 
        // This code is contributed by Potta Lokesh
   </script>


 
 

Output

2

 

Time Complexity: O(N * logN)
Auxiliary Space: O(1) 

 



Similar Reads

Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum
Given an array arr[] consisting of N integers, the task is to check if it is possible to reduce the array elements to 0 by repeatedly subtracting the minimum of any pair of consecutive array elements from both the elements in the pair. If it is possible, then print "Yes". Otherwise, print "No". Examples: Input: arr[] = {2, 3, 3, 4, 2}Output: YesExp
10 min read
Minimum decrements to make an Array at most 0 such that all array elements are cyclically decremented after a number is reduced to 0
Given a circular array arr[] of N integers and an array cost[], the task is to calculate the minimum number of operations required to make all elements of the array equal to 0, where at each operation decrement the value of an index i by 1. If the value of an index becomes 0, decrement the value of arr[i+1] by cost[i], decrement the value of arr[i
6 min read
Maximum number of times Array can be reduced in half when its all elements are even
Given an array arr, the task is to perform operations on the array when all elements in the array are even. In one operation, replace each integer X in the array by X/2. Find maximum possible number of operations that you can perform. Examples: Input: arr[] = {8, 12, 40}Output: 2Explanation: Initially, {8, 12, 40} are present in array. Since all th
6 min read
Check if all array elements can be reduced to less than X
Given an array A[] consisting of N positive integers and an integer X, the task is to determine if it is possible to convert all array elements to less than X by performing the following operations: Select 2 distinct indices j and k.Select an index i, where A[i] &gt; X.Replace A[i] = gcd(A[j], A[k]) if and only if gcd(A[j], A[k]) ? 1.Examples: Inpu
16 min read
Minimize Array elements to be reduced to make subsequences sum 1 to Array max possible
Given an array a[] of positive integers. Subtract any positive integer from any number of elements of the array such that there exists every possible subsequence with sum 1 to s, where s denotes the sum of all the elements of the array. Find the minimum possible number of the array element to be subtracted. Input: a[] = {4, 3, 2, 2, 1, 6}Output: 2E
6 min read
Check if sum of the given array can be reduced to 0 by reducing array elements by K
Given an array arr[] consisting of N integers and an integer K, the task is to check if the sum of the array can be reduced to 0 by subtracting array elements by K any number of times. Examples: Input: arr[ ]= {-3, 2, -1, 5, 1}, K=2Output: "Yes"Explanation: Sum of the array is 4. Therefore, decreasing two elements at any index by K( = 2), makes the
5 min read
Check if an array can be reduced to at most length K by removal of distinct elements
Given an array arr[] consisting of N positive integers and an integer K, the task is to check if it is possible to reduce the size of the array to at most K or not by removing a subset of the distinct array elements. If it is possible, then print "Yes". Otherwise, print "No". Examples: Input: arr[] = {2, 2, 2, 3}, K = 3Output: YesExplanation:By rem
5 min read
Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements
Given an array arr[] of size N, the task is to make all array elements equal to 0 by replacing all elements of a subsequences of equal elements by any integer, minimum number of times. Examples: Input: arr[] = {3, 7, 3}, N = 3Output: 2Explanation:Selecting a subsequence { 7 } and replacing all its elements by 0 modifies arr[] to { 3, 3, 3 }. Select
5 min read
Check if sum of array can be reduced to zero by repetitively reducing array element by their index value
Given an array arr[] consisting of N integers, the task is to determine if the sum of array elements can be reduced to 0 by performing the following operations any number of times: Choose an element A[i] and reduce A[i] by i(1-based indexing), any number of times, possibly 0.If the sum can be reduced to 0, print "Yes". Otherwise, print "No". Exampl
5 min read
Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal
Given an array arr[] consisting of N elements, the task is to find the minimum number of increments required to be performed on the given array such that after selecting any array element at any index and splitting its value to the other array elements makes all other N - 1 elements equal. Examples: Input: N = 3, arr[] = {2, 3, 7}Output: 2Explanati
6 min read
Article Tags :
Practice Tags :