`{"id":"d4812221-27e7-4189-817f-ae6662205924","name":"The State Of Wakanda - 2","description":"The historic state of Wakanda has various monuments and souveniers which are visited by many travellers every day. The guides follow a prescribed route of visiting the monuments which improves them understand the relevance of each monument. The route of the monument is fixed and expressed in a 2-d matrix where the travellers visit the prescribed next monument. For example\r\n1 2 3\r\n4 5 6\r\n7 8 9\r\nis the prescribed route and the visitors travels this path: 1->2->3->4->5->6->7->8->9\r\nHowever, a certain visitor decides to travel a different path as follows:\r\n1. The visitor only plans to visit the upper diagonal triangle of the monument list.\r\n2. The visitor travels diagonally till there are no more moves left in the current journey. \r\n3. He then visits the adjacent monument to the first monument of current diagonal journey. \r\n4. He continues the same path till all the monuments of the upper half have been travelled.\r\nFor Example:\r\nThe monuments are named as:\r\n1 2 3 4\r\n5 6 7 8\r\n9 10 11 12\r\n13 14 15 16\r\nThe path followed by the visitor is: 1->6->11->16->2->7->12->3->8->4\r\nYou are required to print the path followed by the traveller to visit all the monuments.\r\nRefer to the photo for a better clarification.\r\n1. You are given a number n, representing the number of rows and columns of a square matrix.\r\n2. You are given n * n numbers, representing elements of 2d array a.\r\n3. You are required to diagonally traverse the upper half of the matrix and print the contents.\r\nFor details check image.\r\n\r\n<img src=\"http://pepcoding.com/resources/ojquestionresource/images/diagonal-traversal.jpg\" alt=\"diagonal-traversal\">","inputFormat":"A number n\r\ne11\r\ne12..\r\ne21\r\ne22..\r\n.. n * n number of elements of array a","outputFormat":"Diagonal traversal as in image below","constraints":"1 &lt;= n &lt;= 10^2\r\n-10^9 &lt;= e11, e12, .. n * m elements &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nvoid upperDiagonal_traverse(int n, vector<vector<int>> &arr)\n{\n // write your code here\n}\n\nint main()\n{\n int n;\n cin >> n;\n vector<vector<int>> arr(n, vector<int>(n));\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n cin >> arr[i][j];\n \n upperDiagonal_traverse(n, arr);\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def diagonal(arr , n):\n #write your code here\n\n \n \nn= int(input())\narr = []\nfor i in range(n): \n a=[];\n for j in range(n):\n a.append(int(input()));\n arr.append(a);\n\ndiagonal(arr , n)"}},"points":10,"difficulty":"easy","sampleInput":"4\r\n11\r\n12\r\n13\r\n14\r\n21\r\n22\r\n23\r\n24\r\n31\r\n32\r\n33\r\n34\r\n41\r\n42\r\n43\r\n44","sampleOutput":"11\r\n22\r\n33\r\n44\r\n12\r\n23\r\n34\r\n13\r\n24\r\n14","questionVideo":"https://www.youtube.com/embed/rydBe9U-RtY","hints":[],"associated":[{"id":"ca862c99-591e-4766-9547-802bea38705b","name":"(sow) Will the difference between j-th column and i-th row will be constant and unique to a particular diagonal?","slug":"sow-will-the-difference-between-j-th-column-and-i-th-row-will-be-constant-and-unique-to-a-particular-diagonal","type":4},{"id":"ce2545f9-32db-4b89-a011-71f95edee3d7","name":"(sow) If n*n is the size of matrix, then how namy no. of diagonals needed to be printed ?","slug":"sow-if-n-n-is-the-size-of-matrix-then-how-namy-no-of-diagonals-needed-to-be-printed","type":4},{"id":"f92dc867-7803-48e0-9434-7ce7262485b7","name":"(sow2) If we represent an element using this notation of {i, j} it means an element of the i-th row and j-th column. If this element belongs to a diagonal then the next element in the diagonal will be __?__ And the previous element was __?__.","slug":"sow2-if-we-represent-an-element-using-this-notation-of-i-j-it-means-an-element-of-the-i-th-row-and-j-th-column-if-this-element-belongs-to-a-diagonal-then-the-next-element-in-the-diagonal-will-be-and-the-previous-element-was","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f185374c-5448-476e-93e8-c4a4e9d1d520","name":"2D Arrays","slug":"2d-arrays-9999","type":0},{"id":"1048caa6-2f2f-43dc-9caf-7d669fb7de83","name":"The State Of Wakanda - 2","slug":"the-state-of-wakanda-2","type":1}],"next":{"id":"36f65bf4-d6d6-492c-9c0a-a837e3ad5698","name":"The State of Wakanda - 2","type":3,"slug":"the-state-of-wakanda-2"},"prev":{"id":"3e2481e7-d0e2-4740-92d8-e78255367492","name":"Ring Rotate","type":3,"slug":"ring-rotate"}}}`

