Find K such that repeated subtraction of K from Array elements make the Array equal
Last Updated :
14 Jan, 2022
Given an array arr[] of size N, the task is to find the value of an integer K such that its repeated subtraction from array elements will make all array elements in minimum operations.
Example:
Input: arr[] = {5, 3, 3, 7}
Output: 2
Explanation: Minimum 2 operations must be performed:
1st operation: subtract 2 from elements at indices {0, 3}, arr[] = {3, 3, 3, 5}
2nd operation: subtract 2 from element at index 3, arr[] = {3, 3, 3, 3}
So, the value of K = 2
Input: arr[] = {-1, 0, -1, 0, -1}
Output: 1
Approach: To make all elements in the array arr equal, all elements need to be changed to the smallest value in the array. So, the gcd of the difference of all array elements to the smallest element will be the value of K. Now, to solve the following problem, follow the below steps:
- Create a variable mn to store the minimum element in the array arr.
- Now, traverse the array arr and find the gcd of the differences between all array elements and mn. Store this value in a variable K.
- Return K, as the answer to this question.
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 value to be
// subtracted from array elements
// to make all elements equal
int findtheValue(int arr[], int N)
{
// Minimum element in the array
int mn = *min_element(arr, arr + N);
int K = arr[0] - mn;
// Traverse the array to find the gcd
// of the differences between
// all array elements and mn
for (int i = 1; i < N; ++i) {
K = __gcd(K, arr[i] - mn);
}
return K;
}
// Driver Code
int main()
{
int arr[] = { 5, 3, 3, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << findtheValue(arr, N);
return 0;
}
Java
// Java program for the above approach
class GFG {
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int getMin(int[] inputArray){
int minValue = inputArray[0];
for(int i = 1; i < inputArray.length; i++){
if(inputArray[i] < minValue){
minValue = inputArray[i];
}
}
return minValue;
}
// Function to find the value to be
// subtracted from array elements
// to make all elements equal
static int findtheValue(int[] arr, int N)
{
// Minimum element in the array
int mn = getMin(arr);
int K = arr[0] - mn;
// Traverse the array to find the gcd
// of the differences between
// all array elements and mn
for (int i = 1; i < N; ++i) {
K = gcd(K, arr[i] - mn);
}
return K;
}
// Driver Code
public static void main(String args[])
{
int[] arr = { 5, 3, 3, 7 };
int N = arr.length;
System.out.println(findtheValue(arr, N));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python program for the above approach
import math
# Function to find the value to be
# subtracted from array elements
# to make all elements equal
def findtheValue(arr, N):
# Minimum element in the array
mn = min(arr)
K = arr[0] - mn
# Traverse the array to find the gcd
# of the differences between
# all array elements and mn
for i in range(1, N):
K = math.gcd(K, arr[i] - mn)
return K
# Driver Code
if __name__ == "__main__":
arr = [5, 3, 3, 7]
N = len(arr)
print(findtheValue(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Linq;
class GFG {
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the value to be
// subtracted from array elements
// to make all elements equal
static int findtheValue(int[] arr, int N)
{
// Minimum element in the array
int mn = arr.Min();
int K = arr[0] - mn;
// Traverse the array to find the gcd
// of the differences between
// all array elements and mn
for (int i = 1; i < N; ++i) {
K = gcd(K, arr[i] - mn);
}
return K;
}
// Driver Code
public static void Main()
{
int[] arr = { 5, 3, 3, 7 };
int N = arr.Length;
Console.WriteLine(findtheValue(arr, N));
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript program for the above approach
let gcd = function(a, b) {
if (!b) {
return a;
}
return gcd(b, a % b);
}
// Function to find the value to be
// subtracted from array elements
// to make all elements equal
function findtheValue(arr, N)
{
// Minimum element in the array
let mn = Math.min(...arr)
let K = arr[0] - mn;
// Traverse the array to find the gcd
// of the differences between
// all array elements and mn
for (let i = 1; i < N; ++i) {
K = gcd(K, arr[i] - mn);
}
return K;
}
// Driver Code
let arr = [ 5, 3, 3, 7 ]
let N = arr.length
document.write(findtheValue(arr, N))
// This code is contributed by rohitsingh07052.
</script>
Time Complexity: O(Nlog(mn))
Auxiliary Space: O(1)
Similar Reads
Find maximum array sum after making all elements same with repeated subtraction Given an array of n elements, find out the maximum possible sum of all elements such that all the elements are equal. Only operation allowed is choosing any two elements and replacing the larger of them by the absolute difference of the two. Examples: Input : 9 12 3 6 Output : 12 Explanation : 9 12
6 min read
Make all elements of an array equal with the given operation Given an array arr[] of n integers and an integer k. The task is to make all the elements of arr[] equal with the given operation. In a single operation, any non-negative number x ? k (can be a floating point value) can be added to any element of the array and k will be updated as k = k - x. Print Y
5 min read
Minimum sum after subtracting multiples of k from the elements of the array Given an integer K and an integer array, the task is to find the minimum possible sum of all the elements of the array after they are reduced by subtracting a multiple of K from each element (the result must be positive and every element of the array must be equal after this reduction). If the array
15 min read
Make max elements in B[] equal to that of A[] by adding/subtracting integers in range [0, K] Given two arrays A[] and B[] and an integer K, the task is to maximize the count of integers of array B[] that can be made equal with array A[] by adding or subtracting any integer in the range [0, K]. Examples: Input: K=5, A[] = [100, 65, 35, 85, 55], B[] = [30, 60, 75, 95]Output: 3Explanation:30 +
6 min read
Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other Given an array arr[] and an integer K, the task is to find the minimum number of operations to make all the elements of the array arr[] equal. In one operation, K is subtracted from an element and this K is added into other element. If it is not possible then print -1. Examples: Input: arr[] = {5, 8
7 min read