-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefixsum.java
More file actions
38 lines (36 loc) · 1003 Bytes
/
Copy pathPrefixsum.java
File metadata and controls
38 lines (36 loc) · 1003 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
35
36
37
38
import java.util.*;
public class Prefixsum {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int[] arr=new int[size];
for(int i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
int[] presum=new int[size];
presum[0]=arr[0];
for(int j=1;j<size;j++)
{
presum[j]=presum[j-1]+arr[j];
}
System.out.println("the presum array is");
for(int i=0;i<size;i++)
{
System.out.print(presum[i]+" ");
}
System.out.println("enter the left index and right index");
int left=sc.nextInt();
int right=sc.nextInt();
int result;
if (left == 0) {
result = presum[right];
} else {
result = presum[right] - presum[left - 1];
}
System.out.println("the result is");
System.out.println(result);
sc.close();
}
}