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;
// Function to calculate GCD of two numbers
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to calculate the minimum sum of the array
int minSum(int arr[], int n)
{
// Iterate over all pairs of elements in the array
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Calculate GCD of arr[i] and arr[j]
int g = gcd(arr[i], arr[j]);
// Replace arr[i] and arr[j] with GCD
arr[i] = g;
arr[j] = g;
}
}
// Calculate sum of the array
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
// Driver code
int main()
{
int arr[] = { 3, 3, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minSum(arr, n);
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
public class GFG {
// Function to find gcd of two numbers
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find the minimum
// sum of array
public static int minSum(int arr[], int n)
{
// Iterate over all pairs of elements in the array
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Calculate GCD of arr[i] and arr[j]
int g = gcd(arr[i], arr[j]);
// Replace arr[i] and arr[j] with GCD
arr[i] = g;
arr[j] = g;
}
}
// Calculate sum of the array
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 3, 3, 9 };
int n = arr.length;
// Function call
System.out.println(minSum(arr, n));
}
}
Python3
def gcd(a: int, b: int) -> int:
if a == 0:
return b
return gcd(b % a, a)
# Function to calculate the minimum sum of the array
def min_sum(arr, n):
# Iterate over all pairs of elements in the array
for i in range(n):
for j in range(i + 1, n):
# Calculate GCD of arr[i] and arr[j]
g = gcd(arr[i], arr[j])
# Replace arr[i] and arr[j] with GCD
arr[i] = g
arr[j] = g
# Calculate sum of the array
s = 0
for i in range(n):
s += arr[i]
return s
# Driver code
if __name__ == "__main__":
arr = [3, 3, 9]
n = len(arr)
print(min_sum(arr, n))
# This code is contributed by divya_p123.
C#
using System;
public class Program {
// Function to find gcd of two numbers
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find the minimum sum of array
public static int minSum(int[] arr, int n)
{
// Iterate over all pairs of elements in the array
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Calculate GCD of arr[i] and arr[j]
int g = gcd(arr[i], arr[j]);
// Replace arr[i] and arr[j] with GCD
arr[i] = g;
arr[j] = g;
}
}
// Calculate sum of the array
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;
// Function call
Console.WriteLine(minSum(arr, n));
}
}
JavaScript
<script>
// JavaScript code to implement the approach
// Function to find gcd of two numbers
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find the minimum
// sum of array
function minSum( arr,n)
{
// Iterate over all pairs of elements in the array
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
// Calculate GCD of arr[i] and arr[j]
let g = gcd(arr[i], arr[j]);
// Replace arr[i] and arr[j] with GCD
arr[i] = g;
arr[j] = g;
}
}
// Calculate sum of the array
let sum = 0;
for (let i = 0; i < n; i++)
sum += arr[i];
return sum;
}
// Driver code
let arr = [3, 3, 9];
let n = arr.length;
// Function call
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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find gcd of two numbers
int gcd(int p, int q) { return q == 0 ? p : gcd(q, p % q); }
// Function to find the minimum
// sum of array
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;
}
// Driver Code
int main()
{
int A[] = { 3, 3, 9 };
int N = sizeof(A) / sizeof(A[0]);;
// Function call
cout << minSum(A, N) << endl;
return 0;
}
// This code is contributed by aarohirai2616.
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
public class GFG {
// Function to find gcd of two numbers
public static int gcd(int p, int q)
{
if (q == 0) {
return p;
}
return gcd(q, p % q);
}
// Function to find the minimum
// sum of array
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;
}
// Driver code
public static void main(String[] args)
{
int A[] = { 3, 3, 9 };
int N = A.length;
// Function call
System.out.println(minSum(A, N));
}
}
Python3
# Python code for the above approach
import sys
# Function to find gcd of two numbers
def gcd(a,b):
# Everything divides 0
if (b == 0):
return a
return gcd(b, a%b)
# Function to find the minimum
# sum of array
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
# Driver Code
A = [ 3, 3, 9 ]
N = len(A)
# Function call
print(minSum(A, N))
# This code is contributed by code_hunt.
C#
// C# code to implement the approach
using System;
class GFG
{
// Function to find gcd of two numbers
public static int gcd(int p, int q)
{
if (q == 0) {
return p;
}
return gcd(q, p % q);
}
// Function to find the minimum
// sum of array
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;
}
// Driver Code
public static void Main(string[] args)
{
int[] A = { 3, 3, 9 };
int N = A.Length;
// Function call
Console.WriteLine(minSum(A, N));
}
}
// This code is contributed by sanjoy_62.
JavaScript
<script>
// Javascript code to implement the approach
// Function to find gcd of two numbers
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to find the minimum
// sum of array
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;
}
// Driver code
let A = [ 3, 3, 9 ];
let N = A.length;
// Function call
console.log(minSum(A, N));
// This code is contributed by aarohirai2616.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum gcd operations to make all array elements one Given an array A[] of size N. You can replace any number in the array with the gcd of that element with any of its adjacent elements. Find the minimum number of such operation to make the element of whole array equal to 1. If its not possible print -1. Examples: Input : A[] = {4, 8, 9} Output : 3 Ex
8 min read
Remove an element to maximize the GCD of the given array Given an array arr[] of length N ⥠2. The task is to remove an element from the given array such that the GCD of the array after removing it is maximized. Examples: Input: arr[] = {12, 15, 18} Output: 6 Remove 12: GCD(15, 18) = 3 Remove 15: GCD(12, 18) = 6 Remove 18: GCD(12, 15) = 3 Input: arr[] = {
13 min read
Minimize distinct elements in Array after replacing arr[i] with arr[i] modulo X Given an array arr[] of size N, the task is to minimize the number of distinct elements in the array after performing the following operation once: Choose an integer X and replace each element of the array with the remainder when arr[i] is divided by X (i.e., arr[i]%X). Examples: Input: N = 5, arr[]
6 min read
Minimize Array length by repeatedly replacing co-prime pairs with 1 Given an array arr[] consisting of N elements, the task is to minimize the array length by replacing any two coprime array elements with 1.Examples: Input: arr[] = {2, 3, 5} Output: 1 Explanation: Replace {2, 3} with 1 modifies the array to {1, 5}. Replace {1, 5} with 1 modifies the array to {1}.Inp
6 min read
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
5 min read