-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangeSum.java
More file actions
34 lines (28 loc) · 784 Bytes
/
RangeSum.java
File metadata and controls
34 lines (28 loc) · 784 Bytes
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
package Array;
import java.util.Scanner;
public class RangeSum {
/**
* @param arr - It is the array whose prefix sum is to be calculated.
*/
static void prefixSum(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
arr[i] += arr[i - 1];
}
}
// Time Complexity - (n+q), Auxiliary Space - O(1)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
int[] arr = {0, 5, 2, 1, 4, 3};
prefixSum(arr);
while (q-- >= 0) {
int l, r;
l = sc.nextInt();
r = sc.nextInt();
int ans = (arr[r] - arr[l - 1]);
System.out.println(ans);
}
sc.close();
}
}