Hello,
I think this is a pretty simple question, and the solution that i have come up with is - store the weights in an array, sort them in ascending, pick the first K for the kid; add them; the remaining n-k go to the dad, add them- find the diff and thats the answer. Can someone please point out where am i going wrong?
here is the code for your reference -
/* package codechef; // don’t place package name! */
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int numTestCases;
Scanner sc = new Scanner(System.in);
numTestCases = sc.nextInt();
while(numTestCases>0)
{
int n,k;
n= sc.nextInt();
k = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
Arrays.sort(a);
int sumk=0;
int sumDad=0;
for(int i=0;i<k;i++){
sumk= sumk + a[i];
}
for(int i =k; i<n;i++){
sumDad = sumDad + a[i];
}
int maxDiff=0;
maxDiff = sumDad-sumk;
System.out.println(maxDiff);
numTestCases--;
}
}
}