Hello coders, today we are going to solve **Bill Division HackerRank Solution** which is a Part of **HackerRank Algorithm** Series.

**Task**

Two friends Anna and Brian, are deciding how to split the bill at a dinner. Each will only pay for the items they consume. 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:Â ** bill = [2, 4, 6]**. Anna declines to eat itemÂ

**Â which costsÂ**

*k*=*bill*[2]**6**. If Brian calculates the bill correctly, Anna will payÂ

**(2 + 4)/2 = 3**. If he includes the cost ofÂ

**, he will calculateÂ**

*bill*[2]**(2 + 4 + 6)/2 = 6**. In the second case, he should refundÂ

**3**Â to Anna.

**Function Description**

Complete the *bonAppetit* function in the editor below. It should print `Bon Appetit`

if the bill is fairly split. Otherwise, it should print the integer amount of money that Brian owes Anna.

bonAppetit has the following parameter(s):

*bill*: an array of integers representing the cost of each item ordered*k*: an integer representing the zero-based index of the item Anna doesn’t eat*b*: the amount of money that Anna contributed to the bill

**Input Format**

The first line contains two space-separated integersÂ ** n**Â andÂ

**, the number of items ordered and theÂ**

*k***0**-based index of the item that Anna did not eat.

The second line containsÂ

**Â space-separated integersÂ**

*n***Â whereÂ**

*bill*[*i*]**0 <=**.

*i*<*n*The third line contains an integer,Â

**, the amount of money that Brian charged Anna for her share of the bill.**

*b***Constraints**

**2 <=***n*<= 10^{5}**0 <=****k****<***n***0 <=***bill*[*i*] <= 10^{4}**0 <=***b*<=*bill*[*i*]- The amount of money due Anna will always be an integer

**Output Format**

If Brian did not overcharge Anna, printÂ `Bon Appetit`

Â on a new line; otherwise, print the difference (i.e.,Â ** bc_{harged} – b_{actual}**) that Brian must refund to Anna. This will always be an integer.

**Sample Input 0**

```
4 1
3 10 2 9
12
```

**Sample Output** **0**

`5`

**Explanation 0**

Anna didn’t eat itemÂ ** bill[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

**Â**. Brian charged herÂ

*b*= 7_{actual}**Â for her portion of the bill. We print the amount Anna was overcharged,Â**

*b*= 12_{charged}**, on a new line.**

*b*– b_{charged}_{actual}= 12 – 7 = 5**Sample Input 1**

```
4 1
3 10 2 9
7
```

**Sample Output 1**

`Bon Appetit`

**Explanation 1**

Anna didn’t eat itemÂ ** bill[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

**Â**

*b*_{actual}**= 7**. BecauseÂ

**, we printÂ**

*b*=_{actual}*b*= 7_{charged}`Bon Appetit`

Â on a new line.**Solution – Bill Division**

**C++**

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int n, k, sum=0; cin >> n >> k; for (int i=0;i<n;i++) { int a; cin >> a; if (i!=k) sum+=a; } int l; cin >> l; if (sum/2==l) cout << "Bon Appetit" << endl; else cout << l-sum/2 << endl; }

**Python**

n, k = map(int, input().split()) a = list(map(int, input().split())) b = int(input()) x = sum(a) - a[k] if 2 * b == x: print("Bon Appetit") else: print(a[k] // 2)

**Java**

import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int sum = 0; for(int i = 0; i < n; i++){ if(i != k){ sum += in.nextInt(); }else{ in.nextInt(); } } int res = in.nextInt(); int t = res - sum / 2; System.out.println(t == 0 ? ("Bon Appetit") : t); } }

**Disclaimer:** The above Problem **(Bill Division)** is generated by **Hacker Rank** but the Solution is Provided by **CodingBroz**. This tutorial is only for **Educational** and **Learning** Purpose.