CATSDOGS - Editorial

@mihir1440 Your code throws out of index error whenever T=1

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
	at Catsdogs.main(Main.java:14)

I strongly suspect that it is due to wrong allocation of memory to cnt[][]. Please have a look at it.

What I’m doing is this:

  1. Calculate maximum value of legs possible which is max = 4*(c+d) when all cats and dogs are on the floor.
  2. Calculate minimum value of legs possible which is min = 4*(c-d) when max cats are on top of dogs.
  3. Number of legs should be positive multiples of 4.
  4. Number of legs should be between max and min.

To my surprise, it doesn’t work. It gives a straight WA.

Where did I go wrong?

long long int t;
scanf("%lld", &t);

while(t--)
{
    long long int c, d, l;
    scanf("%lld %lld %lld", &c, &d, &l);

    long long int max = (c+d)*4;
    long long int min = (c-d)*4;

    if(l >= min && l <= max && l%4 == 0 && l >= 4)
        printf("yes\n");
    else
        printf("no\n");
}
return 0;

}

Any help?

PS: It works fine with the given test case!

@mr_nair

Your calculation of min is only partially correct.

Its correct if number of cats are way more than number of dogs, but what if number of cats are less than number of dogs?

In a scenario when all cats can be on top of dogs, the min is not 4x(c-d), but its simply 4xd. Visualise it, when all cats on top of dogs, minimum legs is equal to number legs of dogs.

Your case fails at this test case-

Input
1
1 3 4
Output
yes
Expected output
no

Your min here is 4x(1-3) = -8. While practically we can see that min should be 4*3=12.

Can someone tell why i am failing my testcases

O(1) Solution:

#include <bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t--){
	long long c,d,l,maxlegs,minlegs,cog,flag=0;
	cin>>c>>d>>l;
	if(l%4==0){
		maxlegs=(c+d)*4;
		if(c>2*d){
			cog=c-2*d;
		}
		else cog=0;
		minlegs=(d+cog)*4;
		
		if(l>=minlegs && l<=maxlegs) flag=1;	
	}
	else flag=0;
	
	if(flag) cout<<"yes"<<endl;
	else cout<<"no"<<endl;
}
return 0;
}

Have a look at my solution https://www.codechef.com/viewsolution/17555935
It is self explanatory .

1 Like

What’s wrong in this code? https://www.codechef.com/viewsolution/18693946
Only 30%?

What’s wrong in this code? https://www.codechef.com/viewsolution/18693946

Only 30%?

what is wrong in my code .I followed editorial

https://www.codechef.com/viewsolution/21711700

Please help

1 Like

You’ve calculated wrong min, learn to debug your code.
ll mnl=d4;
if(c>d
2)
mnl=(ll)((ll)(c-(ll)(d*2))*4 + mnl);

Not sure why it posted in such a weird format sorry about that

nevermind I got it

Describe what you think the following input means:

1
5 5 0

and why you should not allow this as a valid situation.

Simple python Implementation.
First check if we have query between max and min number of legs possible.
Then just check if legs are multiple of 4.

t = int(input())
while(t>0):
    cats, dogs, legs = map(int, input().split())
    max_legs = (dogs + cats) * 4

if((2 * dogs) >= cats):
    min_legs = 4 * dogs

else:
    min_legs = (cats - (2*dogs)) * 4 + dogs * 4

# print(min_legs, max_legs)

if(legs < min_legs or legs > max_legs):
    print("no")

else:
    if(legs % 4 == 0):
        print("yes")
    else:
        print("no")
t -= 1

https://www.codechef.com/viewsolution/33135232
can someone pls tell why it is giving WA

wrongly consider the input order
this caused me 1 hr :frowning_face:

#include
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;

while(t--)
{
    int c,d,l;
    cin>>c>>d>>l;
    
    if(c*4 + d*4 == l)
    {
        cout<<"yes"<<endl;
    }
    else if(((c*4)-4)  + d*4 == l )
    {
        cout<<"yes"<<endl;
    }
    else if(((c*4)-8)  + d*4 == l )
    {
        cout<<"yes"<<endl;
    }
    else {
        cout<<"no"<<endl;
    }
}
return 0;

}

This code gets me 0 marks can anyone look into it ??
I have gone for 3 cases
case1: No cats on back
case2: 1 cat on back
case3: 2 cats on back

plz help me.

#include
using namespace std;

int main() {
int t;cin>>t;

while(t--)
{
int c,d,l;cin>>c>>d>>l;
int s=(c+d)*4;

if(s==l || s/2==l)
cout<<"yes"<<endl;

else
cout<<"no"<<endl;

}
}

it does not works for the input 1 1 2
think !
once try reading editorial’s logic, its easy to grasp

consider this case as well
case 4: 1 cat on back of some dogs && 2 cats on the back of some other dogs
like this there are many other case.

In this case, the logic described in editorial will output “no”
the code will work on invalid case like these as well