# Minimum sum by choosing minimum of pairs from array

Given an array A[] of n-elements. We need to select two adjacent elements and delete the larger of them and store smaller of them to another array say B[]. We need to perform this operation till array A[] contains only single element. Finally, we have to construct the array B[] in such a way that total sum of its element is minimum. Print the total sum of array B[].

Examples:

Input : A[] = {3, 4} Output : 3 Input : A[] = {2, 4, 1, 3} Output : 3

There is an easy trick to solve this question and that is always choose the smallest element of array A[] and its adjacent, delete the adjacent element and copy smallest one to array B[]. Again for next iteration we have same smallest element and any random adjacent element which is to be deleted. After n-1 operations all of elements of A[] got deleted except the smallest one and at the same time array B[] contains “n-1” elements and all are equal to smallest element of array A[].

Thus total sum of array B[] is equal to **smallest * (n-1)**.

## C++

`// CPP program to minimize the cost ` `// of array minimization ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns minimum possible sum in ` `// array B[] ` `int` `minSum(` `int` `A[], ` `int` `n) ` `{ ` ` ` `int` `min_val = *min_element(A, A+n); ` ` ` `return` `(min_val * (n-1)); ` `} ` ` ` `// driver function ` `int` `main() ` `{ ` ` ` `int` `A[] = { 3, 6, 2, 8, 7, 5}; ` ` ` `int` `n = ` `sizeof` `(A)/ ` `sizeof` `(A[0]); ` ` ` `cout << minSum(A, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to minimize the ` `// cost of array minimization ` `import` `java.util.Arrays; ` ` ` `public` `class` `GFG { ` ` ` `// Returns minimum possible ` `// sum in array B[] ` ` ` `static` `int` `minSum(` `int` `[] A, ` `int` `n) { ` ` ` `int` `min_val = Arrays.stream(A).min().getAsInt(); ` ` ` `return` `(min_val * (n - ` `1` `)); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `main(String[] args) { ` ` ` `int` `[] A = {` `3` `, ` `6` `, ` `2` `, ` `8` `, ` `7` `, ` `5` `}; ` ` ` `int` `n = A.length; ` ` ` `System.out.println((minSum(A, n))); ` ` ` ` ` `} ` `} ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python

`# Python code for minimum cost of ` `# array minimization ` ` ` `# Function defintion for minCost ` `def` `minSum(A): ` ` ` ` ` `# find the minimum element of A[] ` ` ` `min_val ` `=` `min` `(A); ` ` ` ` ` `# return the answer ` ` ` `return` `min_val ` `*` `(` `len` `(A)` `-` `1` `) ` ` ` `# driver code ` `A ` `=` `[` `7` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `, ` `6` `] ` `print` `(minSum(A)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to minimize the ` `// cost of array minimization ` `using` `System; ` `using` `System.Linq; ` ` ` `public` `class` `GFG ` `{ ` ` ` `// Returns minimum possible ` `// sum in array B[] ` `static` `int` `minSum(` `int` `[]A, ` `int` `n) ` `{ ` ` ` `int` `min_val = A.Min(); ` ` ` `return` `(min_val * (n - 1)); ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main() ` ` ` `{ ` ` ` `int` `[]A = {3, 6, 2, 8, 7, 5}; ` ` ` `int` `n = A.Length; ` ` ` `Console.WriteLine(minSum(A, n)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to minimize the ` `// cost of array minimization ` ` ` `// Returns minimum possible ` `// sum in array B[] ` `function` `minSum(` `$A` `, ` `$n` `) ` `{ ` ` ` `$min_val` `= min(` `$A` `); ` ` ` `return` `(` `$min_val` `* (` `$n` `- 1)); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$A` `= ` `array` `(3, 6, 2, 8, 7, 5); ` ` ` `$n` `= ` `count` `(` `$A` `); ` ` ` `echo` `minSum(` `$A` `, ` `$n` `); ` ` ` `// This code is contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

10

**Time Complexity :** O(n) in finding the smallest element of the array.

This article is contributed by **Shivam Pradhan (anuj_charm)**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Minimum cost of choosing 3 increasing elements in an array of size N
- Find the maximum cost of an array of pairs choosing at most K pairs
- Find k ordered pairs in array with minimum difference d
- Minimum cost to make array size 1 by removing larger of pairs
- Maximum and minimum of an array using minimum number of comparisons
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Minimum and Maximum sum of absolute differences of pairs
- Minimum sum of absolute difference of pairs of two arrays
- Minimum number of pairs required to make two strings same
- Minimum number of swaps required for arranging pairs adjacent to each other
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Find minimum value to assign all array elements so that array product becomes greater
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Insert minimum number in array so that sum of array becomes prime
- Minimum Possible value of |ai + aj - k| for given array and k.