Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Bill Division problem solution

YASH PAL, 31 July 202430 November 2025

HackerRank Bill Division problem solution – In this Bill Divison problem, you need to complete the function bonAppetit that should print Bon Appetit if the bill is fairly split. otherwise, it should print the integer amount of money that Brian owes Anna.

HackerRank bill division problem solution
HackerRank bill division problem solution

Problem solution in Python programming.

n, k = map(int, input().split())
c = list(map(int, input().split()))
b_charged = int(input())

b = (sum(c) - c[k]) / 2
if b_charged == b:
    print("Bon Appetit")
else:
    print(int(b_charged-b))

Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int items = in.nextInt();
        int skipped = in.nextInt();
        
        int totalToSplit = 0;
        for (int i = 0; i < items; ++i) {
            int cost = in.nextInt();
            if (i != skipped) {
                totalToSplit += cost;
            }
        }
        
        int paid = in.nextInt();
        
        if (totalToSplit / 2 >= paid) {
            System.out.println("Bon Appetit");
        }
        else {
            System.out.println(paid - totalToSplit / 2);
        }
    }
}

 

Problem solution in C++ programming.

#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;
}

 

Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int n,k;
    scanf("%d %d",&n,&k);
    int a[n];
    int aller;
    int sum=0;
    for(int i=0;i<n;i++){
        scanf("%d",a+i);
        if(i==k)
            aller=a[i];
        else
            sum+=a[i];
    }
    int part=sum/2;
    int charg;
    scanf("%d",&charg);
    if(part==charg)
        printf("Bon Appetitn");
    else
        printf("%dn",charg-part);
    return 0;
}

 

Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    var lines = input.split('n');
    // first line n k
    var n = +(lines[0].split(' ')[0]);
    var k = +(lines[0].split(' ')[1]);
    // second line is each item cost
    var items = lines[1].split(' ');
    // third line is how much brian charged anna
    var annaCharged = +(lines[2]);
    var totalCostOfSharedItems = 0;
    var actualCost = 0;
    items.forEach(function(item, idx) {
        if (idx !== k) {
            totalCostOfSharedItems += (parseInt(item));
        }
        actualCost += (parseInt(item));
    });
    if (annaCharged === (totalCostOfSharedItems / 2)) {
        console.log('Bon Appetit');
    } else {
        console.log((actualCost / 2) - (totalCostOfSharedItems / 2));
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

 

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes