Given an integer array nums and an integer k, perform the following operations:
-
Rotate the array to the right by
k
steps.
-
After rotation, find the most frequent element in the rotated array. If there are multiple elements with the same highest frequency, return the smallest one.
Return a list containing two elements: the rotated array and the most frequent element.
Function Signature
def rotate_and_find_mode(nums: list[int], k: int) -> list:
Rules
-
Rotating right by one step moves the last element to the front. After
k
steps, the element at index
i
ends up at index
(i + k) % n
, where
n
is the length of
nums
.
-
k
can be larger than
n
.
-
The return value is
[rotated, most_frequent]
, where
rotated
is a list of integers and
most_frequent
is an integer.
-
When several values share the highest frequency,
most_frequent
is the smallest of them.
Constraints
-
1 <= nums.length <= 10^5
-
-10^4 <= nums[i] <= 10^4
-
0 <= k <= 10^5
Examples
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [[5,6,7,1,2,3,4],1]
Rotating [1,2,3,4,5,6,7] right by 3 moves the last three elements to the front, giving [5,6,7,1,2,3,4]. All elements appear once, so the smallest of them, 1, is the answer.
Input: nums = [1,1,2,2,2,3,3], k = 2
Output: [[3,3,1,1,2,2,2],2]
Rotating right by 2 gives [3,3,1,1,2,2,2]. Element 2 appears 3 times, more than any other value.
Input: nums = [-2,4,4,-2,7], k = 7
Output: [[-2,7,-2,4,4],-2]
With n = 5, rotating by 7 is the same as rotating by 2, which moves [-2,7] to the front. Both -2 and 4 appear twice, and -2 is the smaller value.