Taum and B’day HackerRank Solution in C, C++, Java, Python

Taum is planning to celebrate the birthday of his friend, Diksha. There are two types of gifts that Diksha wants from Taum: one is black and the other is white. To make her happy, Taum has to buy b black gifts and white gifts.

  • The cost of each black gift is bc units.
  • The cost of every white gift is wc units.
  • The cost to convert a black gift into white gift or vice versa is units.

Determine the minimum cost of Diksha’s gifts.

Example

b=3
w=5
bc=3
wc=4
z=1

He can buy a black gift for 3 and convert it to a white gift for 1, making the total cost of each white gift 4. That matches the cost of a white gift, so he can do that or just buy black gifts and white gifts. Either way, the overall cost is 3*3+5*4=29.

Function Description

Complete the function taumBday in the editor below. It should return the minimal cost of obtaining the desired gifts.

taumBday has the following parameter(s):

int b: the number of black gifts
int w: the number of white gifts
int bc: the cost of a black gift
int wc: the cost of a white gift
int z: the cost to convert one color gift to the other color

Returns

int: the minimum cost to purchase the gifts.

Input Format

The first line will contain an integer t, the number of test cases.

The next t pairs of lines are as follows:

– The first line contains the values of integers b and w.

– The next line contains the values of integers bc,wc , and z.

Constraints

1<=t<=10

0<=b,w,b,wc,z<=10^9

Output Format

lines, each containing an integer: the minimum amount of units Taum needs to spend on gifts.

Sample Input

STDIN Function

----- --------

5 t = 5

10 10 b = 10, w = 10

1 1 1 bc = 1, wc = 1, z = 1

5 9 b = 5, w = 5

2 3 4 bc = 2, wc = 3, z = 4

3 6 b = 3, w = 6

9 1 1 bc = 9, wc = 1, z = 1

7 7 b = 7, w = 7

4 2 1 bc = 4, wc = 2, z = 1

3 3 b = 3, w = 3

1 9 2 bc = 1, wc = 9, z = 2

Sample Output

20

37

12

35

12

Taum and B’day HackerRank Solution in C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
long int min(long int a, long int b, long int c);
int main() {
int t;
long int b,w,x,y,z;
scanf("%d",&t);
while(t--)
{
long int sum=0;
long int sum1=0,sum2=0;
scanf("%ld%ld",&b,&w);
scanf("%ld%ld%ld",&x,&y,&z);
sum=b*x+w*y;
sum1=(b+w)*x+w*z;
sum2=(b+w)*y+b*z;
printf("%ld\n",min(sum,sum1,sum2));
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
return 0;
}
long int min(long int a,long int b, long int c)
{
if(a<=b&&a<=c)
return a;
if(b<=a&&b<=c)
return b;
return c;
}

Taum and B’day HackerRank Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int cases;
scanf("%d", &cases);
while (cases--) {
int B, W, X, Y, Z;
scanf("%d %d %d %d %d", &B, &W, &X, &Y, &Z);
long long res = (long long)B * X + (long long)W * Y;
res = min(res, (long long)B * X + (long long)W * (X + Z));
res = min(res, (long long)B * (Y + Z) + (long long)W * Y);
printf("%lld\n", res);
}
return 0;
}

Taum and B’day HackerRank Solution in Java

import java.io.*;
import java.util.*;
public class Solution
{
public static void main(String[] args)
{
int m;
long b,w,x,y,z;
long v1,v2,v3,v4,s;
Scanner in = new Scanner(System.in);
m = in.nextInt();
while(m!=0)
{
b = in.nextLong();
w = in.nextLong();
x = in.nextLong();
y = in.nextLong();
z = in.nextLong();
v1=0L;v2=0L;v3=0L;v4=0L;s=0L;
v1 = (b*x)+(w*y);
v2 = (b*z)+(b*y) + (w*z)+(w*x);
if(v1<=v2)
s=v1;
else
s=v2;
v3 = (b*x) + (w*z)+(w*x);
if(v3<=s)
s=v3;
v4 = (b*z)+(b*y) + (w*y);;
if(v4<=s)
s=v4;
System.out.println(s);
m--;
}
}
}

Taum and B’day HackerRank Solution in Python

def solve(b, w, x, y, z) :
return b*min(x, y+z) + w*min(y, x+z)

for _ in xrange(int(input())) :
bw = map(int, raw_input().split())
b, w = bw[0], bw[1]
xyz = map(int, raw_input().split())
x, y, z = xyz[0], xyz[1], xyz[2]
print solve(b, w, x, y, z)

Taum and B’day HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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 */
int t = int.Parse(Console.ReadLine());
while(t-- > 0){
long[] q = Console.ReadLine().Trim().Split().Select(x => long.Parse(x)).ToArray();
long b = q[0];
long w = q[1];
long[] num = Console.ReadLine().Trim().Split().Select(x => long.Parse(x)).ToArray();
long bcost = num[0];
long wcost = num[1];
long conversion = num[2];
long total = -1;
if(wcost + conversion < bcost){
total = w * wcost + (b * ( wcost + conversion));
} else if (bcost + conversion < wcost){
total = b * bcost + (w * ( bcost + conversion));
}else{
total = b * bcost + w * wcost;
}
Console.WriteLine(total);
}
}
}

Attempt Taum and B’day HackerRank Challenge

Linkhttps://www.hackerrank.com/challenges/taum-and-bday/

Next HackerRank Challenge Solution

Linkhttps://exploringbits.com/organizing-containers-of-balls-solution/

Leave a Comment