# The State Of Wakanda - 2

The historic state of Wakanda has various monuments and souveniers which are visited by many travellers every day. The guides follow a prescribed route of visiting the monuments which improves them understand the relevance of each monument. The route of the monument is fixed and expressed in a 2-d matrix where the travellers visit the prescribed next monument. For example 1 2 3 4 5 6 7 8 9 is the prescribed route and the visitors travels this path: 1->2->3->4->5->6->7->8->9 However, a certain visitor decides to travel a different path as follows: 1. The visitor only plans to visit the upper diagonal triangle of the monument list. 2. The visitor travels diagonally till there are no more moves left in the current journey. 3. He then visits the adjacent monument to the first monument of current diagonal journey. 4. He continues the same path till all the monuments of the upper half have been travelled. For Example: The monuments are named as: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 The path followed by the visitor is: 1->6->11->16->2->7->12->3->8->4 You are required to print the path followed by the traveller to visit all the monuments. Refer to the photo for a better clarification. 1. You are given a number n, representing the number of rows and columns of a square matrix. 2. You are given n * n numbers, representing elements of 2d array a. 3. You are required to diagonally traverse the upper half of the matrix and print the contents. For details check image. <img src="http://pepcoding.com/resources/ojquestionresource/images/diagonal-traversal.jpg" alt="diagonal-traversal">

