Skip to content

Instantly share code, notes, and snippets.

@arafatkamaal
Created November 22, 2019 14:07
Show Gist options
  • Save arafatkamaal/f871f86adea9fa38eb52a9ab83b75fa0 to your computer and use it in GitHub Desktop.
Save arafatkamaal/f871f86adea9fa38eb52a9ab83b75fa0 to your computer and use it in GitHub Desktop.
Prefix sums in java.
public class PrefixSumArray {
public static int[][] prefixSumArray(int[] array) {
int al = array.length;
int[][] result = new int[al][al];
int sum = 0;
for(int i = 0; i < al; i++) {
for(int j = i; j < al; j++) {
sum += array[j];
result[i][j] = sum;
}
sum = 0;
}
return result;
}
public static void main(String args[]) {
System.out.println("Beginning of the Program");
int[][] result = prefixSumArray(new int[] {1, 2, 3, 4, 5});
for(int i = 0; i < result.length; i++) {
for(int j = 0; j < result.length; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment