Questions tagged [prefix-sum]
63 questions
2
votes
1 answer
How to solve M times prefix sum with better time complexity
The problem is to find the prefix sum of array of length N by repeating the process M times. e.g.
Example N=3
M=4
array = 1 2 3
output = 1 6 21
Explanation:
Step 1 prefix Sum = 1 3 6
Step 2 prefix sum = 1 4 10
Step 3 prefix sum = 1 5 15
Step 4(M)…

rsham
- 23
- 2
2
votes
1 answer
CUDA parallel scan algorithm shared memory race condition
I'm reading the book "Programming Massively Parallel Processor" (3rd edition) that presents an implementation of the Kogge-Stone parallel scan algorithm.
This algorithm is meant to be run by a single block (this is just a preliminary simplification)…

Damiano Massarelli
- 55
- 3
2
votes
1 answer
The Game of Piles
Recently I have encountered what seems to be a quite interesting game that suggest implementing both two-pointers and prefix-sum techniques on a large dataset. Here is the task itself:
Imagine there is an array v of length k where k (k<=10**5)…

Ramiil
- 59
- 8
2
votes
1 answer
What is the cleanest way to do a `std::partial_sum` with a `0` in front?
In C++, there is a function std::partial_sum to compute prefix sum. The code
#include
#include
#include
#include
int main() {
std::vector a = {1, 2, 3, 4, 5};
std::partial_sum(a.begin(),…

TYeung
- 2,579
- 2
- 15
- 30
2
votes
3 answers
Is prefix sum included in dynamic programming?
I've been solving algorithm problems, and I'm a bit confused about the terms.
When we want to calculate prefix sum (or cumulative sum) like the code below, can we say that we are using dynamic programming?
def calc_prefix_sum(nums):
N =…

Ricky
- 61
- 8
2
votes
1 answer
Need help understanding how Binary Search works on prefix sum arrays
I was solving the problem Minimum Size Subarray Sum. I am trying to solve it by using binary search on a prefix sum array that solves the problem in n*log(n) complexity.
I managed to get it working however I do not understand why my solution…

Arat254
- 449
- 2
- 5
- 17
2
votes
3 answers
codility GenomicRangeQuery algorithm comparsion speed Java vs Swift
I rewrited code that solves GenomicRangeQuery task from Java to Swift. The code in Jave gets 100/100 score but the code in Swift fails all performance tests. I'm trying to understand why because logic in code is the same. I'w wondering why Swift…

Marcin Kapusta
- 5,076
- 3
- 38
- 55
2
votes
0 answers
learning prefix sum by tree reduction
I need to learn about prefix sum by tree reduction and write an MPI code in C for that. I already know prefix sum by recursive doubling or scan, and have some background in programming by MPI. Here is the structure of tree reduction which I should…

Amir
- 637
- 1
- 6
- 11
2
votes
1 answer
PyOpenCL, array filter: copy_if vs my own atomic-based implementation
I have an array of random integers. For example [132, 2, 31, 49, 15, 6, 70, 18 ... , 99, 1001]. I want to produce array of all numbers that greater than 100 for example and get size of that array.
There are two ways:
New feature of PyOpenCL…

petRUShka
- 9,812
- 12
- 61
- 95
1
vote
1 answer
Using partial_sum() with long long values
I am solving a problem for which, I need to calculate the prefix and suffix sum values. When I do it this way:
class Solution {
public:
int minimumAverageDifference(vector& nums) {
long n=size(nums);
vector…

J. Doe
- 857
- 3
- 7
1
vote
1 answer
bit shift operation in parallel prefix sum
The code is to compute prefix sum parallelly from OpengGL-Superbible 10.
The shader shown has a local workgroup size of 1024, which means it will process arrays of 2048 elements, as each invocation computes two elements of the output array. The…

mq s
- 41
- 4
1
vote
1 answer
Hillis and Steele on a prefix sum multithreading assignment in C
I'm working on a CS assignment, where I have to use p_threads to compute an array's prefix sum. The professor told us to use the Hillis and Steele algorithm. I found some pseudocode on wikipedia (here), specifically:
I'm a little stuck on…

Gummysaur
- 96
- 7
1
vote
2 answers
Why is this code failing a test case [Max Distance]
Problem: Given an array A of integers, find the maximum of j - i subjected to the constraint of A[i] <= A[j].
If there is no solution possible, return -1.
Example :
A : [3 5 4 2]
Output : 2 for the pair (3, 4)
INPUT:
9 8 7 -9 -1
EXPECTED…

Naga Sai Sriya
- 61
- 1
- 7
1
vote
1 answer
Why is 1 for-loop slower than 2 for-loops in problem related to prefix sum matrix?
I'm recently doing this problem, taken directly and translated from day 1 task 3 of IOI 2010, "Quality of life", and I encountered a weird phenomenon.
I was setting up a 0-1 matrix and using that to calculate a prefix sum matrix in 1 loop:
for (int…

silverfox
- 1,568
- 10
- 27
1
vote
2 answers
Undoing a prefix sum
The simple way to calculate a prefix sum in Haskell is
scanl1 (+) [1,2,3,4,5,6]
which produces the output
[1,3,6,10,15,21]
I needed to write the inverse, and this is what I came up with:
undo_prefix_sum :: (Num a) => [a] -> [a]
undo_prefix_sum s =…

Theo H
- 131
- 10