Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
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 *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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