From 1f3199603e31e61a25cf497c62ed959116f96d4c Mon Sep 17 00:00:00 2001 From: ittou-shura Date: Mon, 16 Dec 2024 12:02:50 +0530 Subject: [PATCH] implemented prefix sum generation for given array --- Array_Functions/Prefix_Sum/README.md | 35 ++++++++++ Array_Functions/Prefix_Sum/prefix_sum.cpp | 39 +++++++++++ Array_Functions/Prefix_Sum/prefix_sum.java | 75 ++++++++++++++++++++++ Array_Functions/Prefix_Sum/prefix_sum.py | 24 +++++++ 4 files changed, 173 insertions(+) create mode 100644 Array_Functions/Prefix_Sum/README.md create mode 100644 Array_Functions/Prefix_Sum/prefix_sum.cpp create mode 100644 Array_Functions/Prefix_Sum/prefix_sum.java create mode 100644 Array_Functions/Prefix_Sum/prefix_sum.py diff --git a/Array_Functions/Prefix_Sum/README.md b/Array_Functions/Prefix_Sum/README.md new file mode 100644 index 0000000..7ec86b4 --- /dev/null +++ b/Array_Functions/Prefix_Sum/README.md @@ -0,0 +1,35 @@ +# Prefix Sum Algorithm + +## Overview +The Prefix Sum algorithm computes the cumulative sum of elements in an array or list. This cumulative sum is also known as the "partial sum". It is often used in problems where you need to quickly calculate the sum of elements in a range or subarray. + +## Example: +Given an array: + +arr = [1, 2, 3, 4, 5] + +The Prefix Sum array will store the cumulative sums up to each index: + +prefix_sum = [0, 1, 3, 6, 10, 15] + +Where: +- prefix_sum[0] = 0 (Initial value for easy range sum calculations) +- prefix_sum[i] = arr[0] + arr[1] + ... + arr[i-1] + +## Visual Breakdown: + +1. **Input Array:** + +arr = [1, 2, 3, 4, 5] + +2. **Prefix Sum Calculation:** + - prefix_sum[0] = 0 (initialized to 0) + - prefix_sum[1] = prefix_sum[0] + arr[0] = 0 + 1 = 1 + - prefix_sum[2] = prefix_sum[1] + arr[1] = 1 + 2 = 3 + - prefix_sum[3] = prefix_sum[2] + arr[2] = 3 + 3 = 6 + - prefix_sum[4] = prefix_sum[3] + arr[3] = 6 + 4 = 10 + - prefix_sum[5] = prefix_sum[4] + arr[4] = 10 + 5 = 15 + +3. **Resulting Prefix Sum Array:** + +prefix_sum = [0, 1, 3, 6, 10, 15] diff --git a/Array_Functions/Prefix_Sum/prefix_sum.cpp b/Array_Functions/Prefix_Sum/prefix_sum.cpp new file mode 100644 index 0000000..2e9cc37 --- /dev/null +++ b/Array_Functions/Prefix_Sum/prefix_sum.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +template +vector generatePrefixSum(Iterator start, Iterator end) { + vector prefixSum = {0}; + int currentSum = 0; + for (Iterator it = start; it != end; ++it) { + currentSum += *it; + prefixSum.push_back(currentSum); + } + return prefixSum; +} + +int main() { + //example with array + int a[] = {1, 2, 3, 4, 5}; + int n = sizeof(a) / sizeof(a[0]); + vector prefixSum = generatePrefixSum(a, a+n); + + cout << "Prefix Sum for range: "; + for (int sum : prefixSum) { + cout << sum << " "; + } + cout << endl; + + //example with vector + vector b = {1, 2, 3, 4, 5}; + + vector prefixSum2 = generatePrefixSum(b.begin(), b.end()); + + cout << "Prefix Sum for range: "; + for (int sum : prefixSum2) { + cout << sum << " "; + } + cout << endl; + return 0; +} diff --git a/Array_Functions/Prefix_Sum/prefix_sum.java b/Array_Functions/Prefix_Sum/prefix_sum.java new file mode 100644 index 0000000..a59397d --- /dev/null +++ b/Array_Functions/Prefix_Sum/prefix_sum.java @@ -0,0 +1,75 @@ +package CodeStock.Array_Functions.Prefix_Sum; + +import java.util.*; + +public class prefix_sum { + + public static List generatePrefixSum(int[] array, int start, int end) { + if (start < 1 || end > array.length || start > end) { + System.out.println("Invalid range for array. Please check the start and end indices."); + return Collections.emptyList(); + } + + List prefixSum = new ArrayList<>(); + prefixSum.add(0); + + int currentSum = 0; + + for (int i = start; i <= end; i++) { + currentSum += array[i - 1]; + prefixSum.add(currentSum); + } + + return prefixSum; + } + + public static List generatePrefixSum(List list, int start, int end) { + if (start < 1 || end > list.size() || start > end) { + System.out.println("Invalid range for list. Please check the start and end indices."); + return Collections.emptyList(); + } + + List prefixSum = new ArrayList<>(); + prefixSum.add(0); + + int currentSum = 0; + + for (int i = start; i <= end; i++) { + currentSum += list.get(i - 1); + prefixSum.add(currentSum); + } + + return prefixSum; + } + + public static void main(String[] args) { + //example as array + int[] a = {1, 2, 3, 4, 5}; + int startIndex = 1; + int endIndex = 4; + + List prefixSumArray = generatePrefixSum(a, startIndex, endIndex); + + if (!prefixSumArray.isEmpty()) { + System.out.print("Prefix Sum for array subrange: "); + for (int sum : prefixSumArray) { + System.out.print(sum + " "); + } + System.out.println(); + } + //example as list + List list = Arrays.asList(1, 2, 3, 4, 5); + startIndex = 1; + endIndex = 4; + + List prefixSumList = generatePrefixSum(list, startIndex, endIndex); + + if (!prefixSumList.isEmpty()) { + System.out.print("Prefix Sum for list subrange: "); + for (int sum : prefixSumList) { + System.out.print(sum + " "); + } + System.out.println(); + } + } +} diff --git a/Array_Functions/Prefix_Sum/prefix_sum.py b/Array_Functions/Prefix_Sum/prefix_sum.py new file mode 100644 index 0000000..56d7abc --- /dev/null +++ b/Array_Functions/Prefix_Sum/prefix_sum.py @@ -0,0 +1,24 @@ +def generate_prefix_sum(iterable, start, end): + if start < 1 or end > len(iterable) or start > end: + print("Invalid range. Please ensure the range is within bounds and start <= end.") + return [] + + prefix_sum = [0] + current_sum = 0 + for i in range(start - 1, end): # Adjusting for 1-based indexing + current_sum += iterable[i] + prefix_sum.append(current_sum) + + return prefix_sum + +if __name__ == "__main__": + # Example with a list + a = [1, 2, 3, 4, 5] + start_index = 2 + end_index = 4 + + prefix_sum_a = generate_prefix_sum(a, start_index, end_index) + if prefix_sum_a: # Check if the range is valid and prefix sum is not empty + print("Prefix Sum for range (array):", " ".join(map(str, prefix_sum_a))) + + \ No newline at end of file