Find maximum array sum after making all elements same with repeated subtraction
Last Updated :
12 Sep, 2023
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 3 6
replace a2 = 12 with a2-a4 = 12 - 6 => 6
i.e, 9 6 3 6
replace a4 = 6 with a4-a3 = 6 - 3 => 3
i.e, 9 6 3 3
replace a1 = 9 with a1-a2 = 9 - 6 => 3
i.e, 3 6 3 3
replace a2 = 6 with a2-a4 = 6 - 3 => 3
i,e. 3 3 3 3
Now, at this point we have all the elements equal,
hence we can return our answer from here.
Input : 4 8 6 10
Output : 8
Explanation :
Resultant array formed will be:
4 8 6 10
replace a4 = 10 with a4-a1 = 10 - 4 => 6
i.e, 4 8 6 6
replace a3 = 6 with a3-a1 = 6 - 4 => 2
i.e, 4 8 2 6
replace a2 = 8 with a2-a4 = 8 - 6 => 2
i.e, 4 2 2 6
replace a4 = 6 with a4-a1 = 6 - 4 => 2
i,e. 4 2 2 2
replace a1 = 4 with a1-a2 = 4 - 2 => 2
i,e. 2 2 2 2
Now, at this point we have all the elements equal,
hence we can return our answer from here.
By analyzing the given operation i.e,
ai = ai - aj where ai > aj
We see that this is similar to finding GCD through Euclidean algorithm as:
GCD(a, b) = GCD(b, a - b)
And also, the order of rearrangement does not matter, we can proceed by taking any two elements and replace the larger value by the absolute difference of the two, and repeat among them till the difference comes out to be zero[both the elements be same]. That is, taking out the GCD of any two numbers. The reason for this to work is, GCD is associative and commutative.
So the idea is the take the GCD of all the elements at once and replace all the elements by that result.
Implementation:
C++
// Maximum possible sum of array after repeated
// subtraction operation.
#include<bits/stdc++.h>
using namespace std;
int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
int findMaxSumUtil(int arr[], int n)
{
int finalGCD = arr[0];
for (int i = 1; i < n; i++)
finalGCD = GCD(arr[i], finalGCD);
return finalGCD;
}
// This function basically calls findMaxSumUtil()
// to find GCD of all array elements, then it returns
// GCD * (Size of array)
int findMaxSum(int arr[], int n)
{
int maxElement = findMaxSumUtil(arr, n);
return (maxElement * n);
}
// Driver code
int main()
{
int arr[] = {8, 20, 12, 36};
int n = sizeof(arr)/sizeof(arr[0]);
cout << findMaxSum(arr, n) << endl;
return 0;
}
Java
// Maximum possible sum of array after repeated
// subtraction operation.
import java.io.*;
class GFG {
static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
static int findMaxSumUtil(int arr[], int n)
{
int finalGCD = arr[0];
for (int i = 1; i < n; i++)
finalGCD = GCD(arr[i], finalGCD);
return finalGCD;
}
// This function basically calls
// findMaxSumUtil() to find GCD of all
// array elements, then it returns
// GCD * (Size of array)
static int findMaxSum(int arr[], int n)
{
int maxElement = findMaxSumUtil(arr, n);
return (maxElement * n);
}
// Driver code
public static void main (String[] args) {
int arr[] = {8, 20, 12, 36};
int n = arr.length;
System.out.println(findMaxSum(arr, n));
}
}
//This code is contributed by vt_m.
Python3
# Maximum possible sum of array after
# repeated subtraction operation.
def GCD(a, b):
if (b == 0): return a
return GCD(b, a % b)
def findMaxSumUtil(arr, n):
finalGCD = arr[0]
for i in range(1, n):
finalGCD = GCD(arr[i], finalGCD)
return finalGCD
# This function basically calls
# findMaxSumUtil() to find GCD of
# all array elements, then it returns
# GCD * (Size of array)
def findMaxSum(arr, n):
maxElement = findMaxSumUtil(arr, n)
return (maxElement * n)
# Driver code
arr = [8, 20, 12, 36]
n = len(arr)
print(findMaxSum(arr, n))
# This code is contributed by Anant Agarwal.
C#
// C# Code for Maximum possible sum of array
// after repeated subtraction operation.
using System;
class GFG {
static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
static int findMaxSumUtil(int []arr, int n)
{
int finalGCD = arr[0];
for (int i = 1; i < n; i++)
finalGCD = GCD(arr[i], finalGCD);
return finalGCD;
}
// This function basically calls
// findMaxSumUtil() to find GCD of all
// array elements, then it returns
// GCD * (Size of array)
static int findMaxSum(int []arr, int n)
{
int maxElement = findMaxSumUtil(arr, n);
return (maxElement * n);
}
// Driver code
public static void Main () {
int []arr = {8, 20, 12, 36};
int n = arr.Length;
Console.WriteLine(findMaxSum(arr, n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP code to find Maximum possible
// sum of array after repeated
// subtraction operation.
function GCD($a, $b)
{
if ($b == 0)
return $a;
return GCD($b, $a % $b);
}
function findMaxSumUtil( $arr, $n)
{
$finalGCD = $arr[0];
for ( $i = 1; $i < $n; $i++)
$finalGCD = GCD($arr[$i], $finalGCD);
return $finalGCD;
}
// This function basically
// calls findMaxSumUtil()
// to find GCD of all array
// elements, then it returns
// GCD * (Size of array)
function findMaxSum( $arr, $n)
{
$maxElement = findMaxSumUtil($arr, $n);
return ($maxElement * $n);
}
// Driver Code
$arr = array(8, 20, 12, 36);
$n = count($arr);
echo findMaxSum($arr, $n) ;
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript Code for Maximum possible sum of array
// after repeated subtraction operation.
function GCD(a, b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
function findMaxSumUtil(arr, n)
{
let finalGCD = arr[0];
for (let i = 1; i < n; i++)
finalGCD = GCD(arr[i], finalGCD);
return finalGCD;
}
// This function basically calls
// findMaxSumUtil() to find GCD of all
// array elements, then it returns
// GCD * (Size of array)
function findMaxSum(arr, n)
{
let maxElement = findMaxSumUtil(arr, n);
return (maxElement * n);
}
let arr = [8, 20, 12, 36];
let n = arr.length;
document.write(findMaxSum(arr, n));
</script>
Time Complexity: O(N * log max(a, b)), where N is the size of the given array and a & b are the elements of the array of which gcd is to be done.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array Given an array a[] consisting of N integers, the task is to find the maximum possible sum that can be achieved by deducting any value, say X, from all elements of a subarray. Examples: Input: N = 3, a[] = {80, 48, 82} Output: 144 Explanation: 48 can be deducted from each array element. Therefore, su
11 min read
Minimum operation to make all elements equal in array Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output :
11 min read
Maximum subarray sum with same first and last element formed by removing elements Given an array arr[] of N integers, the task is to find the maximum sum of subarray having length an at least 2 whose first and last elements are the same after removing any number of array elements. If there exists no such array, then print 0. Examples: Input: arr[] = {-1, -3, -2, 4, -1, 3}Output:
6 min read
Minimize difference with 0 after adding or subtracting any element of the given Array Given an array arr[] of N integers, the task is to find the minimum difference from 0 after adding or subtracting any element of the array from it. Examples: Input: N = 4, arr[] = {1, 2, 1, 3}Output: 1Explanation: Add 1 and 2 with 0 and subtract 1 and 3.So total sum = (1 + 2 - 1 - 3) = -1. Differenc
13 min read
Minimum Cost to make all array elements equal using given operations Given an array arr[] of positive integers and three integers A, R, M, where The cost of adding 1 to an element of the array is A,the cost of subtracting 1 from an element of the array is R andthe cost of adding 1 to an element and subtracting 1 from another element simultaneously is M. The task is t
12 min read