3
\$\begingroup\$

I was asked to write code for the below structure:

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

And 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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 13, 2014 at 9:19
\$\endgroup\$
4
  • 3
    \$\begingroup\$ oh hell please, first and foremost: properly indent your code. especially for interviews... \$\endgroup\$ Commented Oct 13, 2014 at 12:49
  • \$\begingroup\$ i don't worry about indenting in here, but the implementation matters. Indenting is a matter of ctrl + keys.. \$\endgroup\$ Commented Oct 13, 2014 at 14:32
  • 2
    \$\begingroup\$ "i don't worry about indenting in here", but you should as we, who review your code, should be able to do this as best as possible. \$\endgroup\$ Commented Oct 14, 2014 at 7:42
  • \$\begingroup\$ @Heslacher agreed \$\endgroup\$ Commented Oct 14, 2014 at 8:35

3 Answers 3

6
\$\begingroup\$

you have done it like this

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]+" ");
 }
 }

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 below

for(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 values and then printing the values, and using extra memory for storing the values.

instead you can implement it like this

else
 {
 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 advantages

  1. No use of extra memory.
  2. No use of extra iteration.
Vogel612
25.5k7 gold badges59 silver badges141 bronze badges
answered Oct 13, 2014 at 10:05
\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Review! Although this answer provides an alternative way of implementing what the OP wants, commenting on what things you do differently, why you are doing it that way and pointing out the flaws in the OP's original code would make this a better answer. Copy-pasting code does not always provide a good learning experience. \$\endgroup\$ Commented Oct 13, 2014 at 10:50
7
\$\begingroup\$

Naming

int n=5; // no of rows 

If you need a comment to explain the intent of an variable, then something went wrong. You should just rename the variable to numberOfRows.
Also variable names which aren't loop iteration variables shouldn't have single letter names.
E.g c should better be currentNumber.

Space

for(int i=1;i<=n;i++){ 

Help you code to breath and add some space

for(int i = 1; i <= n; i++){ 

is easier to read.

Indention

 if(i%2!=0){
 for(int j=0;j<i;j++){
 c++;
 System.out.print(c +" ");
 }
 }

wouldn't the above be easier to read if coded like

 if(i%2 != 0){
 for(int j = 0; j < i; j++){
 c++;
 System.out.print(c +" ");
 }
 }
answered Oct 13, 2014 at 9:38
\$\endgroup\$
0
6
\$\begingroup\$

Any better implementation or am i doing it right?

As soon as you print double digits, your pyramid doesn't look quite right anymore. This will only get worse with three digits, etc. But if this wasn't a concern in the question, then your implementation looks fine.

Naming

@Heslacher already mentioned n, but all your other variable names are also not expressive: c, i, k, j, a. You should avoid using one-letter variables except in a very limited number of situations (i and j for loops in case there is no better name, x and y for coordinates).

Better names would be:

  • i: row
  • k: I don't know. But I would just move this code to its own function (getSpaces(int amountSpaces)) anyways.
  • j: column
  • c: value or number or output
  • a: rowContent

These are just suggestions, you can improve on them.

Comments and extracting Functions

If you look at this code in a month, will you still understand it directly, without thinking about it? I don't think so. But there are ways to improve on this (in addition to better naming):

I would either extract some code to its own functions (eg getSpaces(int), getRow(int), getReversedRow(int)), or at least add some comments to the loops to make your code a lot more readable.

answered Oct 13, 2014 at 9:59
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.