# Minimum sum of the elements of an array after subtracting smaller elements from larger

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}

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

 `// CPP program to Find the minimum sum ` `// of given array after applying given operation. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to Find the minimum sum ` `// of given array after applying given operation. ` `int` `MinSum(``int` `a[], ``int` `n) ` `{ ` `    ``// to store final gcd value ` `    ``int` `gcd = a; ` ` `  `    ``// get gcd of the whole array ` `    ``for` `(``int` `i = 1; i < n; i++) ` `        ``gcd = __gcd(a[i], gcd); ` ` `  `    ``return` `n * gcd; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `a[] = { 20, 14, 6, 8, 15 }; ` ` `  `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` ` `  `    ``// function call ` `    ``cout << MinSum(a, n); ` ` `  `    ``return` `0; ` `} `

 `// Java program to Find the minimum sum ` `// of given array after applying given operation. ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `    `  `// Recursive function to return gcd of a and b  ` `static` `int` `__gcd(``int` `a, ``int` `b)  ` `{  ` `    ``// Everything divides 0   ` `    ``if` `(a == ``0``)  ` `       ``return` `b;  ` `    ``if` `(b == ``0``)  ` `       ``return` `a;  ` `    `  `    ``// base case  ` `    ``if` `(a == b)  ` `        ``return` `a;  ` `    `  `    ``// a is greater  ` `    ``if` `(a > b)  ` `        ``return` `__gcd(a-b, b);  ` `    ``return` `__gcd(a, b-a);  ` `}  ` `// Function to Find the minimum sum ` `// of given array after applying given operation. ` `static` `int` `MinSum(``int` `[]a, ``int` `n) ` `{ ` `    ``// to store final gcd value ` `    ``int` `gcd = a[``0``]; ` ` `  `    ``// get gcd of the whole array ` `    ``for` `(``int` `i = ``1``; i < n; i++) ` `        ``gcd = __gcd(a[i], gcd); ` ` `  `    ``return` `n * gcd; ` `} ` ` `  `// Driver code ` ` `  `    ``public` `static` `void` `main (String[] args) { ` `            ``int` `a[] = { ``20``, ``14``, ``6``, ``8``, ``15` `}; ` ` `  `    ``int` `n = a.length; ` ` `  `    ``// function call ` `    ``System.out.println(MinSum(a, n)); ` `    ``} ` `} ` `// This code is contributed by anuj_67.. `

 `# Python3 program to Find the minimum  ` `# sum of given array after applying  ` `# given operation. ` `import` `math ` ` `  `# Function to Find the minimum sum ` `# of given array after applying  ` `# given operation. ` `def` `MinSum(a, n): ` ` `  `    ``# to store final gcd value ` `    ``gcd ``=` `a[``0``] ` ` `  `    ``# get gcd of the whole array ` `    ``for` `i ``in` `range``(``1``, n): ` `        ``gcd ``=` `math.gcd(a[i], gcd) ` ` `  `    ``return` `n ``*` `gcd ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``a ``=` `[``20``, ``14``, ``6``, ``8``, ``15` `] ` ` `  `    ``n ``=` `len``(a) ` ` `  `    ``# function call ` `    ``print``(MinSum(a, n)) ` ` `  `# This code is contributed by ita_c `

 `// C# program to Find the minimum sum ` `// of given array after applying given operation. ` ` `  `using` `System; ` `class` `GFG { ` `    `  `    ``// Recursive function to return gcd of a and b  ` `    ``static` `int` `__gcd(``int` `a, ``int` `b)  ` `    ``{  ` `        ``// Everything divides 0   ` `        ``if` `(a == 0)  ` `           ``return` `b;  ` `        ``if` `(b == 0)  ` `           ``return` `a;  ` `        `  `        ``// base case  ` `        ``if` `(a == b)  ` `            ``return` `a;  ` `        `  `        ``// a is greater  ` `        ``if` `(a > b)  ` `            ``return` `__gcd(a-b, b);  ` `        ``return` `__gcd(a, b-a);  ` `    ``}  ` `     `  `    ``// Function to Find the minimum sum ` `    ``// of given array after applying given operation. ` `    ``static` `int` `MinSum(``int` `[]a, ``int` `n) ` `    ``{ ` `        ``// to store final gcd value ` `        ``int` `gcd = a; ` `     `  `        ``// get gcd of the whole array ` `        ``for` `(``int` `i = 1; i < n; i++) ` `            ``gcd = __gcd(a[i], gcd); ` `     `  `        ``return` `n * gcd; ` `    ``} ` `     `  `     `  `    ``// Driver Program to test above function ` `    ``static` `void` `Main() ` `    ``{ ` `        ``int` `[]a = { 20, 14, 6, 8, 15 }; ` `        ``int` `n = a.Length; ` `        ``Console.WriteLine(MinSum(a, n)); ` `    ``} ` `     `  `    ``// This code is contributed by Ryuga. ` `} `

 ` `

Output:
```5
```

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :