Max Element in Stack

Given a stack represented by an array of integers (with the top of the stack at the end of the array), find and return the maximum element in the stack.

Input Format

nums = elements of stack (last element is top)

Output Format

maximum element in the stack

Constraints

Examples

Example 1:

Input:

nums = [3,1,4,2]

Output:

4

Explanation:

The maximum element is 4.

Example 2:

Input:

nums = [-2,-5,-1]

Output:

-1

Explanation:

All elements are negative; max is -1.

Loading...
Max Element in Stack - Stack DSA Problem