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 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); }); algorithm coding problems AlgorithmsHackerRank
#python code def bonAppetit(bill, k, b_charged): total_bill = sum(bill) actual_bill = (total_bill – bill[k]) / 2 if b_charged == actual_bill: print("Bon Appetit") else: print(int(b_charged – actual_bill))