Day-1 coding

Table of contents

Pattern Problem #java

input : 3
Output : 1,2,3,4,17,18,19,20
         5,6,7,14,15,16
         8,9,12,13
         10,11

code

import java.util.*;
import java.io.*;
public class Main{

    public static void main(String[] args){
    ArrayList<ArrayList<Integer>> a = new ArrayList();
     Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    int x = 1;
    for(int i=0;i<n;i++){
        ArrayList<Integer> temp = new ArrayList();
        for(int j = 0;j<n-i;j++){
            temp.add(x);
            x++;
        }
        a.add(temp);
    }
    for(int i = n-1;i>=0;i--){
        for(int j = 0;j<n-i;j++){
            a.get(i).add(x);
            x++;
        }
    }

    for(int i = 0;i<n;i++){
        System.out.println(a.get(i));


    }
    }
}
0
Subscribe to my newsletter

Read articles from Sahishna Gongani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sahishna Gongani
Sahishna Gongani