# Pascal’s Diagonals in Java

## The challenge

Create a function that returns an array containing the first `l` digits from the `n`th diagonal of Pascal’s triangle.

`n = 0` should generate the first diagonal of the triangle (the ‘ones’). The first number in each diagonal should be 1.

If `l = 0`, return an empty array. Assume that both `n` and `l` will be non-negative integers in all test cases.

## The solution in Java code

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```public class PascalDiagonals {

public static long[] generateDiagonal(int n, int l) {
long[] result = new long[l];
if(l > 0) {
result[0] = 1;
}
for(int i = 1; i < l; i++) {
result[i] =  ( result[i-1] *  (n + i) /  i);
}
return result;
}

}
```Code language: Java (java)```

Option 2:

``````public class PascalDiagonals {

public static long[] generateDiagonal(int n, int l) {
long[] result = new long[l];
if (l > 0) {
result[0] = 1;
for (int i = 1; i < l; ++i)
result[i] = result[i-1] * (n + i) / i;
}
return result;
}
}
```Code language: Java (java)```

Option 3:

``````public class PascalDiagonals {

public static long[] generateDiagonal(int n, int l) {
if (l == 0) return new long[0];
long[] diagonal = new long[l];
long[] temp = null;
long[][] result = new long[n + l][];
for (int i = 1; i <= n + l; i++) {
long[] row = new long[i];
for (int j = 0; j < i; j++) {
if (j == 0 || j == i - 1) row[j] = 1;
else row[j] = temp[j - 1] + temp[j];
}
result[i - 1] = row;
temp = row;
}
for (int i = n, j = 0; i < n + l; i++, j++) {
diagonal[j] = result[i][n];
}
return diagonal;
}

}
```Code language: Java (java)```

## Test cases to validate our solution

``````import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import org.junit.runners.JUnit4;
import java.util.Random;
import java.util.Arrays;

public class SolutionTest {

@Test
public void basicTests() {

long[] expected = new long[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
assertArrayEquals("All the ones", expected, PascalDiagonals.generateDiagonal(0, 10));

expected = new long[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
assertArrayEquals("Natural numbers", expected, PascalDiagonals.generateDiagonal(1, 10));

expected = new long[] { 1, 3, 6, 10, 15, 21, 28, 36, 45, 55 };
assertArrayEquals("Triangular numbers", expected, PascalDiagonals.generateDiagonal(2, 10));

expected = new long[] { 1, 4, 10, 20, 35, 56, 84, 120, 165, 220 };
assertArrayEquals("Tetrahedral numbers", expected, PascalDiagonals.generateDiagonal(3, 10));

expected = new long[] { 1, 5, 15, 35, 70, 126, 210, 330, 495, 715 };
assertArrayEquals("Pentatope numbers", expected, PascalDiagonals.generateDiagonal(4, 10));
}

@Test
public void edgeCases() {

assertArrayEquals("Array length zero", new long[] {}, PascalDiagonals.generateDiagonal(10, 0));

long[] expected = new long[] { 1, 101, 5151, 176851, 4598126, 96560646 };
assertArrayEquals("Late row, short array", expected, PascalDiagonals.generateDiagonal(100, 6));
}

@Test
public void randomTests() {

Random r = new Random();
for (int i = 0; i < 100; i++) {
int n = r.nextInt(26) + 25;
int l = r.nextInt(6) + 10;
assertArrayEquals("Random " + i, generateDiagonal(n, l), PascalDiagonals.generateDiagonal(n, l));
}
}

private static long[] generateDiagonal(int n, int l) {

long[] diagonal = new long[l];

Arrays.fill(diagonal, 1);

for (int i = 1; i < l; i++)
diagonal[i] = diagonal[i - 1] * (n + i) / i;

return diagonal;
}
}
```Code language: Java (java)```
Tags:
Subscribe
Notify of