Minimize Array sum by replacing an element with GCD
Last Updated :
24 Jan, 2023
Given an array A[] of the length N. The task is to find the minimum sum of the array (i.e. A1 + A2 + … + AN) by performing the following operation on the given array any number (0 or more) of times:
- Take any two indices i and j such that 0 ? i, j < N and swap either Ai or Aj with gcd(Ai, Aj).
Examples:
Input: A[] = {3, 3, 9}
Output: 9
Explanation: Choose i = 0 and j = 2 and replace A?3 with gcd(3, 9) = 3.
Now array A[]={3, 3, 3} and sum = 9 which is minimum possible sum of given array.
Input: A[] = {7, 14}
Output: 14
Approach 1:
The idea is to find the GCD of each pair of elements in the array and replace the larger element with the GCD. This will reduce the sum of the array as much as possible.
Algorithm:
- Calculate the GCD of each pair of elements in the array.
- Replace the larger element with the GCD.
- Repeat the above steps until all the elements in the array are equal.
- Calculate the sum of the array.
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int minSum( int arr[], int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
int g = gcd(arr[i], arr[j]);
arr[i] = g;
arr[j] = g;
}
}
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
int main()
{
int arr[] = { 3, 3, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minSum(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
public static int minSum( int arr[], int n)
{
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
int g = gcd(arr[i], arr[j]);
arr[i] = g;
arr[j] = g;
}
}
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += arr[i];
return sum;
}
public static void main(String[] args)
{
int arr[] = { 3 , 3 , 9 };
int n = arr.length;
System.out.println(minSum(arr, n));
}
}
|
Python3
def gcd(a: int , b: int ) - > int :
if a = = 0 :
return b
return gcd(b % a, a)
def min_sum(arr, n):
for i in range (n):
for j in range (i + 1 , n):
g = gcd(arr[i], arr[j])
arr[i] = g
arr[j] = g
s = 0
for i in range (n):
s + = arr[i]
return s
if __name__ = = "__main__" :
arr = [ 3 , 3 , 9 ]
n = len (arr)
print (min_sum(arr, n))
|
C#
using System;
public class Program {
public static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public static int minSum( int [] arr, int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
int g = gcd(arr[i], arr[j]);
arr[i] = g;
arr[j] = g;
}
}
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
static void Main( string [] args)
{
int [] arr = { 3, 3, 9 };
int n = arr.Length;
Console.WriteLine(minSum(arr, n));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function minSum( arr,n)
{
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
let g = gcd(arr[i], arr[j]);
arr[i] = g;
arr[j] = g;
}
}
let sum = 0;
for (let i = 0; i < n; i++)
sum += arr[i];
return sum;
}
let arr = [3, 3, 9];
let n = arr.length;
console.log(minSum(arr, n));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Approach 2: To solve the problem follow the below idea:
Let G = gcd(A1, A2, . . ., AN). The answer is simply N*G. because every element can be converted to be the same as G.
Follow the below steps to solve the problem:
- Calculate the GCD of all the array elements.
- Multiply the result by N to get the minimum sum of the array.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int p, int q) { return q == 0 ? p : gcd(q, p % q); }
int minSum( int A[], int N)
{
int min = INT_MAX;
for ( int i = 0; i < N - 1; i++) {
int b = gcd(A[i], A[i + 1]);
if (b < min)
min = b;
}
return min * N;
}
int main()
{
int A[] = { 3, 3, 9 };
int N = sizeof (A) / sizeof (A[0]);;
cout << minSum(A, N) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static int gcd( int p, int q)
{
if (q == 0 ) {
return p;
}
return gcd(q, p % q);
}
public static int minSum( int A[], int N)
{
int min = Integer.MAX_VALUE;
for ( int i = 0 ; i < N - 1 ; i++) {
int b = gcd(A[i], A[i + 1 ]);
if (b < min)
min = b;
}
return min * N;
}
public static void main(String[] args)
{
int A[] = { 3 , 3 , 9 };
int N = A.length;
System.out.println(minSum(A, N));
}
}
|
Python3
import sys
def gcd(a,b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def minSum(A, N) :
min = sys.maxsize
for i in range ( 0 , N - 1 , 1 ):
b = gcd(A[i], A[i + 1 ])
if (b < min ) :
min = b
return min * N
A = [ 3 , 3 , 9 ]
N = len (A)
print (minSum(A, N))
|
C#
using System;
class GFG
{
public static int gcd( int p, int q)
{
if (q == 0) {
return p;
}
return gcd(q, p % q);
}
public static int minSum( int [] A, int N)
{
int min = Int32.MaxValue;
for ( int i = 0; i < N - 1; i++) {
int b = gcd(A[i], A[i + 1]);
if (b < min)
min = b;
}
return min * N;
}
public static void Main( string [] args)
{
int [] A = { 3, 3, 9 };
int N = A.Length;
Console.WriteLine(minSum(A, N));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function minSum(A,N)
{
let min = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < N - 1; i++) {
let b = gcd(A[i], A[i + 1]);
if (b < min)
min = b;
}
return min * N;
}
let A = [ 3, 3, 9 ];
let N = A.length;
console.log(minSum(A, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...