Given an array of integers nums, sort only the elements that are multiples of 5 in descending order while keeping all other elements in their original positions.
A multiple of 5 is any integer x such that x % 5 == 0. Note that 0 is considered a multiple of 5.
Return the modified array after sorting the multiples of 5 in place.
Function Signature
def sort_multiples_of_five(nums: list[int]) -> list[int]:
Rules
-
Zero and negative multiples such as
-10
count as multiples of 5. Multiples are compared by numeric value, so
5 > 0 > -5
.
-
Every element that is not a multiple of 5 stays at its index.
-
The multiples of 5 are rearranged among the indices where multiples of 5 appear in the input, so that, read from left to right, they are in descending order.
-
If
nums
has no multiple of 5, return it unchanged. You may modify
nums
and return it.
Constraints
-
1 <= len(nums) <= 10^4
-
-10^5 <= nums[i] <= 10^5
Examples
Input: nums = [3,10,7,20,5,2,15]
Output: [3,20,7,15,10,2,5]
The multiples of 5 are 10, 20, 5 and 15 at indices 1, 3, 4 and 6. Sorted in descending order they are 20, 15, 10, 5, and writing them back into those indices gives the output, with 3, 7 and 2 untouched.
Input: nums = [25,1,30,2,10,3]
Output: [30,1,25,2,10,3]
The multiples 25, 30 and 10 sit at indices 0, 2 and 4. In descending order they are 30, 25, 10, which fill those indices from left to right.
Input: nums = [-5,4,0,-15,6,5]
Output: [5,4,0,-5,6,-15]
The multiples are -5, 0, -15 and 5 at indices 0, 2, 3 and 5. In descending order they are 5, 0, -5, -15, and 4 and 6 keep their places.