-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefixSumDynamic.java
More file actions
39 lines (37 loc) · 1.16 KB
/
Copy pathPrefixSumDynamic.java
File metadata and controls
39 lines (37 loc) · 1.16 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
import java.util.Scanner;
public class PrefixSumDynamic {
//METHOD TO PRINT ARRAY
static void display(int a[]){
for(int i=0; i<a.length; i++){
System.out.print(a[i] + " ");
}
System.out.println();
}
//METHOD FOR PREFIX SUM
static void prefixSum(int a[]){
Scanner sc= new Scanner(System.in);
System.out.print("Enter starting index: ");
int start_index= sc.nextInt();
System.out.print("Enter ending index: ");
int end_index= sc.nextInt();
for(int i=start_index; i<end_index; i++){
a[i]= a[i-1]+a[i];
}
System.out.print("Prefix sum array: ");
display(a);
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
//ARRAY SIZE AND ELEMENTS INPUT
System.out.print("Enter size of array: ");
int size= sc.nextInt();
int arr[]= new int[size];
System.out.print("Enter " + size + " elements: ");
for(int i=0; i<size; i++){
arr[i]= sc.nextInt();
}
System.out.print("Original array: ");
display(arr);
prefixSum(arr);
}
}