MCPcopy Create free account
hub / github.com/Manvityagi/PW-Skills-Java-Course-Codes / PrefixSum

Class PrefixSum

Lecture 19 - Prefix Sum/src/PrefixSum.java:3–37  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import java.util.Scanner;
2
3public class PrefixSum {
4 static void printArray(int[] arr){
5 for(int i = 0; i < arr.length; i++){
6 System.out.print(arr[i] + " ");
7 }
8 System.out.println();
9 }
10
11 static int[] makePrefixSumArray(int[] arr){
12 int n = arr.length;
13 for(int i = 1; i < n; i++){
14 arr[i] = arr[i-1] + arr[i];
15 }
16 return arr;
17 }
18
19 public static void main(String[] args) {
20 Scanner sc = new Scanner(System.in);
21 System.out.print("Enter array size ");
22 int n = sc.nextInt();
23 int[] arr = new int[n];
24
25 System.out.println("Enter " + n + " elements");
26 for(int i = 0; i < n; i++){
27 arr[i] = sc.nextInt();
28 }
29 System.out.println("Input Array");
30 printArray(arr);
31
32 int[] pref = makePrefixSumArray(arr);
33 printArray(pref);
34
35
36 }
37}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected