1

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;
}
Top Cat
  • 350
  • 1
  • 7
  • 15

2 Answers2

4

You can solve this in O(n) time and space with a prefix-sum array:

array    = [1, 5, 8, 4, 5]
prefixes = [1, 6,14,18,23]

sums(1,3) = prefixes[3] - prefixes[0] = 18 - 1 = 17
גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61
  • This is by no means answering the question. `sums(1, 3) = array[1] + array[3]`. So what is the point? You have replaced addition by subtraction. And it is not filling the 2D array. – Eugene Sh. Nov 10 '16 at 20:12
  • @EugeneSh. Thank you for your comment. Isn't `array[1] + array[3]` equal to `9`? I think the OP meant `sums[1][3]` should return the sum for `array[1] + array[2] + array[3]`, which would be `5 + 8 + 4 = 17`. As I understand it, the question is "How can I make this Algorithm more efficient?" and I think my answer suggests one method to do that. – גלעד ברקן Nov 10 '16 at 20:19
  • Your answer works, however it is not what I need. What I am looking for is for an algorithm which produces a 2 array, such that `arr[1][3]` will return the sum of all values from the index 1-3. This answer uses a method to calculate the sum (although this is most likely impossible). – Top Cat Nov 10 '16 at 20:26
  • @TopCat what's the wording of the question on your lecture slide? – גלעד ברקן Nov 10 '16 at 20:32
  • The question states "The following algorithm produces a 2D array. Is it possible to make it more efficient?". – Top Cat Nov 10 '16 at 20:37
  • 1
    @TopCat with that wording, I would think the O(n) suggestion answers the question, but it's hard to be sure without more context, of course. – גלעד ברקן Nov 10 '16 at 20:39
  • Depends how you understand it. Like "The following algorithm produces a 2D array, can you make it more efficient than producing it", or "can you produce it more efficiently". – Eugene Sh. Nov 10 '16 at 20:53
  • @EugeneSh. Right, I would agree it depends on the wording and context and how we interpret the question. I guess we'll have to wait and hear what the instructor intended :) – גלעד ברקן Nov 10 '16 at 21:03
0

Calculate prefix sum in the following way:

public static int[] sum(int[] values){
    int[] prefix_sums = new int[values.length];
    prefix_sums[0] = values[0];
    for(int x = 1; x < values.length; x++){
        prefix_sums[x] = prefix_sums[x-1] + values[x];
    }
    return prefix_sums;
}

As per your query, you want to find the sum from index x to index y (0 based indexing), this is how to get the sum:

if(x==0)
    result = prefix_sums[y];
else
    result = prefix_sums[y] - prefix_sums[x-1];

Hope it helps!!!

User_Targaryen
  • 4,125
  • 4
  • 30
  • 51