https://www.codechef.com/JUNE20B/problems/EVENM

I have written a solution which is divided into two parts

  1. One is for when N is odd which is essentially just filling the matrix with consecutive numbers 1 to N^2
  2. When N is even I have just generated a spiral matrix that goes in the clockwise direction.

Here is the solution https://www.codechef.com/viewsolution/34390953

I had also written a function to check my solution against the conditions given.

private static boolean checkMatrix(int[][] matrix) {

    int a = 0;
    int n = matrix.length;
    for (; a < n; a++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                 if( !(j + a >= n || k + a >= n) ) {
                     if((matrix[j][k] + matrix[j+a][k+a]) % 2 == 1) {
                         return false;
                     }else if((matrix[j+a][k] + matrix[j][k+a]) % 2 == 1) {
                         return false;
                     }
                 }
            }
            
        }

    }

    return true;

}

Can someone please help me understand the flaws in my logic :slight_smile:

Your logic is perfectly alright.There is nothing wrong with the logic, seems fine.
Ac. to me , something wrong with accessing the input or printing the output you can check that yourself as i m not familiar with java.
PS: I took your code and submitted in c++ . Surprisingly it got an AC answer. Have a look. CodeChef: Practical coding for everyone

Thanks me after,

1 Like

It’s hard to debug this. I’d say change your approach,
My AC code:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector <vector <int>> v(n);
        long long curr = 1;
        int state = 0;
        for (int i = 0; i < n; ++i) v[i].resize(n);
        for (int i = 0; i < n; ++i, state ^= 1)
        {
            for (int j = 0; j < n; ++j, ++curr) v[i][j] = curr;
            if (state) reverse(v[i].begin(), v[i].end());
            for (int j = 0; j < n; ++j) cout << v[i][j] << " ";
            cout << '\n';
        }
    }
}

When state is 0, the row is printed normally. If it is 1, it is reversed.

check this out

1 Like

Thanks :). Will look into it

Yeah your code is pretty concise.

:+1:

Hey, thanks techno_phyle for putting in the effort to use my logic. Just wanted to confirm that it is correct

My logic was correct there was an extra println with size of the matrix printed. So WA

Pretty Simple

#include <bits/stdc++.h>
using namespace std;
int main(){
   int t;
   cin>>t;
   int n;
   while(t--){
      cin>>n;
      int count=1,turn=1;
      for(int i=0;i<n;i++,turn=1-turn){
         if(turn)
         for(int j=0;j<n;j++){
            cout<<count++;
            if(j!=n-1)cout<<" ";
         }
         else{
            count+=n;
            for(int j=0;j<n;j++){
               cout<<count-j-1;
               if(j!=n-1)cout<<" ";
            }
         }
         cout<<"\n";
      }
   }
   return 0;
}

You can check my code implementation in python. It’s concise and simple to understand.

I=lambda:int(input())
def mainFunction():
    	testCase=I()
    	for t in range(testCase):
    	    n=I()
    	    count = 1
    	    for i in range(n):
    	        matRow = '' 
    	        if i % 2 == 0:
    	            for j in range(n):
    	                matRow = matRow +str(count)+" "
    	                count=count+1
    	        else:
    	            count = count + n-1
    	            for j in range(n):
    	                matRow = matRow +str(count)+" "
    	                count=count-1
    	            count = count + n+1
    	        print(matRow)

    mainFunction()

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