Difference Matrix | starter 82 | codechef

Problem

You are given an integer �N. You need to find an �×�N×N matrix such that:

  • Each element of the matrix is an integer from 11 to �2N2;
  • All elements of the matrix are unique;
  • The absolute difference between elements in neighbouring cells is strictly greater than 11.

It can be shown that there is at least one matrix that satisfies the given conditions. If multiple matrices satisfy the given conditions, print any.

Note that two cells are considered to be neighbours if they have a common side or a common vertex (i.e. a cell can have at most 88 neighbouring cells).

Input Format

  • The first line of input will contain a single integer �T, denoting the number of test cases.
  • Each test case contains a single line of input, an integer �N, denoting the number of rows and columns in the required matrix.

Output Format

For each test case, output �N lines, where the ��ℎith line contains �N space-separated integers, denoting the elements of the ��ℎith row.

Constraints

  • 1≤�≤10001≤T≤1000
  • 4≤�≤10004≤N≤1000
  • The sum of �2N2 over all test cases won’t exceed 106106.

Sample 1:

Input

Output

1
4
12  10  6   14
3   8   4   1
5   15  11  9
2   7   13  16

Explanation:

Test case 11: The given matrix satisfies all the conditions.91d04e4e-6629-4f0d-a880-044ece9ca436.png

Note that all elements are unique and in the range [1,�2][1,N2]. Also, the absolute difference between any two neighbouring cells is greater than 11.

acceptedAccepted

4

total-SubmissionsSubmissions

11

accuracyAccuracy

36.36

Click on below image to get the solution link:

Leave a Reply

Your email address will not be published. Required fields are marked *

मिर्जापुर 3 के बोनस एपिसोड में मुन्ना भैया की वापसी? आपकी साँसे थम जाएंगी! ये है आपके PAN CARD की एक्सपायरी डेट, कहीं छूट तो नहीं गई? यकीन नहीं मानोगे! ये 9 जगहें हैं UP में जो ताजमहल को भी फीका कर देंगी! मेरठ: इतिहास, धर्म और खूबसूरती का संगम! घूमने के लिए ये हैं बेहतरीन जगहें रोज आंवला खाने के 10 धांसू फायदे जो आपको कर देंगे हेल्दी और फिट! Anjali Arora to play Maa Sita: रामायण फिल्म में सीता का रोल निभाएंगी अंजली अरोड़ा, तैयारियों में लगी?