Minimize Array sum by replacing an element with GCD
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
// 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)); |
9
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
// 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. |
9
Time Complexity: O(N)
Auxiliary Space: O(1)