I've got an Algorithm that produces a 2D array containing the sum of all possible values. For example, for the array [1,5,8,4,5]
, the 2D array sums[1][3]
should return the sum of index 1-3 in the original array (17). I believe that in terms of big O, the efficiency is O(N2). Is there any way I can make this algorithm more efficient?
public static int[][] sum(int[] values){
int[][] sums = new int[values.length][values.length];
for(int x = 0; x < values.length; x++){
int total = 0;
for(int y = x; y < values.length; y++) {
total += values[y];
sums[x][y] = total;
}
}
return sums;
}