Thursday, 20 October 2016

Problem : Cats And Dogs


                                   Problem : Cats And Dogs



Cats & Dogs have become friends now. They are going to a picnic and they decided to sit together in groups to have their lunch.

There are N Cats & M Dogs. They can form any number of groups to sit together, subject to rules below. 

Rules:
  1. They will sit in a straight line
  2. A Cat cannot sit beside another Cat or alone
  3. A Dog cannot sit beside another Dog or alone


You need to find minimum number of groups they need to form. If no group can be formed then print -1.

Input Format:

First line of input will be a number of Test Cases T

Next T lines of input will contains two numbers N and M, delimited by space
Output Format:

Print minimum number of groups to be formed for each Test Case in new line
OR

Print -1 if no group can be formed
Constraints:
  1. 0<T<=10^6
  2. 0<N,M<=10^18

Example:

EXAMPLE NUMBERSAMPLE INPUTSAMPLE OUTPUT
1
2
5 5
1 3

1
-1
2
3
7 8
3 5
6 7

1
2
1

Solution:-
#include<iostream>
using namespace std; 
int main()
{
int T,N,M;
cout<<"Enter No of Test case: ";
cin>>T;
for(int i=0;i<T;i++)
{
cout<<"\nEnter cat and dog :";
cin>>N>>M;
if(M/2>N||N/2>M||M<1||N<1)
cout<<"Answer : -1";
else if(M==N)
cout<<"Answer : 1";
else
{
int temp;
if(M>N)
{
temp=M-N-1;
cout<<"Answer : "<<temp+1;
}
else
{
temp=N-M-1;
cout<<"Answer : "<<temp+1;
}
}
}
}

No comments:

Post a Comment