- Practice
- Algorithms
- Implementation
- Bon Appétit
- Discussions

# Bon Appétit

# Bon Appétit

- BP
baparson + 4 comments This problem and expected answers are bogus. To get

*true*cost, you should be evaluating using floating point, but using floats gets you "incorrect" answers for #4 and #5. Switching about to using ints you get all test cases passing. So this problem is pretty misleading.AnantaOne1 + 3 comments It's not misleading, you are missing a step, 1) calculate using ddata type "double" 2) type cast it into a long 3) Print it

shashank_30 + 0 comments thanks :)

- M
smeethsupershah + 1 comment Why does it not work with float,but works perfectly fine with int and double?

- OS
otaviosantana + 0 comments If you're using java, double has highly precision. But you don't need to divide every cost, you can sum all values e divide by 2 at the end.

- SD
SaiManideep1 + 0 comments It ain`t working for me

- SS
Pvokd + 0 comments thank you, I was stuck on this cause I had used floating point. When i changed it to int everything ran perfectly fine.

KalyanamKiran + 3 comments the question specifically states that the result will be an integer, why are u using float???

AnantaOne1 + 2 comments For accuract, and you can type cast it back to integer....

- AA
alaniane + 0 comments Floats don't give you accuracy. If you want to have accuracy in your division when there is a remainder with integer division, you have to use a BCD (binary coded decimal) or other format. In C# you can use decimal. However, if you are going to cast back to a long then you might as well use integer division. Casting just truncates, it does not round.

bennattj + 0 comments To add to alaniane's comment. Float's are not acceptable for representing 32-bit integers since the float type (at least in Java) is, itself, 32 bits. The largest exact integer representations for floating point numbers will depend on the size of the mantissa (and cannot possibly be larger). Since it's necessary for the mantissa to be smaller than 32 bits (for a float), you will not be able to represent all 32-bit int's. This is not a problem for a double (64 bit floating point representation) because the mantissa is much larger than 32 bits.

Assuming Java uses a 24-bit mantissa for a float (from Wikipedia on IEEE 32-bit float), that would mean a largest integer value of around 10^7. Since n can be as large as 10^5 and c[i] as large as 10^4, the maximum value for the summation could be as high as 10^9 (well above what a 24-bit floating point represenation can represent, exactly).

sonijha2395 + 0 comments yeah exactly ..

ScienceD + 0 comments Yea I was confused about it too. But they I found that result will be

**int**, and was relived!

- NS
nadeshseen + 0 comments thanks

- JD
jesdga95 + 6 comments Java solution:

import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); int total = 0; for (int i = 0; i < n; i++) { int price = scanner.nextInt(); if (i != k) total += price; } int charged = scanner.nextInt(); System.out.println(total / 2 == charged ? "Bon Appetit" : charged - (total/2)); } }

- NP
npatel1 + 0 comments Compact, I like it!

- DC
danielgcwik + 0 comments thanks it worked!

JulioLeme + 0 comments Simply great.

- DL
daft_leech + 0 comments A Java 8 version (very simple):

int max = IntStream.range(0,n).map(x -> x!=k?ar[x]:0).sum()/2; System.out.println(max==b?"Bon Appetit":(b-max));

- SK
sksunilkumar107 + 0 comments In the problem,there is method name private static int bonAppetit(int n, int k, int b, int[] ar) { }, we need to write the implementaion within the method..... so there is no use of this method?

- MS
mariojsanchez15 + 0 comments int sum = 0; for( int i = 0; i < n; i++ ) sum += prices[ i ]; System.out.println( sum / 2 == paid ? prices[ k ] / 2 : "Bon Appetit" );

AntonVS + 2 comments This problem is missing proper bindings / frame for the Javascript(node.js) language version.

- TR
troberts8 + 0 comments No one cares about us :(

uzorjchibuzor + 0 comments Like the other user said, nobody cares about us. I thought I was seeing things wrongly until I saw this comment.

- VP
pandeyandroweb + 2 comments #include <bits/stdc++.h> using namespace std; int main() { int n; int k; int sum=0,diff=0; cin >>n; cin>>k; vector<int> ar(n); for(int ar_i = 0; ar_i < n; ar_i++){ cin >> ar[ar_i]; } for(int j=0;j<n;j++) { sum+=ar[j]; } int b; cin >> b; sum=sum-ar[k]; sum=sum/2; diff=b-sum; if(b>sum) { cout<<diff; } else { cout<<"Bon Appetit"; } return 0; }

wanghaitang_ufl + 1 comment It is nice. But we can actully simplify the code using accumulate to do the summation for the trick: ... int total = accumulate(ar.begin(), ar.end(), 0)-ar[k]; ...

- AB
adityabadve123 + 0 comments Did just that

string bonAppetit(int n, int k, int b, vector <int> ar) { int actual_split; int charged_split = b; string res = ""; actual_split = (accumulate(ar.begin(), ar.end(), 0)/2) - (ar[k]/2); if(charged_split - actual_split == 0){ res = "Bon Appetit"; }else{ res = to_string(charged_split - actual_split); } return(res); }

diksha_tsingh + 0 comments Dont do this in main..it is very simple. Use anothr function and tell how to print Bon appetit and what to return in that case bacause return type is integer.

runcy + 3 comments Python one-liner logic:

n, k = map(int, input().strip().split(' ')) c = [int(x) for x in input().strip().split(' ')] b = int(input().strip()) print('Bon Appetit' if (sum(c) - c[k]) // 2 == b else c[k] // 2)

LeHarkunwar + 2 comments I wrote a two liner

def bonAppetit(n, k, b, ar): annaBill = sum(ar[i] for i in range(n) if i != k)//2 return 'Bon Appetit' if annaBill == b else str(b-annaBill)

hilder_vitor + 0 comments this summation with an if inside is surely slower than just adding up everything and subtracting k like this: sum(ar) - ar[k]

- MA
malexand314 + 1 comment You're looping two times over the list. You can avoid this by deleting the k-th item from the list with

`del ar[k]`

, just substracting it from the sum or using reduce instead of sum + list comprehension, even though it's not necessary for the test cases.hilder_vitor + 0 comments Well... Do you know the way that built-in function del is implemented? Because it probably shifts all elements from position k+1 until the last position, which would mean that you would still looping the list two times...

- FD
f2dahlitz + 0 comments Is simply did the same thing, although my variables are named a little different, the logic is still the same. In my opinion the easiest way to solve it.

# Enter your code here. Read input from STDIN. Print output to STDOUT n, k = [int(x) for x in input().split(" ")] c = [int(x) for x in input().split(" ")] charged = int(input()) print("Bon Appetit" if (sum(c) - c[k]) // 2 == charged else c[k] // 2)

- LK
lokeshlkr + 0 comments Perfect!!!

- DB
danielsinferno + 3 comments I ♥ Ruby

get_ary = -> { gets.split.map(&:to_i) } n, k = get_ary.() cost = get_ary.() charge = gets.to_i refund = charge - (cost.reduce(:+) - cost[k])/2 puts refund != 0 ? refund : 'Bon Appetit'

- SH
sachin_hg + 1 comment Hey, can you tell me where am I going wrong? It fails on test case 4 and 5.

This is my solution in Ruby:

n,k =gets.split(' ').map{|x| x.to_i} items = gets.split(' ').map{|x| x.to_i} charge = gets.to_i items_shrd = items - [items[k]] sum = items_shrd.reduce(:+).to_i shr = sum/2 if shr == charge puts 'Bon Appetit' else puts "#{charge-shr}" end

- DB
danielsinferno + 1 comment `items - [items[k]]`

only works as long there is only one item which value is item[k] because A - B on arrays is mathematical A \ B[1, 2, 3, 3] - [3] => [1, 2]

P.S. Use markdown for code postings

- SH
sachin_hg + 0 comments yeah ! got it and changed it! thanx btw

ozguryarikkas + 0 comments it is great to see how one is using lambda method properly. it really makes the code shorter!

impskievua + 0 comments i like it more w/o lambdas

i=gets.split.last.to_i costs=gets.split.map(&:to_i) diff = gets.to_i - (costs.inject(:+) - costs[i]) / 2 puts diff.zero? ? "Bon Appetit" : diff

- A
ali_svo + 0 comments A simple C# solution:

`string[] input1 = Console.ReadLine().Split(' '); int n=Convert.ToInt32(input1[0]); int k=Convert.ToInt32(input1[1]); int[] c=Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int b = Convert.ToInt32(Console.ReadLine()); int sum1= c.Where((x, i) => i != k).Sum(); if (b == sum1/2) Console.WriteLine("Bon Appetit"); else Console.WriteLine(b - sum1/2);`

mayankmangal2007 + 0 comments #include <math.h> #include <stdio.h> #include <string.h> int main() { long long int n,k,i,arr[100000],b,sum=0,actual; scanf("%lld %lld", &n, &k); for (i=0;i<n;i++) { scanf ("%lld",&arr[i]); } scanf ("%lld",&b); for (i=0;i<n;i++) { sum=sum+arr[i]; } actual=(sum-arr[k])/2; if (b>actual) printf ("%lld",b-actual); else if (b=actual) printf ("Bon Appetit"); return 0; }

- RB
bankarohit1809 + 0 comments I am using C++ and we are expected to only complete the function. Can someone tell me how can we achieve this without modfying the main function. the function has to return something even if the bill is correctly split and it will be printed to consol

#include <bits/stdc++.h> using namespace std; int bonAppetit(int n, int k, int b, vector <int> ar) { // Complete this function } int main() { int n; int k; cin >> n >> k; vector<int> ar(n); for(int ar_i = 0; ar_i < n; ar_i++){ cin >> ar[ar_i]; } int b; cin >> b; int result = bonAppetit(n, k, b, ar); cout << result << endl; return 0; }

- AN
amyshukz + 2 comments In Python 3

n,c = input().split() li = [int(i) for i in input().split()] brian_charged = int(input()) anna_price = li.pop(int(c)) total = 0 for i in range(0,len(li)): total+=li[i] total = total//2 if(brian_charged>total): print(brian_charged-total) elif(brian_charged==total): print("Bon Appetit")

- AN
amyshukz + 0 comments [deleted] - JP
jackpincombe1 + 0 comments [deleted]

Sort 337 Discussions, By:

Please Login in order to post a comment