Stalin Sort - aloalgo

Stalin Sort

Easy

You are given an array of integers nums.

Your task is to apply Stalin sort: iterate through the array from left to right, keeping the first element and any subsequent element that is greater than or equal to the last kept element, removing all others.

You may assume that:

  • The array may be empty.

Return the resulting array.

Example 1

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

Keep 3 (first element). 1 < 3, remove. 2 < 3, remove. 4 >= 3, keep. 1 < 4, remove. 5 >= 4, keep.

Example 2

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

The array is already non-decreasing, so all elements are kept.

Example 3

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

2 < 3 and 1 < 3, so only the first element remains.

Loading...
Input
[3, 1, 2, 4, 1, 5]
Output
[3, 4, 5]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!