Codechef Cook off

May Cook Off - 2020

Codechef Cook off

May Cook Off - 2020

Impressing Chefina

Problem Statement In Simple Language

You are given nXm matrix. 

Problem Statement In Simple Language

You are given nXm matrix. 

Chef travels the matrix left to right and then right to left from top left corner.

Problem Statement In Simple Language

You are given nXm matrix. 

Chef travels the matrix left to right and then right to left from top left corner.

Chefina travels the matrix from right to left and then left to right from top right corner.

Problem Statement In Simple Language

You are given nXm matrix. 

Chef travels the matrix left to right and then right to left from top left corner.

Chefina travels the matrix from right to left and then left to right from top right corner.

Chef can rearrange the matrix.

Problem Statement In Simple Language

You are given nXm matrix. 

Chef travels the matrix left to right and then right to left from top left corner.

Chefina travels the matrix from right to left and then left to right from top right corner.

Chef can rearrange the matrix.

AIM :  To check if chef and chefina can have equal matrix, if so print that matrix.

Understanding the concept..!!

Understanding the concept..!!

Let's think how two opposite zig zag traversal can be equal????

Understanding the concept..!!

Let's think how two opposite zig zag traversal can be equal????

                1               2            3

Is right to left and left to right traversal equal?

Understanding the concept..!!

Let's think how two opposite zig zag traversal can be equal????

                1               2            3

Is right to left and left to right traversal equal?

NO

Understanding the concept..!!

Let's think how two opposite zig zag traversal can be equal????

                1               2            3
               1             2          1

Is right to left and left to right traversal equal?

Understanding the concept..!!

Let's think how two opposite zig zag traversal can be equal????

                1               2            3
               1             2          1

Is right to left and left to right traversal equal?

Yes!! In this case both traversals are equal..!!

Observation that we have is

The sequences must be palindromic to fulfill the conditions demanded in the problem..!!

Observation that we have is

The sequences must be palindromic to fulfill the conditions demanded in the problem..!!

Hurray..!! We are one step closer to the problem..!!

Now we have two task to complete the question

Now we have two task to complete the question

1. Is it possible to form a matrix with the same elements in the given matrix such that every row is a palindrome.

Now we have two task to complete the question

1. Is it possible to form a matrix with the same elements in the given matrix such that every row is a palindrome.

2. If the answer to the previous condition is "YES" then print the solution matrix.!!

Solving the first condion..!!

To check wether we can form the palindromic matrix or not..!!

Solving the first condion..!!

To check wether we can form the palindromic matrix or not..!!

It is better to analyse on which conditions we cannot form the matrix

The prime step is to calculate frquencies of all the numbers in the matrix..!!

This will help us to check wether we can make palindrome or not..!!

The prime step is to calculate frquencies of all the numbers in the matrix..!!

This will help us to check wether we can make palindrome or not..!!

We can make a map<int,int> to store (key,value) pair of number and its frequency..!!

To judge the conditions in which we cannot form palindromic matrix is:

To judge the conditions in which we cannot form palindromic matrix is:

1. If the number of colums are even then the number with odd frequencies must be 0.

Reason : Because any number with odd frequency cannot form a palindromic row

Let us suppose we have 3X4 matrix and freq[2] = 1

            2

Let us suppose we have 3X4 matrix and freq[2] = 1

No we can not form palindrome..!!

            
          2

Let us suppose we have 3X4 matrix and freq[2] = 1

No we can not form palindrome..!!

            
            2         2           2

Let us suppose we have 3X4 matrix and freq[2] = 3

No we can not form palindrome..!!

Hence this condition will hold true..!!

Now let's see what happen when we have odd number of columns

Hence this condition will hold true..!!

Now let's see what happen when we have odd number of columns

The observation that  we can make is the if the count of odd frequency numbers is greater than number of rows than we cannot form the matrix.

        1
        2
        3

If the number of odd frequency values are greater than 3 let us suppose 4

        1
        2
        3

If the number of odd frequency values are greater than 3 let us suppose 4

So we cannot fit all these values in the middle column and hence matrix cannot be formed

Summarizing the conditions..!!!

1. If the number of columns are even then odd frequency numbers must be 0.

2. If the number of columns are odd then odd frequency numbers must be <= number of rows

Summarizing the conditions..!!!

1. If the number of columns are even then odd frequency numbers must be 0.

2. If the number of columns are odd then odd frequency numbers must be <= number of rows

If these conditions do not fulfill then print -1.

TASK 2

How to print the matrix ??

TASK 2

How to print the matrix ??

Its very easy 3 step process..!!

Since we have the frequencies stored in the frequency array..!!

We will make the use of it..!!

Step 1 :

Make a seperate vector of numbers with odd frquency and subtract one from their frequency.

Step 1 :

Make a seperate vector of numbers with odd frquency and subtract one from their frequency.

Reason : We subtract one because it will make their frequency even and we will make the palindrome possible by making one value in front and other from back.

The frequency which we have subtracted will be used in middle row..!! If the middle row exist in odd column cases..!!

Step 1 :

Start filling the matrix from the front and the back .. Lets see how we can do that..!!

Step 1 :

Start filling the matrix from the front and the back .. Lets see how we can do that..!!

auto it = freq.begin();
		for(int  i =0; i<n; i++)
		{
			for(int  j =0; j<m/2; j++)
			{
				if(j!=m-j-1)
				{
					while(it->second == 0)
						it++;

					arr[i][j] = it->first;
					arr[i][m-j-1] = it->first;
					it->second =  it->second-2;
				}
			}
		}

Step 2 :

The case in which odd frequency is less than n then what we will place at remaining values in the middle row?

Step 2 :

The case in which odd frequency is less than n then what we will place at remaining values in the middle row?

The answer is simple..!!

The remaing values with even frequencies..!!

Step 2 :

The answer is simple..!!

The remaing values with even frequencies..!!

for(auto it: freq)
{
	if(it.second!=0)
	{
		for(int j = 0; j<it.second; j++)
			odd.push_back(it.first);
	}
}

Step 3 :

Fill the values in the midlle row in case when number of columns is odd..!!

Step 3 :

Fill the values in the midlle row in case when number of columns is odd..!!

if(m%2==1)
{
	for(int  i =0; i<n; i++)
		arr[i][m/2] = odd[i];
}

And at last print the matrix

And at last print the matrix

We are done with problem..!!

For full solution check the description and do comment for any doubts..!!

Links to more content is in the description..!!

Made with Slides.com