HackerRank Bill Division Problem Solution

 


HackerRank Bill Division Problem Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n, k, total;
    int brian_tot = 0;
    scanf("%d %d\n", &n, &k);
    int *arr = (int*)malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d ", &arr[i]);
        if (i != k)
            brian_tot += arr[i];
    }
    scanf("\n%d", &total);
    if (brian_tot / 2 == total)
        printf("Bon Appetit");
    else
    {
        printf("%d", total - brian_tot/2);
    }
        
    return 0;
}

HackerRank Bill Division Problem Solution in JavaScript

function processData(input) {
    //Enter your code here
    
    var lines = input.split('\n');
    var n_k = lines[0].split(' ');
    var n = parseInt(n_k[0]);
    var k = parseInt(n_k[1]);
    var a = lines[1].split(' ');
    a = a.map(Number);
    var b_charged = parseInt(lines[2]);
    
    var b_actual = 0;
    for (var i = 0; i < a.length; i++) {
        if (i != k) {
            b_actual += a[i];         
        }
    }
    
    var difference = b_charged - (b_actual / 2);
    if (difference === 0)
        console.log("Bon Appetit");
    else
        console.log(difference);
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

HackerRank Bill Division Problem Solution in Python

n, k = list(map(int, input().split()))
L = list(map(int, input().split()))
c = int(input())
L.pop(k)
if sum(L)//2 == c:
    print ('Bon Appetit')
else:
    print (c - sum(L)//2)

0 Comments