# Bon Appétit Hackerrank problem solution

##### Bon Appétit Hackerrank

Anna and Brian order items at a restaurant, but Anna declines to eat any of the kth item(where 0<=k<=n) due to an allergy. When the check comes, they decide to split the cost of all the items they shared; however, Brian may have forgotten that they didn't split the kth item and accidentally charged Anna for it.

You are given n, k, the cost of each of the n items, and the total amount of money that Brian charged Anna for her portion of the bill. If the bill is fairly split, print Bon Appetit; otherwise, print the amount of money that Brian must refund to Anna.

Sample Input 0

4 1
3 10 2 9
12
Sample Output 0

5
Explanation 0
Anna didn’t eat item c[1]=10, but she shared the rest of the items with Brian. The total cost of the shared items is 3+2+9=14 and, split in half, the cost per person is bactual=7. Brian charged her bcharged=12 for her portion of the bill, which is more than the 7 dollars worth of food that she actually shared with him. Thus, we print the amount Anna was overcharged, bcharged-bactual=12-7=5, on a new line.

Sample Input 1

4 1
3 10 2 9
7
Sample Output 1

Bon Appetit
Explanation 1
Anna didn’t eat item c[1]=10, but she shared the rest of the items with Brian. The total cost of the shared items is 3+2+9=14 and, split in half, the cost per person is bactual=7. Because this matches the amount, bcharged=7, that Brian charged Anna for her portion of the bill, we print Bon Appetit on a new line.

##### Solution

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,k,x=0,p,a;
cin>>n>>k;
for(int i=0;i<n; i++){
cin>>a;
if(i!=k)
x+=a;
}
x=x/2;
cin>>p;
if(p-x==0)
cout<<"Bon Appetit";
else cout<<p-x;
return 0;
}
```
``````n, k = map(int, raw_input().split())
arr = map(int, raw_input().split())
val = input()
t = val - (sum(arr) - arr[k]) / 2
if (t == 0):
print "Bon Appetit"
else:
print t``````
``````n, k = map(int, raw_input().split())
arr = map(int, raw_input().split())
val = input()
t = val - (sum(arr) - arr[k])/2
if (t == 0):
print "Bon Appetit"
else:
assert val == sum(arr)/2
print t``````

JAVA

``````
import java.util.*;

class Solution {
public static void bonAppetit(int[] cost, int k, int charged) {
int total = (charged << 1);

for (int i : cost) {
total = total - i;
}

System.out.println( (total >= 0) ? cost[k] / 2 : "Bon Appetit");
}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int[] cost = new int[n];
for (int i = 0; i < n; i++) {
cost[i] = scan.nextInt();
}
int charged = scan.nextInt();
scan.close();

bonAppetit(cost, k, charged);
}
}``````

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

### hasectic

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi