# Time in Trains

Practice

Author: Mudit Mahajan

Cakewalk

# PROBLEM:

There are three cities A, B and C.

A one-way train takes x hours to travel from A to B.

Another one-way train takes y hours to travel from B to C.

Another one-way train takes z hours to travel from C to A.

Chef will start from any one city and travel to the other 2 cities.

Find the the minimum amount of time he will have to spend in his travels.

# EXPLANATION:

We need to find the minimum time taken to travel al the cities starting from any of the three cities.

The answer will be the minimum of the sum of any two of the three times of travel.

# SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int x, y, z;
cin>>x>>y>>z;
int a, b, c;
a = x + y;
b = y + z;
c = z + x;
int ans = INT_MAX;
ans = min(a, ans);
ans = min(b, ans);
ans = min(c, ans);
cout << ans <<endl;
return 0;
}

# Time Balance

Practice

Author: Devansh Goel

Easy

# PROBLEM:

Chef is fond of coding and gaming and wants to spend his free time on these two activities.

He wants to find how much total time he will spend doing these two activities over n days.

The days start from 1 and go on to n.

On the odd days, Chef will practice coding for 25 minutes and on the even days he will play game for 45 minutes.

If the free time on a given day is less than the time required for gaming or coding (if a day is even or odd), he will not do anything.

You are given the number of days and the free time (t_i) Chef has on each day.

Find the total time spent by him on the 2 activities over n days.

# EXPLANATION:

The day start from 1. Therefore, for n days, first check if the day is odd and time is greater than or equal to 25. Simply add 25 to the answer for each such day.
If the day is not odd, then it will be even and check if the time is equal to 45.

# SOLUTION:

Setter's Solution
``````#include <stdio.h>

int main()
{
//input the no. of days
int n=0;
scanf("%d",&n);

//input the free time available on each day
int t[n+1];
int i=0;
for(i=1;i<=n;i++)
{
scanf("%d",&t[i]);
}

int ans=0;

for(i=1;i<=n;i++)
{
if(i%2!=0) //odd day
{
if(t[i]>=25)
ans+=25;
}
else if(i%2==0) //even day
{
if(t[i]>=45)
ans+=45;
}
}
printf("%d\n",ans);
return 0;
}
``````

# Pies of Apple

Practice

Author: Mudit Mahajan

Cake Walk

# PROBLEM:

Chef wants to bake Apple Pies.

He has p pieces of apples beforehand and he has a apples. He will cut each apple in 3 pieces.

Chef uses 2 apples for each apple pie he bakes.

Help him find the maximum number of pies he can bake.

# EXPLANATION:

We already have p pieces and the number of pieces obtained by cutting up the apples will be 3 * a (number of apples), as given in the problem statement.

The final answer will be the the half of the total number of pieces of apples.

# SOLUTIONS:

Setter's Solution
``````#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>p;
int total = 3 * a + b;
cout << total / 2 << endl;
return 0;
}

``````

# Chocolate Boxes

Practice

Author: Utkarsh Bhatnagar

Cake Walk

# PROBLEM:

Chef has two types of chocolate boxes A and B. He has x boxes of type A and y boxes of type B.

He wants to gift chocolates to 3 of his friends. Chef can gift chocolates to his friends in 3 following ways:

• He can divide all A type chocolates (x) and give them to his friends.
• He can divide all B type chocolates (y) and give them to his friends.
• He can divide all A and B type chocolates (x + y) and give them to his friends.

Chef wants to know if there is any way ou of the above 3 mentioned such that his friends all receive equal amount of chocolates.

If it is possible, print YES otherwise print NO.

# EXPLANATION:

We have x boxes of A type and y boxes of B type.

Chocolate can be destributed in either of:

• give them all x boxes.
• give them all y boxes.
• give them all x+y boxes.

So if number of given boxes can be divided into 3 (x%3==0||y%3==0||x+y%3==0) than possible.

# SOLUTIONS:

Setter's Solution
``````#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
if(a%3==0||b%3==0||(a+b)%3==0)
cout<<"Possible"<<endl;
else
cout<<"Impossible"<<endl;
}
``````

# Minutes Left

Practice

Author: Mudit Mahajan

Easy

# PROBLEM:

Chef likes New Years very much.

You know the current time and Chef wants you to find the minutes before New Year comes.

The time is given in h hours and m minutes. The time is in 24-hour format.

New Year comes when the time is exactly 0 hours and 0 minutes.

You have to find the minutes before New Year comes.

Note: It is guaranteed that the time given is never New Year, i.e. the following two conditions will not be met at the same time: h = 0 and m = 0.

# EXPLANATION:

We are given the hours and minutes at present and we need to find the minutes it will take to midnight or 00:00.

We can get the hours left to midnight easily by subtracting the hour at the given time from the total 24 hours, i.e., (24 - h). We can then multiply this with 60 to get the minutes.

