HackerRank Bill Division Problem Solution Yashwant Parihar, April 12, 2023April 12, 2023 In this post, We are going to solve HackerRank Bill Division Problem. Two friends Anna and Brian, are deciding how to split the bill at 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 = 3. If he includes the cost of bill 2, he will calculate (2 + 4 + 6)/2 = 6. 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. Bon Appetit 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 bill[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. Output Format If Brian did not overcharge Anna, print Bon Appetit on a new line; otherwise, print the difference (i.e.,bcharged – bcharged) that Brian must refund to Anna. This will always be an integer. Sample Input 0 4 1 3 10 2 9 12 Sample Output 0 5 Sample Input 1 4 1 3 10 2 9 7 Sample Output 1 Bon Appetit Explanation 1Anna didn’t eat item bill[1] = 10, but she shared the rest of the items with Brian. The total cost of the shared items is 3 + 2 + 9 = 14 and, split in half, the cost per person is b actual = 7. Because b = b charged = 7 actual , we print Bon Appetit on a new line. HackerRank Bill Division Problem Solution Bill Division C Solution #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int n,k,i,c=0,m; int a[100005]; scanf("%d %d",&n,&k); for(i=0;i<n;i++) { scanf("%d",&a[i]); if(i!=k) c=c+a[i]; } scanf("%d",&m); c=c/2; if(c==m) printf("Bon Appetit\n"); else printf("%d\n",m-c); /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; } Bill Division C++ Solution #include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define maxn 500002 #define mmaxn 1000000000 #define pii pair<int,int> #define pll pair<ll int,ll int> #define pdd pair<double,double> #define mp make_pair #define S(a) scanf("%d",&a) #define SSS(a,b,c) scanf("%d%d%d",&a,&b,&c) #define SSSS(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d) int main(){ int n,k; int arr[maxn]; cin>>n>>k; ll int sum=0; for(int i=0;i<n;i++){ cin>>arr[i]; sum+=arr[i]; } int c; cin>>c; if ( c==((sum/2)-arr[k]/2)) cout<<"Bon Appetit"<<"\n"; else cout<<arr[k]/2<<"\n"; } Bill Division C Sharp Solution using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ string strCounters = Console.ReadLine(); int[] arrCounters = Array.ConvertAll<string, int>(strCounters.Split(' '), int.Parse); int n = arrCounters[0]; int k = arrCounters[1]; string strPrices = Console.ReadLine(); int[] prices = Array.ConvertAll<string, int>(strPrices.Split(' '), int.Parse); int totalShared = 0; for (int i =0; i < n; i++){ if (i != k){ totalShared = totalShared + prices[i]; } } int fair = totalShared / 2; string strCharged = Console.ReadLine(); int charged = Convert.ToInt32(strCharged); if (charged == fair){ Console.WriteLine("Bon Appetit"); }else{ Console.WriteLine(charged - fair); } } } Bill Division Java Solution import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'bonAppetit' function below. * * The function accepts following parameters: * 1. INTEGER_ARRAY bill * 2. INTEGER k * 3. INTEGER b */ public static void bonAppetit(List<Integer> bill, int k, int b) { int billTotal = 0; for(int dish : bill) { billTotal += dish; } if(b == (billTotal - bill.get(k))/2) { System.out.println("Bon Appetit"); } else { System.out.println(b - (billTotal - bill.get(k))/2); } } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); int n = Integer.parseInt(firstMultipleInput[0]); int k = Integer.parseInt(firstMultipleInput[1]); List<Integer> bill = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) .map(Integer::parseInt) .collect(toList()); int b = Integer.parseInt(bufferedReader.readLine().trim()); Result.bonAppetit(bill, k, b); bufferedReader.close(); } } Bill Division JavaScript Solution function processData(input) { var data = formatInput(input); var totalCost = data['costs'].reduce(function(prev, curr){ return parseInt(prev) + parseInt(curr); }); var notOwed = parseInt(data['costs'][data['k']]); var actualCost = (totalCost - notOwed)/2; if (actualCost == data['charged']) { console.log('Bon Appetit') } else { var refund = parseInt(data['charged']) - actualCost; console.log(refund); } } function formatInput(input) { var lines = input.split('\n'); var n = lines[0].split(' ')[0]; var k = lines[0].split(' ')[1]; var costs = lines[1].split(' '); var charged = parseInt(lines[2]); return {n:n, k:k, costs:costs, charged:charged}; } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); Bill Division Python Solution line1 = input() allergy = -1 if len(line1.split()) == 2: allergy = int(line1.split()[1]) items = int(line1.split()[0]) items_list = input().split() cost = int(input()) brian = 0 anna = 0 for i in range(0, len(items_list)): item_cost = int(items_list[i]) if i == allergy: brian += item_cost else: anna += item_cost/2.0 brian += item_cost/2.0 if cost == anna: print("Bon Appetit") else: print(int(round(cost - anna))) Other solutions HackerRank Sales by Match Problem Solution HackerRank Drawing Book Problem Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython