Bill Division HackerRank Solution in C, C++, Java, Python

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  k=bill[2] which costs 6. If Brian calculates the bill correctly, Anna will pay (2+4)/2. If he includes the cost of bill[2], he will calculate (2+4+6)/2. 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 k, the number of items ordered and the 0-based index of the item that Anna did not eat.

The second line contains n space-separated integers bills[i] where 0<=i<n.

The third line contains an integer,b , the amount of money that Brian charged Anna for her share of the bill.


  • 2<=n<=10^5
  • 0<=k<n
  • 0<=bill[i]<=10^4
  • 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 that Brian must refund to Anna. This will always be an integer.

Sample Input 0

4 1

3 10 2 9



Sample Output 0



Bill Division HackerRank Solution in C

int main()
int n,k;
scanf("%d %d",&n,&k);
int i,a[n];
int sum = 0;
sum += a[i];
int paid;
int toBePaid = sum-a[k];
printf("Bon Appetit\n");
return 0;

Bill Division HackerRank Solution in 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;


Bill Division HackerRank Solution in Java

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner sc=new Scanner(;
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
int sum=0;
for(int i=0;i<n;i++){
int num=sc.nextInt();
System.out.println("Bon Appetit");


Bill Division HackerRank Solution in Python

import bisect
import itertools
import math
import random
import string

getArray = lambda : map(int, raw_input().strip().split())
getInt = lambda : int(raw_input().strip())


n, k = getArray()
c = getArray()
b = getInt()

fair = sum(c[:k] + c[k+1:])/2

print "Bon Appetit" if b == fair else b - fair


Bill Division HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
class Solution {
static void Main(String[] args) {
var tmp = Console.ReadLine().Split(' ');
int n = int.Parse(tmp[0]);
int k = int.Parse(tmp[1]);

int[] A = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);

int total = (A.Sum() - A[k]) / 2;
int c = int.Parse(Console.ReadLine());
if (c == total) {
Console.WriteLine("Bon Appetit");
} else {
Console.WriteLine(c - total );


Attempt – Bill Division HackerRank Challenge

Link –

Next HackerRank Challenge Solution 

Link –

Leave a Comment