YoAllTheseStacks
YoAllTheseStacks

Reputation: 13

Codechef returns "WA" (Wrong Answer) for the following code to find max sum of two numbers from a group of three

Statement: Chef has three spells. Their powers are AA, BB, and CC respectively. Initially, Chef has 00 hit points, and if he uses a spell with power PP, then his number of hit points increases by PP.Before going to sleep, Chef wants to use exactly two spells out of these three. Find the maximum number of hit points Chef can have after using the spells.

The following code is my attempt at a solution:

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();
        while (T-->0)
        {
            int A=sc.nextInt();
            int B=sc.nextInt();
            int C=sc.nextInt();
            int min=A;
            if (B<A && B<C)
                min=B;
            else if (C<A && C<B)
                min=C;
            min=A+B+C-min;
            System.out.println(min);
        }
    }
}

I'm unable to understand which test cases this program might fail for (if any), as it's returning the correct output for all constraints I add manually. The website doesn't show the failed cases, which is of huge inconvenience.

Edit: I used the "min" variable to collect the minimum value of the three numbers. My logic is to subtract this "min" value from the sum of all three, which in theory should return the sum of the other two numbers.

Upvotes: 0

Views: 348

Answers (1)

Dennis LLopis
Dennis LLopis

Reputation: 315

I took a crack at the practice problem on CodeCheff and looked over your solution as well. I found it easier by creating the spell combinations and assigning the first as the max. Then I compared the second and third combinations to the current max. You created a 'min' variable, but never used it when comparing and I think that's where you went wrong.

public static void main (String[] args) throws java.lang.Exception {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt(); // Number of testcases
        
        for(int i = 0; i < T; i++) {
            /* Populate each spell variable before comparison */
            int A = scanner.nextInt();
            int B = scanner.nextInt();
            int C = scanner.nextInt();
            
            /* Create the spell combinations for comparison */
            int combo_1 = A + B;
            int combo_2 = A + C;
            int combo_3 = B + C;
            
            /* Assign the first spell combo as max
             * Then compare combo_2 & combo_3 to max
             */
            int max = combo_1;
            if(combo_2 > max) max = combo_2;
            if(combo_3 > max) max = combo_3;
            
            /* Print the max of the three */
            System.out.println(max); 
        }
        scanner.close();
}

After testing with some custom input and your code, it seems like the issue is with duplicate numbers. Adding the '=' allows your code to pass. The little things can cause issues :). Always good to try and make some custom testcases to weed out the problem. Nice approach with the min variable; I learned something new.

public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
        int T = sc.nextInt();
        while (T-- > 0)
        {
            int A=sc.nextInt();
            int B=sc.nextInt();
            int C=sc.nextInt();
            int min = A;
            
            if (B <= A && B <= C)
                min = B;
            else if (C <= A && C <= B)
                min = C;
            min = A + B + C - min;
            System.out.println(min);
        }
    }

Upvotes: 1

Related Questions