0

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[].

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

1

You can do so by making it a static member of the class as follow:

public class PrefixSum {

    private static int[] aux;

    ...

}

However, keep in mind the following:

  • Your array parameter declaration is wrong, you should declare it as int[] aux rather than int aux[]
  • You should use the global state as little as possible, they're dangerous and will cause you issues in the future if the program would run on multiple threads for example. Local state is always better if possible.
  • Last but not least, and mainly FYI - arrays in java are passed by reference and not by value, which means that when you return an array from a function you return the actual array and not a copy of it.

So my suggestion would be something like this:

public class PrefixSum {

    static int[] 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;
        }

        return aux;
    }

    static int getSum(int[] arr, int start, int end) {
        int[] aux = 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));
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Yonatan Karp-Rudin
  • 1,056
  • 8
  • 24
  • `type[] id` is _preferred_ style, but `type id[]` has also been allowed always (for decl of field, parameter, or local, though not in more recent try-resources or pattern) -- and you've used it yourself in `main`! – dave_thompson_085 Nov 24 '22 at 08:51
  • The main was a copy-paste, I'll fix it :) The main reason I suggested using the type declaration is that IntelliJ linter was complaining about it – Yonatan Karp-Rudin Nov 24 '22 at 08:55
0

you can define the aux[] array in class level and initialize it inside the main method:

public class prefixSum {
    static int[] aux;

...
    public static void main(String[] args) {
        int[] arr = { 2, 5, 7, 3, 4, 5, 3 };
        aux = new int[arr.length];
    ...
    }

And remove the aux array declaration from sumArray method.

P.S.: The Java convention about array declarations is like int[] arr but not int arr[]

Read more about Java code conventions here.

After re-formatting your code It'll be like this (I've not tested the functionality btw) :

public class prefixSum {
    static int aux[];

    static void sumArray(int[] arr) {
        int n = arr.length;
        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};
        aux = new int[arr.length];

        int start = 2;
        int end = 5;
        System.out.print(getSum(arr, start, end));

    }
}
Joshgun
  • 372
  • 3
  • 16