Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .

0 like 0 dislike
3,047 views

in Online Assessments by Expert (144,420 points)

1 Answer

0 like 0 dislike
import java.util.*;
public class Main
{
    public static void main(String[] args) {
        int arr[] = { 4, 6, 8 };
        System.out.println(solve(arr));
    }
    
    private static int solve(int[] arr){
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        
        for(int a : arr)
            pq.add(a);
        int cost = 0;
        while(pq.size() >= 2){
            int x = pq.remove();
            int y = pq.remove();
            cost += x+y;
            pq.add(x+y);
        }
        
        return cost;
    }
}
by (150 points)
...