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