← All problems
#29 Top K Selection Medium

Implement a GPU program that, given a 1D array input of 32-bit floating point numbers of length N, selects the k largest elements and writes them in descending order to the output array of length k.

Implementation Requirements

Example 1:

  Input:
  input = [1.0, 5.0, 3.0, 2.0, 4.0]
  N = 5
  k = 3

  Output:
  output = [5.0, 4.0, 3.0]
  

Example 2:

  Input:
  input = [7.2, -1.0, 3.3, 8.8, 2.2]
  N = 5
  k = 2

  Output:
  output = [8.8, 7.2]
  

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.