PROBLEM LINK:Author: AmirReza PoorAkhavan PREREQUISITES:NONE PROBLEM:You are given a number $N$ and you are asked to output an $N \times N$ matrix consisting of numbers from $1$ to $N \times N$ according to a given pattern. EXPLANATION:To solve this problem, let's first find out what the pattern really is. By looking at the given matrix, we realize that each antidiagonal of the matrix consists of some consecutive numbers written from top to bottom. the first antidiagonal is composed of only number $1$, the second is composed of numbers $2$ and $3$, and the following antidiagonals are filled similarly. So it turns out the task is fairly simple. We have to start from the first antidiagonal, go through all of those diagonals one by one and write the numbers from top to bottom starting from $1$. So all that's left is to go through antidiagonals one by one efficiently. Let's enumerate the rows of the matrix from top to bottom starting from $0$. Similarly, let's enumerate the columns from left to right starting from $0$. Now let's enumerate the antidiagonals starting from $0$. Note that based on our enumeration, the sum of row and column indices of all the cells of the matrix that are on the $i^{th}$ antidiagonal is equal to $i$. We can write a neat implementation based on this. We simply iterate over numbers from $0$ up to $2 \times (N1)$ and deal with the $i^{th}$ antidiagonal on the $i^{th}$ iteration. The time complexity of this solution is $O(N^2)$ Refer to the editorialist's code to see the implementation of the described solution. AUTHOR'S AND TESTER'S SOLUTIONS:Author's solution can be found here Tester's solution can be found here Editorialist's solution can be found here
This question is marked "community wiki".
asked 28 Dec '18, 22:08

VIDEO SOLUTION : https://m.youtube.com/watch?v=nVJpkCVtIE&t=3s answered 31 Dec '18, 16:38

include <stdio.h>int main() { int i, j, k, m = 1, p = 1, l, q, n, w, t; scanf("%d", &t); while (t) { scanf("%d", &n); w = n  1; for (i = 1; i <= n; i++) {
} answered 03 Jan, 01:14

include <stdio.h>int main() { int i, j, k, m = 1, p = 1, l, q, n, w, t; scanf("%d", &t); while (t) { scanf("%d", &n); w = n  1; for (i = 1; i <= n; i++) {
} answered 03 Jan, 01:14

In my answer I have tried to solve this question using a series. For example if n=5 then we make a series: l = 1,2,3,4,4,3,2,1 . Now we take a 2 variables i.e. x=1 & prev. Now we do this
link
This answer is marked "community wiki".
answered 05 Jan, 13:18

I don't know why my solution is Wrong, although it does what is required by the Question?
answered 05 Jan, 15:27

import java.util.*; class pattern{ public static void main(String[] args) { Scanner scan= new Scanner(System.in); System.out.println("enter the order"); int n = scan.nextInt(); System.out.println("enter the initial number"); int ini=scan.nextInt(); int[][] real=new int [n][n];
} for (int i=0;i<n ;i++ ) {
}
} *I still cant figure out why is a wrong solution* answered 06 Jan, 04:14

My solution, probably the smallest answered 19 Feb, 22:42
