CodeChef 

Cats and Dogs

(CATSDOGS)

Problem Description

C: Number of cats in the barn

D: Number of Dogs in the barn

 

Problem Description

C: Number of cats in the barn

D: Number of Dogs in the barn

 

NOTE: There can be at most 2 cats upon the one dog

L: Number of legs as seen by the chef touching the ground

Problem Description

For Example 

cats = 1

dogs = 1

legs = 8

Both pets are on the Ground

Problem Description

Example 1:

cats = 1

dogs = 1

legs = 8

Example 2:

cats = 1

dogs = 1

legs = 4

The cat is upon the dog.

Problem Description

Example 1:

cats = 1

dogs = 1

legs = 8

Example 2:

cats = 1

dogs = 1

legs = 4

Example 3:

cats = 1

dogs = 1

legs = 2

Solution 

CASE 1

Legs must always be the multiple of 4. 

legs\ =\ 4*n\ for\ n\ =\ 0,1,2,3...

Solution 

CASE 1

Legs must always be the multiple of 4. 

legs\ =\ 4*n\ for\ n\ =\ 0,1,2,3...
Constraints\newline 1 ≤ T ≤ 10^5\newline 0 ≤ C, D, L ≤ 10^9

Solution 

CASE 2

Legs reported by chef cannot be greater than the total legs of dogs and cats altogether. 

Solution 

CASE 2

Legs reported by chef cannot be greater than the total legs of dogs and cats altogether. 

legs\ <=\ 4*(cats + dogs)

Solution 

CASE 3

legs must be greater than or equal to total legs of dogs

Solution 

CASE 3

legs must be greater than or equal to total legs of dogs

legs\ >=\ 4*dogs

Solution 

CASE 3

legs must be greater than or equal to total legs of dogs

legs\ >=\ 4*dogs

Legs

0

Total Legs

legs of dogs & cats in the ground

legs of cats riding on the dogs

Solution 

CASE 3

legs must be greater than or equal to total legs of dogs

legs\ >=\ 4*dogs

Legs

0

Total Legs

legs of dogs 

legs of cats riding on the dogs

legs of cats on the ground

remaining cats

Solution 

CASE 3

legs must be greater than or equal to total legs of dogs

legs\ >=\ 4*dogs

Legs

0

Total Legs

legs of dogs 

legs of cats riding on the dogs

legs of cats on the ground

cats\ <=\ 2*dogs

remaining cats

Let's Code it!



Let Us Code it in 

C++




Solution 

         1        2        3        4         

         1       2       3      4         

Solution 

         1        2        3        4         

         1       2       3      4         

2 OK

Solution 

         1        2        3        4         

         1       2       3      4         

2 OK

Solution 

         1        2        3        4         

         1       2       3      4         

2 OK

Solution 

         1        2        3        4         

         1       2       3      4         

1 ODD

It means x coordinate of missing point is 4. Now to get y let's move in y direction.

Solution 

         1        2        3        4         

         1       2       3      4         

4 OK

Missing Point = (4, )

Solution 

         1        2        3        4         

         1       2       3      4         

2 OK

Missing Point = (4, )

Solution 

         1        2        3        4         

         1       2       3      4         

1 ODD

Missing Point = (4,3)

Code

#include <bits/stdc++.h>

using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out1.txt", "w", stdout);
	#endif
	int t;
	cin >> t;
	while(t--)
	{
		int n;
		cin >> n;
		map<long long int, long long int> m1;
		map<long long int, long long int> m2;
		for(int i =0; i<4*n-1; i++)
		{
           long long int a,b;
           cin >> a >> b;
           m1[a]++;
           m2[b]++;
		}
		long long int ii;
		long long int jj;
		
		for(auto i : m1)
		{
			if(i.second%2!=0)
				ii = i.first;
		}
		for(auto j : m2)
		{
			if(j.second%2!=0)
				jj = j.first;
		}
		cout << ii << " " << jj << endl;
	}


return 0;
}

Any Doubts

If you still have any doubts you can comment your doubts in the comment section.

If you have a better approach than this you can also comment it out.

Like, Share, Subscribe!

If you like our efforts Please Do Like this video, share it with your friends who was not able to solve this problem.

Also Do subscribe this channel because more video Editorials and coding related content is coming on this channel.

Like, Share, Subscribe!

Made with Slides.com