Print Pattern – Codechef December Lunchtime 2018 Problem in Java

Problem Statement

We are provided with an integer N. We have to print the N*N pattern like consider the following 4×4 pattern:

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

Algorithm to solve December lunchtime 2018 problem

  1. For the upper half matrix sum of indexes of rows and columns are less than the order of square matrix and for lower half its greater.
  2. For upper half matrix we store the values in an array starting from a column and decreasing column  and increasing row value by 1 until they reach 0 and store the sum of indexes of rows and columns in hash set because for a diagonal sum of indexes of rows and columns are same for all elements and with storing it in a hash set we can check using set. contains a method that we do not use that value again.
  3. For lower half matrix we store the values in an array by increasing row and decreasing column value by 1 here we are considering the number of elements in a diagonal like diagonal just after main diagonal element consider order-1 elements, diagonal after that order-2 like that we go till the value reaches 1.
  4. Finally, we print the stored array which is our final output.

What does Hash_Set.contains() do ?

Hash_Set.contains() check that if a particular value is there in a set or not. If the set holds that value it returns true otherwise false.

   Syntax :- Hash_Set.contains(value);

Java Code to print the required pattern

import java.util.Scanner;

import java.util.HashSet; 

   class Codespeedy

{
  public static void main(String[] args)
   
    {
       Scanner scan = new Scanner(System.in);
       
         int no,i,j,count=1,counter;
           
               no=scan.nextInt();
             
             counter=no-1;
             
            HashSet<Integer> s = new HashSet<Integer>(); 
             
             int a[][] = new int[no+1][no+1];
             
             for(i=0;i<no;i++)
             
            {
                 for(j=0;j<no;j++)
                
                  {
                        if(!s.contains(i+j))
                       {
                          int m=i;
                          int n=j;
                          
                            if(i+j<no)
                         {
                              while(n>-1)
                            {
                              a[m][n]=count++;
                               
                                m++;
                                n--;
                            }
                         }
                         
                            if((i+j)>no || (i+j)==no)
                         {
                             long c=0;
                              
                               while(c!=counter)
                            {
                              a[m][n]=count++;
                                m++;
                                n--;
                                c++;
                            }
                            
                              counter--;
                            
                         }
                       }
                      
                       s.add(i+j);
                 }
             }
             
                   for(i=0;i<no;i++)
             {
                     for(j=0;j<no;j++)
                 {
                    System.out.print(a[i][j]+" ");
                 }
                
                    System.out.println(" ");
             }
      }
 }

INPUT

4

OUTPUT

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

You may also learn,

2 responses to “Print Pattern – Codechef December Lunchtime 2018 Problem in Java”

  1. Swapnil says:

    Nice explained and done in an easy way this problem .

  2. Parth Bajpai says:

    Nicely explained

Leave a Reply

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