I was asked to write code for the below structure:     1    3 2   4 5 6  10 9 8 711 12 13 14 15And I tried the below code, which works, yet I'm wondering if there is any better approach than this:public static void main(String[] args) {    int n=5; // no of rows    int c=0;    for(int i=1;i<=n;i++){        for(int k=i;k<n;k++){            System.out.print( );        }        if(i%2!=0){        for(int j=0;j<i;j++){            c++;            System.out.print(c + );        }        }        else{            int a[] = new int [i+1];            for(int j=0;j<i;j++){                c++;                a[j]=c;                             }            for(int j=i-1;j>=0;j--){               System.out.print(a[j]+ );            }        }        System.out.println();    }}Is there any better implementation or am I doing it right?
pyramid numbers in alternative reverse order
java;interview questions
you have done it like thiselse{            int a[] = new int [i+1];            for(int j=0;j<i;j++){                c++;                a[j]=c;                             }            for(int j=i-1;j>=0;j--){               System.out.print(a[j]+ );            }        }analyzing your code:               int a[] = new int [i+1];                for(int j=0;j<i;j++){                    c++;                    a[j]=c;                                 }in the above statements you are using an array to store the values to be displayed and then iterating over the array belowfor(int j=i-1;j>=0;j--){               System.out.print(a[j]+ );            }so in all looping over twice for a single line first generating the valuesand then printing the values, and using extra memory for storing the values. instead you can implement it like thiselse      {        c+=i; // find the number to start from         for (int j = 0; j < i; j++){          System.out.print(c+ ); // print the number          c--; // decrement the number        }        c+=i; // reset the number to maintain count.      }Explanation    c+=i; // find the number to start from just calculate the number from where to print then use a single loop     for (int j = 0; j < i; j++){      System.out.print(c+ ); // print the number      c--; // decrement the number    }to print the number by just decrementing the counter, and the last step would be to just restore the counter to proceed with normal execution.  c+=i; // reset the number to maintain count.This will have following advantagesNo use of extra memory.No use of extra iteration.