Anna and Brian are sharing a meal at a restuarant and they agree to split the bill equally. Brian wants to order something that Anna is allergic to though, and they agree that Anna won't pay for that item. Brian gets the check and calculates Anna's portion. You must determine if his calculation is correct.
For example, assume the bill has the following prices: . Anna declines to eat item  which costs . If Brian calculates the bill correctly, Anna will pay . If he includes the cost of , he will calculate . In the second case, he should refund  to Anna.

Question from hackerrank.


Solution in C# - 


class Solution {

    static void bonAppetit(List<int> bill, int k, int b) {

        int sum = 0;
        for(int i=0; i<bill.Count; i++)
        sum+=bill[i];
        sum -= bill[k];
        sum /= 2;
        if(sum == b)
        Console.WriteLine("Bon Appetit");
        else
        Console.WriteLine(b-sum);

    }

    static void Main(string[] args) {
        string[] nk = Console.ReadLine().TrimEnd().Split(' ');

        int n = Convert.ToInt32(nk[0]);

        int k = Convert.ToInt32(nk[1]);

        List<int> bill = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(billTemp => Convert.ToInt32(billTemp)).ToList();

        int b = Convert.ToInt32(Console.ReadLine().Trim());

        bonAppetit(bill, k, b);
    }

}


Post a Comment

Previous Post Next Post