Actually I am solving one of the SPOJ problem. In this I am comparing two adjacent array elements and storing greater element in previous row index with its addition. Here is my code-
import java.util.Scanner;
/**
* Created by Sainath on 15-08-2016.
*/
public class SIT {
public static void main(String[] args) {
int a;
int b[][];
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n != 0) {
a = sc.nextInt();
b = new int[a][a];
for (int i = 0; i<a; i++) {
for (int j = 0; j<=i; j++) {
b[i][j] = sc.nextInt();
}
}
for (int i=a; i>1; i--) {
for (int j=1; j<a; j++) {
if (b[i][j] >= b[i][j+1])
b[i-1][j] = b[i-1][j] + b[i][j];
else
b[i-1][j] = b[i-1][j] + b[i][j+1];
}
}
System.out.println(b[0][0]);
n--;
}
}
}
I am getting "ArrayIndexOutOfBoundsException" exception at line 22.