Minimum sum of the elements of an array after subtracting smaller elements from larger
Last Updated :
08 Jul, 2022
Given an array arr, the task is to find the minimum sum of the elements of the array after applying the following operation:
For any pair from the array, if a[i] > a[j] then a[i] = a[i] – a[j].
Examples:
Input: arr[] = {1, 2, 3}
Output: 3
modified array will be {1, 1, 1}
Input: a = {2, 4, 6}
Output: 6
modified array will be {2, 2, 2}
Approach: Observe here that after each operation, the GCD of all the elements will remain the same. So, in the end, every element will be equal to the gcd of all the elements of the array after applying the given operation.
So, the final answer will be (n * gcd).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinSum( int a[], int n)
{
int gcd = a[0];
for ( int i = 1; i < n; i++)
gcd = __gcd(a[i], gcd);
return n * gcd;
}
int main()
{
int a[] = { 20, 14, 6, 8, 15 };
int n = sizeof (a) / sizeof (a[0]);
cout << MinSum(a, n);
return 0;
}
|
Java
import java.io.*;
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 MinSum( int []a, int n)
{
int gcd = a[ 0 ];
for ( int i = 1 ; i < n; i++)
gcd = __gcd(a[i], gcd);
return n * gcd;
}
public static void main (String[] args) {
int a[] = { 20 , 14 , 6 , 8 , 15 };
int n = a.length;
System.out.println(MinSum(a, n));
}
}
|
Python3
import math
def MinSum(a, n):
gcd = a[ 0 ]
for i in range ( 1 , n):
gcd = math.gcd(a[i], gcd)
return n * gcd
if __name__ = = "__main__" :
a = [ 20 , 14 , 6 , 8 , 15 ]
n = len (a)
print (MinSum(a, n))
|
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 MinSum( int []a, int n)
{
int gcd = a[0];
for ( int i = 1; i < n; i++)
gcd = __gcd(a[i], gcd);
return n * gcd;
}
static void Main()
{
int []a = { 20, 14, 6, 8, 15 };
int n = a.Length;
Console.WriteLine(MinSum(a, n));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return gcd( $b , $a % $b );
}
function MinSum( $a , $n )
{
$gcdd = $a [0];
for ( $i = 1; $i < $n ; $i ++)
$gcdd = gcd( $a [ $i ], $gcdd );
return $n * $gcdd ;
}
$a = array ( 20, 14, 6, 8, 15 );
$n = count ( $a );
echo MinSum( $a , $n );
?>
|
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 MinSum(a, n)
{
var gcd = a[0];
for ( var i = 1; i < n; i++)
gcd = __gcd(a[i], gcd);
return n * gcd;
}
var a = [ 20, 14, 6, 8, 15 ];
var n = a.length;
document.write( MinSum(a, n));
</script>
|
Time Complexity: O(n * log(min(a, b))), where a and b are two parameters of the gcd.
Auxiliary Space: O(log(min(a, b)))
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...