Connect Ropes

Given a number of ropes say ‘N’ and an array of integers of size ‘N’ containing the length of ropes. Your task is to connect the ropes into one. The cost to connect two ropes is equal to the sum of their lengths. Find the minimum cost for connecting all the ropes.

Input Format:
The first line of input contains a single integer T, representing the number of test cases or queries to be run. 

Then the T test cases follow.

The first line of each test case contains the number of ropes.

The second line of each test case contains space-separated integers containing the length of ropes. 
Output Format:
For each test case, print the minimum cost to connect all the ropes. Each value is separated by a single space.

The output of each test case is printed in a separate line.

Note :

You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 10^4
1 <= length[i] <= 10^4

Where 'length[i]' is the length of the 'i-th' rope.

Time Limit: 1 sec
MOHAMMED MUJAMMIL S IT
1y

import java.util.Arrays;

import java.util.Scanner;

class Ropes{

static int mincost(int[] arr,int N){

int res1=0,res2=0,A=0;

A=N-1;

for (int i=0 ; i<N;i++){

if(i==0){

res1+=arr[i]*arr[i+1];

}

else{

res2+=arr[i]*(...read more

Help your peers!
Add answer anonymously...
Mr Cooper Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter