Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • 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

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Bill Division problem solution

YASH PAL, 31 July 20249 August 2024

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

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

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
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes