You are not logged in. Please login at www.codechef.com to post your questions!

×

Getting WA - unable to understand why-MAXDIFF- Mock Exam-1- CCDSAP

https://www.codechef.com/FLMOCK01/problems/MAXDIFF

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--;
    }
}

}

asked 02 Jan, 10:16

digicode's gravatar image

2★digicode
31
accept rate: 0%


5 4

8 4 5 2 10

Your output is 9 but the correct output is 25. Hope you can find and fix the error by yourself now.

link

answered 02 Jan, 10:41

ankush_953's gravatar image

4★ankush_953
657
accept rate: 8%

yes, realised my mistake, thanks a lot :)

(02 Jan, 14:20) digicode2★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×847

question asked: 02 Jan, 10:16

question was seen: 95 times

last updated: 02 Jan, 14:20