Minimum number of coins Problem Code: MINCOINSSubmit (Practice)
Chef has infinite coins in denominations of rupees and rupees .
Find the minimum number of coins Chef needs, to pay exactly rupees. If it is impossible to pay rupees in denominations of rupees and only, print .
Input Format
- First line will contain , number of test cases. Then the test cases follow.
- Each test case contains of a single integer .
Output Format
For each test case, print a single integer - the minimum number of coins Chef needs, to pay exactly rupees. If it is impossible to pay rupees in denominations of rupees and only, print .
Constraints
Subtasks
- Subtask 1 (100 points): Original constraints.
Sample Input 1
3
50
15
8
Sample Output 1
5
2
-1
Explanation
Test Case : Chef would require at least coins to pay rupees. All these coins would be of rupees .
Test Case : Chef would require at least coins to pay rupees. Out of these, coin would be of rupees and coin would be of rupees .
Test Case : Chef cannot pay exactly rupees in denominations of rupees and only.
solution
#include<iostream>
using namespace std;
class solution
{
public:
void solve()
{
int x;
cin >> x;
if (x % 5 != 0)
cout << -1 << "\n";
else
{
int r5, r10, rem;
r5 = x / 5;
r10 = r5 / 2;
rem = r5 % 2;
int ans = r10 + rem;
cout << ans << "\n";
}
}
};
int main()
{
solution ss;
int t;
cin >> t;
while (t--)
{
ss.solve();
}
return 0;
}
No comments:
Post a Comment