• + 1 comment

    This problem is bogus and whoever posted is also bogus. The reason,the method is requiring int, and how to return "Bon Appetite" when the value is 0. Since S.o.u.t is used to print in the main, we need to return the string value. And how to do it. Or another option, if you try to modify inside sout in main, it is also giving error since i have modified. How to fix?

    This is my solution:

    static int bonAppetit(int n, int k, int b, int[] ar) {
            // Complete this function
            int totalcost = 0;
            for (int i = 0; i < ar.length; i++) {
                totalcost += ar[i];
            }
            totalcost = totalcost - ar[k];
            if (b - totalcost / 2  == 0) {
                System.out.println("Bon Appetit");
            }
            return b - totalcost / 2;
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int k = in.nextInt();
            int[] ar = new int[n];
            for (int ar_i = 0; ar_i < n; ar_i++) {
                ar[ar_i] = in.nextInt();
            }
            int b = in.nextInt();
            int result = bonAppetit(n, k, b, ar);
            System.out.println(result);
        }
    

    it return "Bon Appetit" and 0 which is incorrect as for problem statement when he does not have to refund.

    How to fix it!