← All problems
#72 Stream Compaction Medium

Given a 1D array A of N 32-bit floating point numbers, compact all positive elements (A[i] > 0) to the front of the output array out, preserving their original relative order. Fill any remaining positions with 0.0. Stream compaction is a fundamental GPU primitive used throughout rendering, sparse computation, and collision detection.

Implementation Requirements

Example

Input:  A = [1.0, -2.0, 3.0, 0.0, -1.0, 4.0]
Output: out = [1.0, 3.0, 4.0, 0.0, 0.0, 0.0]

Constraints

Guided walkthrough coming soon. Below is the official starter signature in each language; the clickable line-by-line explanation hasn't been authored yet.
Problem text & starters from the official AlphaGPU/leetgpu-challenges. Solve it on leetgpu.com.