1D Convolution (Valid)

HardMachine LearningArraySliding Window

Description

Given a 1D signal and a 1D kernel, return the valid cross-correlation (the operation deep-learning libraries call convolution): slide the kernel over the signal without padding and, at each position, return the sum of element-wise products. The output length is signal length minus kernel length plus 1.

Examples

Input:[1,2,3,4], [1,0]
Output:[1,2,3]
Explanation:

The kernel slides across the signal one step at a time, and each output is the dot product of the kernel with the window it currently covers.

Input:[1,2,3], [1,1]
Output:[3,5]
Explanation:

The kernel slides across the signal one step at a time, and each output is the dot product of the kernel with the window it currently covers.

Input:[1,2,3,4,5], [1,2,1]
Output:[8,12,16]
Explanation:

The kernel slides across the signal one step at a time, and each output is the dot product of the kernel with the window it currently covers.

Constraints

  • 1 ≤ kernel length ≤ signal length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.