570. Find Middle Element of Stack

EasyStackStack

Given a non-empty stack represented as an array (index 0 is the bottom), return the middle element, defined as the element at 0-indexed position n/2 (integer division) counting from the bottom. The input is JSON {stack}.

Input: JSON {stack}.

Output: Integer — the middle element.

Examples

Example 1
Input: {"stack":[1,2,3,4,5]}
Output: 3
Explanation: Position 2 (0-indexed) of 5 elements.
Example 2
Input: {"stack":[10]}
Output: 10
Explanation: Single element is the middle.
Example 3
Input: {"stack":[1,2,3,4]}
Output: 3
Explanation: Position 2 of 4 elements.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →