Given an array of n elements perform, we need to minimize array sum. We are allowed to perform 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. Value of each element after applying all the operation will be equal to gcd of the array i.e. ans.

Therefore, minimum possible sum will be equal to n * ans.

Below is the implementation of above approach:

## C++

`// CPP program to find the minimum ` `// sum of the array. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// returns gcd of two numbers ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `gcd(b, a % b); ` `} ` ` ` `// returns the gcd of the array. ` `int` `gcdofArray(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `ans = arr[0]; ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `ans = gcd(ans, arr[i]); ` ` ` `return` `ans; ` `} ` ` ` `// Driver Function ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 4, 6 }, n; ` ` ` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << n * gcdofArray(arr, n) ` ` ` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program to find the minimum ` `// sum of the array. ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// returns gcd of two numbers ` ` ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` `return` `gcd(b, a % b); ` ` ` `} ` ` ` ` ` `// returns the gcd of the array. ` ` ` `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; ` ` ` `} ` ` ` ` ` `// Driver Function ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `4` `, ` `6` `}, n; ` ` ` `n = arr.length; ` ` ` `System.out.println( n * gcdofArray(arr, n)) ; ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find the minimum ` `# sum of the array. ` ` ` `# returns gcd of two numbers ` `def` `gcd(a, b): ` ` ` `if` `b ` `=` `=` `0` `: ` ` ` `return` `a ` ` ` `return` `gcd(b, a ` `%` `b) ` ` ` `# returns the gcd of the array. ` `def` `gcdofArray(arr, n): ` ` ` `ans ` `=` `arr[` `0` `] ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `ans ` `=` `gcd(ans, arr[i]) ` ` ` `return` `ans ` ` ` `# Driver Code ` `arr ` `=` `[ ` `2` `, ` `4` `, ` `6` `] ` `n ` `=` `len` `(arr) ` `print` `(n ` `*` `gcdofArray(arr, n)) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the minimum ` `// sum of the array. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// returns gcd of two numbers ` ` ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `gcd(b, a % b); ` ` ` `} ` ` ` ` ` `// returns the gcd of the array. ` ` ` `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; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `[]arr = {2, 4, 6}; ` ` ` `int` `n; ` ` ` `n = arr.Length; ` ` ` `Console.WriteLine(n * gcdofArray(arr, n)) ; ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the minimum ` `// sum of the array. ` ` ` `// returns gcd of two numbers ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$b` `== 0) ` ` ` `return` `$a` `; ` ` ` `return` `gcd(` `$b` `, ` `$a` `% ` `$b` `); ` `} ` ` ` `// returns the gcd of the array. ` `function` `gcdofArray(` `$arr` `, ` `$n` `) ` `{ ` ` ` `$ans` `= ` `$arr` `[0]; ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `$ans` `= gcd(` `$ans` `, ` `$arr` `[` `$i` `]); ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `( 2, 4, 6 ); ` `$n` `= ` `count` `(` `$arr` `); ` `echo` `$n` `* gcdofArray(` `$arr` `, ` `$n` `), ` `"\n"` `; ` ` ` `// This code is contributed by Sach ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

6

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Maximum difference between two elements such that larger element appears after the smaller number
- Minimizing array sum by applying XOR operation on all elements of the array
- Delete array elements which are smaller than next or become smaller
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
- Find elements larger than half of the elements in an array
- Make all elements of an Array equal by adding or subtracting at most K
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Make max elements in B[] equal to that of A[] by adding/subtracting integers in range [0, K]
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Count of array elements which is smaller than both its adjacent elements
- Minimum cost to make array size 1 by removing larger of pairs
- Move all zeros to start and ones to end in an Array of random integers
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Count of smaller or equal elements in sorted array
- Count of smaller elements on right side of each element in an Array using Merge sort
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Queries to answer the number of ones and zero to the left of given index

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.