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


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


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

/ 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(;
    numTestCases = sc.nextInt();
       int n,k;
       n= sc.nextInt();
       k = sc.nextInt();
       int a[] = new int[n];
       for(int i=0;i<n;i++){
       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;


asked 02 Jan, 10:16

digicode's gravatar image

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.


answered 02 Jan, 10:41

ankush_953's gravatar image

accept rate: 8%

yes, realised my mistake, thanks a lot :)

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

Follow this question

By Email:

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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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:


question asked: 02 Jan, 10:16

question was seen: 95 times

last updated: 02 Jan, 14:20