Created
November 22, 2019 14:07
-
-
Save arafatkamaal/f871f86adea9fa38eb52a9ab83b75fa0 to your computer and use it in GitHub Desktop.
Prefix sums in java.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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