PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4
Setter: Lavish Gupta
Tester: Abhinav Sharma, Aryan
Editorialist: Lavish Gupta
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
Chef is playing a variant of Blackjack, where 3 numbers are drawn and each number lies between 1 and 10 (with both 1 and 10 inclusive). Chef wins the game when the sum of these 3 numbers is exactly 21.
Given the first two numbers A and B, that have been drawn by Chef, what should be 3-rd number that should be drawn by the Chef in order to win the game?
Note that it is possible that Chef cannot win the game, no matter what is the 3-rd number. In such cases, report -1 as the answer.
EXPLANATION:
Let us represent the 3-rd number that should be drawn by Chef by C. The following condition needs to be satisfied: A+B+C = 21.
This can be rewritten as C = 21 - (A+B). However, note that the 3-rd number can only lie between 1 and 10 (inclusive).
Hence, after getting the above value of C, we need to check if the resulting C is between 1 and 10. If Yes, we have got our answer as C, otherwise the answer is -1.
TIME COMPLEXITY:
O(1) for each test case.
SOLUTION:
Editorialist's Solution
#include<bits/stdc++.h>
#define ll long long
using namespace std ;
int main()
{
ll t;
cin >> t ;
while(t--)
{
ll a , b ;
cin >> a >> b ;
ll ans = (21 - (a+b)) ;
if(ans >= 1 && ans <= 10)
cout << ans << '\n' ;
else
cout << -1 << '\n' ;
}
return 0;
}