Given an array of n integers. We need to reduce size of array to one. We are allowed to select a pair of integers and remove the larger one of these two. This decreases the array size by 1. Cost of this operation is equal to value of smaller one. Find out minimum sum of costs of operations needed to convert the array into a single element.

**Examples:**

Input: 4 3 2 Output: 4 Explanation: Choose (4, 2) so 4 is removed, new array = {2, 3}. Now choose (2, 3) so 3 is removed. So total cost = 2 + 2 = 4 Input: 3 4 Output: 3 Explanation: choose 3, 4, so cost is 3.

The idea is to always pick minimum value as part of the pair and remove larger value. This minimizes cost of reducing array to size 1.

Below is the implementation of the above approach:

## CPP

`// CPP program to find minimum cost to ` `// reduce array size to 1, ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to calculate the minimum cost ` `int` `cost(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Minimum cost is n-1 multiplied with ` ` ` `// minimum element. ` ` ` `return` `(n - 1) * (*min_element(a, a + n)); ` `} ` ` ` `// driver program to test the above function. ` `int` `main() ` `{ ` ` ` `int` `a[] = { 4, 3, 2 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << cost(a, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum cost ` `// to reduce array size to 1, ` `import` `java.lang.*; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// function to calculate the ` ` ` `// minimum cost ` ` ` `static` `int` `cost(` `int` `[]a, ` `int` `n) ` ` ` `{ ` ` ` `int` `min = a[` `0` `]; ` ` ` ` ` `// find the minimum using ` ` ` `// for loop ` ` ` `for` `(` `int` `i = ` `1` `; i< a.length; i++) ` ` ` `{ ` ` ` `if` `(a[i] < min) ` ` ` `min = a[i]; ` ` ` `} ` ` ` ` ` `// Minimum cost is n-1 multiplied ` ` ` `// with minimum element. ` ` ` `return` `(n - ` `1` `) * min; ` ` ` `} ` ` ` ` ` `// driver program to test the ` ` ` `// above function. ` ` ` `static` `public` `void` `main (String[] args) ` ` ` `{ ` ` ` ` ` `int` `[]a = { ` `4` `, ` `3` `, ` `2` `}; ` ` ` `int` `n = a.length; ` ` ` ` ` `System.out.println(cost(a, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by parashar. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find minimum ` `# cost to reduce array size to 1 ` ` ` `# function to calculate the ` `# minimum cost ` `def` `cost(a, n): ` ` ` ` ` `# Minimum cost is n-1 multiplied ` ` ` `# with minimum element. ` ` ` `return` `( (n ` `-` `1` `) ` `*` `min` `(a) ) ` ` ` ` ` `# driver code ` `a ` `=` `[ ` `4` `, ` `3` `, ` `2` `] ` `n ` `=` `len` `(a) ` `print` `(cost(a, n)) ` ` ` `# This code is contributed by ` `# Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum cost to ` `// reduce array size to 1, ` `using` `System; ` `using` `System.Linq; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// function to calculate the minimum cost ` ` ` `static` `int` `cost(` `int` `[]a, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// Minimum cost is n-1 multiplied with ` ` ` `// minimum element. ` ` ` `return` `(n - 1) * a.Min(); ` ` ` `} ` ` ` ` ` `// driver program to test the above function. ` ` ` `static` `public` `void` `Main (){ ` ` ` ` ` `int` `[]a = { 4, 3, 2 }; ` ` ` `int` `n = a.Length; ` ` ` ` ` `Console.WriteLine(cost(a, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum cost to ` `// reduce array size to 1, ` ` ` `// function to calculate ` `// the minimum cost ` `function` `cost(` `$a` `, ` `$n` `) ` `{ ` ` ` ` ` `// Minimum cost is n-1 ` ` ` `// multiplied with ` ` ` `// minimum element. ` ` ` `return` `(` `$n` `- 1) * (min(` `$a` `)); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$a` `= ` `array` `(4, 3, 2); ` ` ` `$n` `= ` `count` `(` `$a` `); ` ` ` `echo` `cost(` `$a` `, ` `$n` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

4

**Time Complexity** : O(n)

This article is contributed by **Striver**. 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.

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 cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Make array elements equal with minimum cost
- Minimum cost to make all array elements equal
- Make all array elements equal with minimum cost
- Minimum cost to make an Array a permutation of first N natural numbers
- Minimum Cost to make all array elements equal using given operations
- 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
- Make two sets disjoint by removing minimum elements
- Minimum cost required to convert all Subarrays of size K to a single element
- Count of ways to make Array sum even by removing only one element
- Steps to make array empty by removing maximum and its right side
- Minimize the cost to make all the adjacent elements distinct in an Array
- Minimum number of pairs required to make two strings same
- Maximum of minimum difference of all pairs from subsequences of given size
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Find minimum adjustment cost of an array
- Minimum cost to equal all elements of array using two operation
- Minimum cost to connect weighted nodes represented as array

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.