I'm pretty new to Java and I am trying to implement prefix sum
package Arrays;
public class prefixSum {
static void sumArray(int arr[]) {
int n = arr.length;
int aux[] = new int[n];
int curr = arr[0];
aux[0] = curr;
for (int i = 1; i < n; i++) {
aux[i] = arr[i] + curr;
}
}
static int getSum(int arr[],int start,int end){
int n=arr.length;
sumArray(arr);
if(start==0){
return aux[end];
}
return aux[end]-aux[start-1]
}
public static void main(String[] args) {
int arr[] = { 2, 5, 7, 3, 4, 5, 3 };
int start = 2;
int end = 5;
System.out.print(getSum(arr, start, end));
}
}
I want aux[] to be a global array that can be accessed anywhere. Also, I would like the length of the aux[] array to be the same as the length as arr[].