← All problems
#9 1D Convolution Easy

Implement a program that performs a 1D convolution operation. Given an input array and a kernel (filter), compute the convolved output. The convolution should be performed with a "valid" boundary condition, meaning the kernel is only applied where it fully overlaps with the input.

input 1 2 3 4 5 kernel 1 0 -1 1×1 2×0 3×(-1) = 1 + 0 + (-3) = -2 output -2 …

The input consists of two arrays:

The output should be written to the output array, which will have a size of input_size - kernel_size + 1.

The convolution operation is defined mathematically as:

\[ output[i] = \sum_{j=0}^{kernel\_size-1} input[i + j] \cdot kernel[j] \]

where \(i\) ranges from 0 to \(input\_size - kernel\_size\).

Implementation Requirements

Example 1:

Input: input = [1, 2, 3, 4, 5], kernel = [1, 0, -1]
Output: [-2, -2, -2]

Example 2:

Input: input = [2, 4, 6, 8], kernel = [0.5, 0.2]
Output: [1.8, 3.2, 4.6]

Constraints

Problem text & starters from the official AlphaGPU/leetgpu-challenges. Solve it on leetgpu.com.