How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Difficulty: Medium
Topics: Greedy, Sorting
You are given an array happiness of length n, and a positive integer k.
There are n children standing in a queue, where the iแตสฐ child has a happiness value of happiness[i].
You want to select k children from these n children in k turns.
Return the maximum possible sum of happiness values of the selected children after selecting exactly k children.
Input:
happiness = [1, 2, 3]
k = 2
Output:
4
Explanation:
[0, 1][0]Total happiness = 3 + 1 = 4
Input:
happiness = [1, 1, 1, 1]
k = 2
Output:
1
Explanation:
[0, 0, 0]Total happiness = 1 + 0 = 1
Input:
happiness = [2, 3, 4, 5]
k = 1
Output:
5
Explanation:
[1, 2, 3]Total happiness = 5
1 โค n == happiness.length โค 2 ร 10โต1 โค happiness[i] โค 10โธ1 โค k โค n๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.