Maximize the Topmost Element After K Moves
You are given a 0-indexed integer array nums representing the contents of a pile, where nums[0] is the topmost element of the pile.
In one move, you can perform either of the following:
- If the pile is not empty, remove the topmost element of the pile.
- If there are one or more removed elements, add any one of them back onto the pile. This element becomes the new topmost element.
You are also given an integer k, which denotes the total number of moves to be made.
Return the maximum value of the topmost element of the pile possible after exactly k moves. In case it is not possible to obtain a non-empty pile after k moves, return -1.
Example 1
Input
nums = [5,2,2,4,0,6], k = 4Output
5After removing 5, 2, and 2, adding 5 back on the fourth move makes 5 the topmost element, and it is the largest possible answer.
Example 2
Input
nums = [2], k = 1Output
-1The only move removes the single element, so it is not possible to obtain a non-empty pile after one move.
Constraints
- 1 <= nums.length <= 10^5
- 0 <= nums[i], k <= 10^9