Skip to content

Latest commit

 

History

History
47 lines (37 loc) · 1.79 KB

MaximizetheTopmostElementAfterKMoves.md

File metadata and controls

47 lines (37 loc) · 1.79 KB

The question

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 = 4
Output: 5
Explanation:
One of the ways we can end with 5 at the top of the pile after 4 moves is as follows:
- Step 1: Remove the topmost element = 5. The pile becomes [2,2,4,0,6].
- Step 2: Remove the topmost element = 2. The pile becomes [2,4,0,6].
- Step 3: Remove the topmost element = 2. The pile becomes [4,0,6].
- Step 4: Add 5 back onto the pile. The pile becomes [5,4,0,6].
Note that this is not the only way to end with 5 at the top of the pile. It can be shown that 5 is the largest answer possible after 4 moves.

Approach:- if array length is more than k, can do k-1 operations, but the last operation should be the maximum element added. if k is more, just choose the maximum one from all.

public int maximumTop(int[] nums, int k) {
        if(nums.length==1 && k%2!=0){
            return -1;
        }
        int max = 0;
        for(int i=0;i<Math.min(k-1, nums.length);i++){
            max = Math.max(max, nums[i]);
        }
        if(nums.length>k){
            max = Math.max(max, nums[k]);
        }
        return max;
    }

Time complexity - O(N) Space complexity - O(1)