`{"id":"d4812221-27e7-4189-817f-ae6662205924","name":"The State Of Wakanda - 2","description":"The historic state of Wakanda has various monuments and souveniers which are visited by many travellers every day. The guides follow a prescribed route of visiting the monuments which improves them understand the relevance of each monument. The route of the monument is fixed and expressed in a 2-d matrix where the travellers visit the prescribed next monument. For example\r\n1 2 3\r\n4 5 6\r\n7 8 9\r\nis the prescribed route and the visitors travels this path: 1->2->3->4->5->6->7->8->9\r\nHowever, a certain visitor decides to travel a different path as follows:\r\n1. The visitor only plans to visit the upper diagonal triangle of the monument list.\r\n2. The visitor travels diagonally till there are no more moves left in the current journey. \r\n3. He then visits the adjacent monument to the first monument of current diagonal journey. \r\n4. He continues the same path till all the monuments of the upper half have been travelled.\r\nFor Example:\r\nThe monuments are named as:\r\n1 2 3 4\r\n5 6 7 8\r\n9 10 11 12\r\n13 14 15 16\r\nThe path followed by the visitor is: 1->6->11->16->2->7->12->3->8->4\r\nYou are required to print the path followed by the traveller to visit all the monuments.\r\nRefer to the photo for a better clarification.\r\n1. You are given a number n, representing the number of rows and columns of a square matrix.\r\n2. You are given n * n numbers, representing elements of 2d array a.\r\n3. You are required to diagonally traverse the upper half of the matrix and print the contents.\r\nFor details check image.\r\n\r\n<img src=\"http://pepcoding.com/resources/ojquestionresource/images/diagonal-traversal.jpg\" alt=\"diagonal-traversal\">","inputFormat":"A number n\r\ne11\r\ne12..\r\ne21\r\ne22..\r\n.. n * n number of elements of array a","outputFormat":"Diagonal traversal as in image below","constraints":"1 &lt;= n &lt;= 10^2\r\n-10^9 &lt;= e11, e12, .. n * m elements &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\nusing namespace std;\n\nvoid upperDiagonal_traverse(int n, vector<vector<int>> &arr)\n{\n // write your code here\n}\n\nint main()\n{\n int n;\n cin >> n;\n vector<vector<int>> arr(n, vector<int>(n));\n for (int i = 0; i < n; i++)\n for (int j = 0; j < n; j++)\n cin >> arr[i][j];\n \n upperDiagonal_traverse(n, arr);\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def diagonal(arr , n):\n #write your code here\n\n \n \nn= int(input())\narr = []\nfor i in range(n): \n a=[];\n for j in range(n):\n a.append(int(input()));\n arr.append(a);\n\ndiagonal(arr , n)"}},"points":10,"difficulty":"easy","sampleInput":"4\r\n11\r\n12\r\n13\r\n14\r\n21\r\n22\r\n23\r\n24\r\n31\r\n32\r\n33\r\n34\r\n41\r\n42\r\n43\r\n44","sampleOutput":"11\r\n22\r\n33\r\n44\r\n12\r\n23\r\n34\r\n13\r\n24\r\n14","questionVideo":"https://www.youtube.com/embed/rydBe9U-RtY","hints":[],"associated":[{"id":"ca862c99-591e-4766-9547-802bea38705b","name":"(sow) Will the difference between j-th column and i-th row will be constant and unique to a particular diagonal?","slug":"sow-will-the-difference-between-j-th-column-and-i-th-row-will-be-constant-and-unique-to-a-particular-diagonal","type":4},{"id":"ce2545f9-32db-4b89-a011-71f95edee3d7","name":"(sow) If n*n is the size of matrix, then how namy no. of diagonals needed to be printed ?","slug":"sow-if-n-n-is-the-size-of-matrix-then-how-namy-no-of-diagonals-needed-to-be-printed","type":4},{"id":"f92dc867-7803-48e0-9434-7ce7262485b7","name":"(sow2) If we represent an element using this notation of {i, j} it means an element of the i-th row and j-th column. If this element belongs to a diagonal then the next element in the diagonal will be __?__ And the previous element was __?__.","slug":"sow2-if-we-represent-an-element-using-this-notation-of-i-j-it-means-an-element-of-the-i-th-row-and-j-th-column-if-this-element-belongs-to-a-diagonal-then-the-next-element-in-the-diagonal-will-be-and-the-previous-element-was","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f185374c-5448-476e-93e8-c4a4e9d1d520","name":"2D Arrays","slug":"2d-arrays-9999","type":0},{"id":"1048caa6-2f2f-43dc-9caf-7d669fb7de83","name":"The State Of Wakanda - 2","slug":"the-state-of-wakanda-2","type":1}],"next":{"id":"36f65bf4-d6d6-492c-9c0a-a837e3ad5698","name":"The State of Wakanda - 2","type":3,"slug":"the-state-of-wakanda-2"},"prev":{"id":"3e2481e7-d0e2-4740-92d8-e78255367492","name":"Ring Rotate","type":3,"slug":"ring-rotate"}}}`

Editor

# The State Of Wakanda - 2

easy

The historic state of Wakanda has various monuments and souveniers which are visited by many travellers every day. The guides follow a prescribed route of visiting the monuments which improves them understand the relevance of each monument. The route of the monument is fixed and expressed in a 2-d matrix where the travellers visit the prescribed next monument. For example 1 2 3 4 5 6 7 8 9 is the prescribed route and the visitors travels this path: 1->2->3->4->5->6->7->8->9 However, a certain visitor decides to travel a different path as follows: 1. The visitor only plans to visit the upper diagonal triangle of the monument list. 2. The visitor travels diagonally till there are no more moves left in the current journey. 3. He then visits the adjacent monument to the first monument of current diagonal journey. 4. He continues the same path till all the monuments of the upper half have been travelled. For Example: The monuments are named as: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 The path followed by the visitor is: 1->6->11->16->2->7->12->3->8->4 You are required to print the path followed by the traveller to visit all the monuments. Refer to the photo for a better clarification. 1. You are given a number n, representing the number of rows and columns of a square matrix. 2. You are given n * n numbers, representing elements of 2d array a. 3. You are required to diagonally traverse the upper half of the matrix and print the contents. For details check image.

## Constraints

1 <= n <= 10^2 -10^9 <= e11, e12, .. n * m elements <= 10^9

## Format

### Input

A number n e11 e12.. e21 e22.. .. n * n number of elements of array a

### Output

Diagonal traversal as in image below

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}11 22 33 44 12 23 34 13 24 14```

Question Video

Discussions

Show Discussion

Related Resources