IIT Kanpur(Let us Play with even and odd numbers)

Can anyone please see my solution http://www.codechef.com/viewsolution/5950794 for the question http://www.codechef.com/WPC1501/problems/IITK2P06/ and tell in which particular type of test cases my solution is failing?

Hello Saikat ,

I was looking at your code … it is too long to check … btw this problem is trivial and this is my solution which is the simplest one …


#include 
using namespace std ;
 
int T,E,O ;
 
int main(){
 
	cin >> T ;
	while(T--){
		cin >> E >> O ;
		if((E+O)%5){
			cout << -1 << endl ;
			continue ;
		}else{
			int sum = (E+O)/5 ;
			E -= 2*sum ;
			O -= 3*sum ;
			cout << max(E,O) << endl ; 
		}
	}
	return 0 ;
} 

Why are you applying efforts in making it too complex ?

1 Like

See this test case:

1
100000000 1000000000

Your code gives TLE for that test case. There must be some mistake in your logic which you can easily figure out (there may be an infinite loop somewhere) or you are doing too many computations. ALso, the problem doesnot need so much of workout, you can easily solve this way:

Lets say we have done k operations, then the ratio of even to odd numbers will be:

(e-k)/(o+k) or (e+k)/(o-k) 

Any of the above two ratios should be 2:3 for our requirement. Just find out k from the two equations:

(e-k) = 2 or (e+k) = 2
-----   -    -----   -
(o+k)   3    (o-k)   3

Solve the above two equations, and with the added constraint that k should be some integer, we get:

k= 2*o - 3*e   or k = 3*e - 2*o
   ---------          ----------
       5                   5

Just check this and you’ll get an AC. My code,

cin>>n1>>n2;
k= 3*n1 - 2*n2;
if(k>=0 && k%5==0)
{
	k= k/5;
	cout<<k<<endl;
}
else
{
	k= 2*n2- 3*n1;
	if(k>=0 && k%5==0)
	{
		k=k/5;
		cout<<k<<endl;
	}
	else
	cout<<"-1"<<endl;
}
1 Like

Thanks :)… I did the same later… but still cannot understand the error in my first trial. :\

Thanks for your explannation :slight_smile:

Happy to help…you may like closing the thread after accepting the correct answer which satisfies you…