forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefixSum.java
More file actions
54 lines (49 loc) · 1.86 KB
/
PrefixSum.java
File metadata and controls
54 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.thealgorithms.prefixsum;
/**
* A class that implements the Prefix Sum algorithm.
*
* <p>Prefix Sum is a technique used to preprocess an array such that
* range sum queries can be answered in O(1) time.
* The preprocessing step takes O(N) time.
*
* <p>This implementation uses a long array for the prefix sums to prevent
* integer overflow when the sum of elements exceeds Integer.MAX_VALUE.
*
* @see <a href="https://en.wikipedia.org/wiki/Prefix_sum">Prefix Sum (Wikipedia)</a>
* @author Chahat Sandhu, <a href="https://github.com/singhc7">singhc7</a>
*/
public class PrefixSum {
private final long[] prefixSums;
/**
* Constructor to preprocess the input array.
*
* @param array The input integer array.
* @throws IllegalArgumentException if the array is null.
*/
public PrefixSum(int[] array) {
if (array == null) {
throw new IllegalArgumentException("Input array cannot be null");
}
this.prefixSums = new long[array.length + 1];
this.prefixSums[0] = 0;
for (int i = 0; i < array.length; i++) {
// Automatically promotes int to long during addition
this.prefixSums[i + 1] = this.prefixSums[i] + array[i];
}
}
/**
* Calculates the sum of elements in the range [left, right].
* Indices are 0-based.
*
* @param left The starting index (inclusive).
* @param right The ending index (inclusive).
* @return The sum of elements from index left to right as a long.
* @throws IndexOutOfBoundsException if indices are out of valid range.
*/
public long sumRange(int left, int right) {
if (left < 0 || right >= prefixSums.length - 1 || left > right) {
throw new IndexOutOfBoundsException("Invalid range indices");
}
return prefixSums[right + 1] - prefixSums[left];
}
}