Sort by

recency

|

1812 Discussions

|

  • + 0 comments

    This is my solution in Js:

        // Write your code here
        let sharedBill = bill.filter((item, index)=> index !== k).reduce((acc, ele)=>acc+ele,0)/2;
           
    console.log(sharedBill === b ? 'Bon Appetit' : b-sharedBill);
    }
    
  • + 0 comments
    def bonAppetit(bill, k, b):
        # Write your code here
        actual=0
        for i in bill:
            actual=actual+i
        actual-=bill[k]
        actual=actual//2
        if b==actual:
            print("Bon Appetit")
        else:
            print(b-actual)
    
  • + 0 comments

    rust

    fn bon_appetit(bill: &[i32], k: i32, b: i32) {
        let _k = k as usize;
        let out = match bill.iter()
            .enumerate()
            .filter_map(|(i, x)| if i != _k {Some(*x)} else {None})
            .sum::<i32>()
            .wrapping_div(2)
            .wrapping_sub(b)
            .abs() {
                0 => "Bon Appetit".to_string(),
                x => x.to_string()
            };
        println!("{out}")
    }
    
  • + 0 comments

    When splitting a bill, like Anna and Brian did, fairness means only paying for what you actually ate. If Anna skipped an item, that cost should be excluded before dividing the total. For example, if the meal included DQ burgers and fries but Anna passed on one burger, Brian should calculate her share based only on the items she consumed. If he overcharged her, the extra amount should be refunded. You can try this logic in real scenarios.

  • + 0 comments
    public static void bonAppetit(List<Integer> bill, int k, int b) {
        int supposedSum = 0;
        for(int i=0;i<bill.size();i++) {
            if(i!=k) supposedSum += bill.get(i);
        }
        int supposedPaid = supposedSum/2;
        if(supposedPaid == b) System.out.println("Bon Appetit");
        else {
            int toRefund = b-supposedPaid;
            System.out.println(toRefund);
        }
    }