If the minutes at the given time is not zero we can subtract the minutes at the given time (which will be between 0 and 59 inclusive) from the above calculated minutes and get our answer.

# SOLUTIONS:

Setter's Solution
``````#include<iostream>
using namespace std;
int main()
{
int h, m;
cin>>h>>m;
int minutes = 60 * (24 - h);
minutes -= m;
cout << minutes << endl;
return 0;
}

``````

# Digit Game

Practice

Author: Utkarsh Bhatnagar

Cake Walk

# PROBLEM:

The chef and his father are playing a game. He gave the chef 3 digits 1 to 9 inclusive. He has to form the largest number from it in the following manner:-

Three digits are: 4 7 1

74+1=75

Given the digits, A, B, and C. Find the maximum value possible.

# EXPLANATION:

We have to find the largest number formed in the given manner

lets input number:- A B C

Since the most significant place in ten place so largest of the three numbers should be at tenth place (for the number to be largest).

The other two numbers can be interchangeably used.

lets assume A>B>C so   Ans=AxB+C or AxC+B

# SOLUTIONS:

Setter's Solution
``````#include "bits/stdc++.h"
using namespace std;

int main()
{
int a[3];
cin >> a[0] >> a[1] >> a[2];
sort(a, a + 3);
int ans = (a[2] * 10 + a[1]) + a[0];
cout << ans;
return 0;
}
``````

# Delivery Coupon

Practice

Author: Devansh Goel

Easy

# PROBLEM:

Chef wants to order food from a app.

He will order three items costing A1, A2 and A3 on the first day and three items costing B1, B2 and B3 on the second day.

There is also D delivery cost that is applied to both orders on each day.

That is Chef will have to pay (A1 + A2 + A3 + B1 + B2 + B3 + D + D) as total for his orders.

There is a coupon costing C that when applied will mean there will be no delivery charges on either day.

That is Chef will have to pay (A1 + A2 + A3 + B1 + B2 + B3 + C) as total for his orders.

Chef wants to know if he should buy the coupon or not. Help him find the answer for T test cases.

Note: He will buy the coupon only if he has to pay strictly less than without the coupon, i.e. if the total price that he has to pay remains the same with or without the coupon, then he will not buy the coupon and you should answer NO.

# EXPLANATION:

If (A1 + A2 + A3 + B1 + B2 + B3 + D + D) is greater than (A1 + A2 + A3 + B1 + B2 + B3 + C), then print YES.

# SOLUTION:

Setter's Solution
``````#include <stdio.h>

int main()
{
//input the no. of test cases
int t=0,d=0,c=0;
scanf("%d",&t);

while(t)
{
//input the cost of delivery charge per day
//and the coupon price
scanf("%d %d",&d,&c);

//input the price of each food item of day 1
int day1[3];
int i=0;
for(i=0;i<3;i++) scanf("%d",&day1[i]);

//input the price of each food item of day 2
int day2[3];
for(i=0;i<3;i++) scanf("%d",&day2[i]);

//total cost of order for 2 days
int c1=0,c2=0;
for(i=0;i<3;i++)
{
c1+=(day1[i]+day2[i]);
}
//when coupon purchased then total cost
c2= c1+c;
//when delivery charges applied
c1= c1+2*d;

if(c2<c1) printf("YES\n");
else printf("NO\n");

t--;
}
return 0;
}
``````

# Equal Sets

Practice

Author: Mudit Mahajan

Easy

# PROBLEM:

Chef has been given the task of dividing the numbers 1, 2, …, n into two sets such that the sum of the two sets is equal.

Help Chef determine if it is possible to divide the numbers 1, 2, …, n into two sets of equal sum or not.

# EXPLANATION:

We can divide the numbers into two equal sets if and only if the sum of the numbers up to n is even. We cannot divide them in two sets of equal sum if the sum is odd.

We can easily find the sum using the A.P. progression formula i.e., n*(n+1)/2.

# SOLUTIONS:

Setter's Solution
``````#include<iostream>
using namespace std;
int main()
{
int t;
cin >> t;
for(int tt = 0; tt < t; tt++)
{
int n;
cin >> n;
int sum = (n * (n + 1)) / 2;
if (sum % 2 == 0)
cout << "YES" <<  endl;
else
cout << "NO" << endl;
}
return 0;
}

``````

# Avatar Speed

Practice

Author: Mudit Mahajan

Medium

# PROBLEM:

Chef likes to play video games and he has set the speed of his avatar to same value m.

But he finds out that someone has changed the speed of his avatar to n. Chef is not happy about this and wants to change it back to m.

However he can change the speed of his avatar by only pressing the following buttons: (-5, -2, -1, 1, 2, 5), with which he can either increase or decrease the speed of his avatar.

Note that the speed of the avatar can never be negative, i.e., it can be zero but not less than zero.

