Reputation:
This was actually an interview question. I had to print the following using Java:
9
9 8 9
9 8 7 8 9
9 8 7 6 7 8 9
. . .
. . .
During the interview, I wrote an embarrassing piece of code, but it worked nonetheless - using an outer loop, two inner loops (one for the decrementing sequence and one for the incrementing sequence!) and a ton of variables. One of the variables was the length of each row.
The interviewer asked me to try and rewrite it using
just one outer and one inner loop
without the row length variable.
Note: After looking at the answers, I think the interviewer didn't really mean the second condition. He might have just wanted me to simplify my code and the second point just bumbled out of his mouth.
So, later back home, I arrived at this:
int rowCnt = 5;
for(int i = 1; i <= rowCnt; i++)
{
int val = 9;
int delta = -1;
int rowLen = i * 2 - 1;
for(int j = 1; j <= rowLen; j++)
{
System.out.print(val + " ");
val += delta;
if(j >= rowLen / 2) delta = 1;
}
System.out.println();
}
Here, I am using just one inner loop. I'm using a delta
value to determine whether increment or decrement happens. For each row, I compare the current index to the midpoint of the row and change the delta.
I satisfied the first condition - just one inner loop. But I am not able to do it without using the row length.
How can we print this without finding out the row length?
Many answers were acceptable, But I had to choose one, and picked the one that was simplest to understand for me.
Upvotes: 5
Views: 292
Reputation: 22446
My non-recursive solution:
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 2*i+1; j++)
System.out.print((Math.abs(j - i) + 9 - i) + " ");
System.out.println();
}
Upvotes: 0
Reputation: 17268
public class Pyramid {
public static void main(String[] args) {
int start = 9;
String left = "";
String right = "";
for (int i=start; i>=0; i--) {
System.out.println(left+i+right);
left = left+i;
right = i+right;
}
}
}
Sample output:
9
989
98789
9876789
987656789
98765456789
9876543456789
987654323456789
98765432123456789
9876543210123456789
This iterative solution is equivalent to the recursive solution. I would prefer to use iteration over recursion since the extra stack memory needed by the recursive solution could be huge when the number of rows grows big.
Upvotes: 0
Reputation: 8311
They probably wanted to hear the word 'recursion'.
Here's a recursive solution that doesn't need length:
countDownInMiddle("", 9, "");
private static void countDownInMiddle(String start, int n, String end) {
if (n < 0) {
return;
}
System.out.println(start + n + end);
countDownInMiddle(start + n, n - 1, n + end);
}
Upvotes: 2
Reputation: 3071
This is simple PHP, hope logic is clear and easily portable to Java:
$rowCount = 10;
$startNum = 9;
for ($idx =0; $idx <$rowCount; $idx ++) {
for ($jdx=0; $jdx < (2*$idx +1); $jdx++) {
if ($idx < $jdx)
echo $startNum -(2*$idx) + $jdx.' ';
else
echo $startNum - $jdx.' ';
}
echo '<br/>';
}
Upvotes: 1
Reputation: 37813
How about:
int start = 9;
for (int i = 0; i <= start; i++) {
StringBuilder sb = new StringBuilder((start - i) + " ");
for (int j = start - i; j < start; j++) {
sb.insert(0, (j + 1) + " ");
sb.append((j + 1) + " ");
}
System.out.println(sb.toString());
}
Upvotes: 2