You are not logged in. Please login at www.codechef.com to post your questions!

×

PPATERN Decemeber lunchtime 2018

This is my JAVA code for this question- import java.util.*;

class Solution {

 public static void main(String[] args) {
     Scanner ip=new Scanner(System.in);
     int n=ip.nextInt();
     int arr[][]=new int[n+1][n+1];
     int a=1;
     int i=1;
     int j=1;
     int k=1;
     int z=0;
     while(1>0) {
       int m=1;
       if(k==n*2) {
           break;
       }
       else if(k>n) {
          i=k-n+1;
          j=n;
          z--;
       }else {
       i=1;
       j=k;
       z++;
       }
       while(m<=z) {
        arr[i][j]=a;
        a++;
        m++;
        i++;
        j--; 
     }
     k++;
    }
     for(int r=1;r<=n;r++) {
         for(int c=1;c<=n;c++) {
             System.out.printf("%5d ",arr[r][c]);
         }
         System.out.println();
     }
}

}

and the pattern printing for different cases is also correct,help me finding what is wrong in this. Pattern for n=4:

1     2     4     7 
3     5     8    11 
6     9    12    14

10 13 15 16

asked 30 Dec '18, 01:33

ankyy_0103's gravatar image

1★ankyy_0103
1
accept rate: 0%


test cases t not considered

link

answered 30 Dec '18, 02:34

machoman's gravatar image

1★machoman
101
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×678

question asked: 30 Dec '18, 01:33

question was seen: 147 times

last updated: 30 Dec '18, 02:34