Help Chef find the minimum number of presses required to change speed from n to m for t test cases.

# EXPLANATION:

The problem can be reduced to either always increasing or always decreasing the speed of the avatar. As a -5 and +2 can be replaced with a +2 and +1, or a +5 and -1 can be replaced with a +2 and +2.

Similarly, it can be shown that we can get to the desired speed using either only positive or only negative values.

Also, it is always better to use as many 5 as possible and only then adding the 2s and 1s to it.

After using the maximum number of 5s, we will use the maximum number of 2s from the remaining difference and then add the 1s to the answer.

# SOLUTION

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
for(int tt = 0; tt < t; tt++)
{
int n, m;
cin >> n >> m;
int temp = abs(n - m);
int ans = 0;
ans = temp / 5;
temp %= 5;
ans += temp / 2;
temp %= 2;
ans += temp;
cout << ans << endl;
}
return 0;
}

# Chef and Letters

Practice

Author: Utkarsh Bhatnagar

Medium

# PROBLEM:

The chef has N  letters with him. Each letter contains the name of the person who sent it.

He wants to choose 3 letters such that the following conditions are met:-

1. Selected letters must be from the person whose name start with A,C,M,H or R.
2. All three selected letters have to be from a different person.

How many such ways are there to choose three letters, disregarding order?
Note that the answer may not fit into a 32 -bit integer type.

# EXPLANATION:

We have to select exactly 3 letters with each have a different name on it and it should with MARCH.
So number of selection can be:
MAR,MAC,MAH,MRC,MRH,MCH,ARC,ARH,ACH,RCH.

Let’s say we have:

• a persons with the name starting with A.
• c persons with the name starting with C.
• h persons with the name starting with H.
• m persons with the name starting with M.
• r persons with the name starting with R.

So the take number of ways for formation of ACH is (ac1* cc1*hc1).

Similarly for all the combinations.

So how to implement it:

It can be implemented using bitmasking.

So ACHMR can be represented as binary 11111 in which 1 means the occurrence of each character.

Since we have to select only 3 person, so only 3 bits must be 1(on) in 11111.

So ACH is represented by 11100, MAR 10011, and so on.Any of the above listed pattern can be represented as binary form.

So 0 to 31 (00000->11111) contain all the pattern listed(if we represent them in binary form) above but we only want the binary number with 3 bits on(1).We can do that __builtin_popcount() it counts number of on(1) bits in the number.
eg:
ACH=11100
MAR=10011
AHR=10101
CMR=01011

OR
2nd Apporach

If you find above apporach difficult.

When choosing 3 people,there are no more than 10 patterns (as listed above).

So,it is OK to calculate all the pattern and try all 10 patterns.

# SOLUTIONS:

Setter's Solution
``````//Using bitmasking

#include <bits/stdc++.h>
using namespace std;
#define  ll       long long
#define  FIO      ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

int main()
{
FIO;
int n;
cin >> n;
int a[5] = {0};
string s;
for (int i = 0; i < n; i++) {
cin >> s;
if (s[0] == 'M')
a[4]++;
else if (s[0] == 'A')
a[3]++;
else if (s[0] == 'R')
a[2]++;
else if (s[0] == 'C')
a[1]++;
else if (s[0] == 'H')
a[0]++;
}

ll mul = 1, sum = 0;
//here i am representing 11111=MARCH

for (int i = 0; i < 31; i++) {
//count the bit here if they are equal to 3 than perform operation
int k = __builtin_popcount(i);
mul = 1;
if (k == 3) {
for (int j = 0; j <= 4; j++) {
if (i & (1 << j)) {
mul = mul * 1ll * a[j];
}
}
sum += mul;
}
}
cout << sum;
return 0;
}
``````
Setter's Solution
``````//2nd approach

# include < cstdio >
# include < iostream >
using namespace std ;
typedef long long ll ;

string s ;
int N ;
ll m ,a ,r ,c , h ;
ll D [5];
int P [10]={0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,1 ,2};
int Q [10]={1 ,1 ,1 ,2 ,2 ,3 ,2 ,2 ,3 ,3};
int R [10]={2 ,3 ,4 ,3 ,4 ,4 ,3 ,4 ,4 ,4};

int main ()
{
scanf ("% d " ,& N );

for ( int i =0; i < N ; i ++)
{
cin > > s ;
if ( s [0]== ’ M ’) m ++;
if ( s [0]== ’ A ’) a ++;
if ( s [0]== ’ R ’) r ++;
if ( s [0]== ’ C ’) c ++;
if ( s [0]== ’ H ’) h ++;
}

D [0]= m , D [1]= a , D [2]= r , D [3]= c , D [4]= h ;
ll res =0;
for ( int d =0; d <10; d ++){
res += D [ P [ d ]]* D [ Q [ d ]]* D [ R [ d ]];
}
printf ("% lld \ n " , res );
}

``````