Minimizing array sum by subtracting larger elements from smaller ones
Given an array of n elements perform, we need to minimize the array sum. We are allowed to perform the below operation any number of times.
- Choose any two elements from the array say A and B where A > B and then subtract B from A.
Examples:
Input : 1
arr[] = 1
Output : 1
There is no need to apply the above operation
because there is only a single number that is 1.
Hence, the minimum sum of the array is 1.
Input : 3
arr[] = 2 4 6
Output : 6
Perform the following operations:-
subtract 2 from 4 then the array becomes 2 2 6
subtract 2 (at 2nd position) from 6 the array
becomes 2 2 4
subtract 2 (at 2nd position) from 4 the array
becomes 2 2 2
Now the sum of the array will be 6.
Approach:
After applying all the operations, all the values in the given array will be equal otherwise we can still choose two numbers A and B such that A > B and can reduce the sum further. The value of each element after applying all the operations will be equal to the gcd of the array i.e. ans.
Therefore, the minimum possible sum will be equal to n * ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int gcdofArray( int arr[], int n)
{
int ans = arr[0];
for ( int i = 1; i < n; i++)
ans = gcd(ans, arr[i]);
return ans;
}
int main()
{
int arr[] = { 2, 4, 6 }, n;
n = sizeof (arr) / sizeof (arr[0]);
cout << n * gcdofArray(arr, n)
<< endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int gcdofArray( int arr[], int n)
{
int ans = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
ans = gcd(ans, arr[i]);
return ans;
}
public static void main (String[] args)
{
int arr[] = { 2 , 4 , 6 }, n;
n = arr.length;
System.out.println( n * gcdofArray(arr, n)) ;
}
}
|
Python3
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def gcdofArray(arr, n):
ans = arr[ 0 ]
for i in range (n):
ans = gcd(ans, arr[i])
return ans
arr = [ 2 , 4 , 6 ]
n = len (arr)
print (n * gcdofArray(arr, n))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int gcdofArray( int []arr, int n)
{
int ans = arr[0];
for ( int i = 1; i < n; i++)
ans = gcd(ans, arr[i]);
return ans;
}
public static void Main ()
{
int []arr = {2, 4, 6};
int n;
n = arr.Length;
Console.WriteLine(n * gcdofArray(arr, n)) ;
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return gcd( $b , $a % $b );
}
function gcdofArray( $arr , $n )
{
$ans = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
$ans = gcd( $ans , $arr [ $i ]);
return $ans ;
}
$arr = array ( 2, 4, 6 );
$n = count ( $arr );
echo $n * gcdofArray( $arr , $n ), "\n" ;
?>
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function gcdofArray(arr, n)
{
let ans = arr[0];
for (let i = 1; i < n; i++)
ans = gcd(ans, arr[i]);
return ans;
}
let arr = [ 2, 4, 6 ], n;
n = arr.length;
document.write(n * gcdofArray(arr, n));
</script>
|
Time Complexity: O(N*logN), as we are using a loop to traverse N times and in each traversal, we are calling the gcd function which costs logN time. Where N is the number of elements in the array.
Auxiliary Space: O(logN), due to recursive stack space in gcd function.
Last Updated :
25 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...