Can You Eat It Problem Code: COMPCAND
Problem Link : https://www.codechef.com/FZBZ21C/problems/COMPCAND
You are a person who is always fond of eating candies. Your friend gave you a candy of length N, to eat during the break period of your school.
You start eating this candy from one of the ends. But as it is not your candy, your friend told you to eat exactly K unit length of candy during each bite. You will stop eating if the candy's length becomes 0. This means that you have eaten the entire candy.
If at some point of time, the candy's length is positive, but less than K, you cannot take a bite thereafter.
Can you eat the complete candy? If yes, print the number bites it will take, else print −1.
Input Format
First line will contain T, number of testcases. Then the testcases follow.
Each testcase contains of two spaced integers N, K.
Output Format
For each testcase, print the minimum number of bites you need to eat the complete candy. Print −1 if it is not possible to do so.
Constraints
1≤T≤105
0≤N≤106
1≤K≤106
Sample Input 1
3
3 1
3 2
0 3
Sample Output 1
3
-1
0
Explanation
Test case 1: When you eat your first bite of length 1, the length of the candy will become 2. After the second bite, the candy's length will be 1 and finally after the third bite, candy's length will be 0, and hence you eat the entire candy!
Test case 2: After the first bite, candy's length will become 1. You cannot go for the second bite as candy's length is positive and less than 2.
Test case 3: Candy's length is already 0, hence it can be said that you have eaten the entire candy!
Solution :
C++
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a, b, c;
cin>>a>>b;
if(a%b==0)
{
c = a / b;
cout<<c<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
return 0;
}