Posts Prefix Sum Array
Post
Cancel

Prefix Sum Array

Problem Description

Find prefix Array Sum of a given array. Prefix Sum Array

Solution

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
package com.arrays;

import java.util.Arrays;

public class PrefixSum {

	public static void main(String[] args) {
		
		int[] arr = {10, 20, 10, 5, 15};
		System.out.println(Arrays.toString(getPrefixSumArray(arr)));
		
	}
	
	public static int[] getPrefixSumArray(int[] arr) {
		
		int[] prefixArray = new int[arr.length];
		
		prefixArray[0] = arr[0];
		
		for(int i=1; i<prefixArray.length; i++) {
			prefixArray[i] = prefixArray[i-1] + arr[i];
		}
		
		return prefixArray;
	}
	
	
}
This post is licensed under CC BY 4.0 by the author.