700. Find Maximum Element in Array Recursively

EasyRecursionRecursion

Given an integer array nums of size n (n ≥ 1), find and return the maximum element using recursion. At each recursive step, compare the first element with the maximum of the remaining subarray.

Input: An integer array nums (1 ≤ n ≤ 10^5, -10^9 ≤ nums[i] ≤ 10^9).

Output: A single integer — the maximum element in nums.

Examples

Example 1
Input: [3, 1, 4, 1, 5, 9, 2, 6]
Output: 9
Explanation: max([3,1,4,1,5,9,2,6]): compare 3 with max([1,4,1,5,9,2,6])=9 → 9.
Example 2
Input: [1]
Output: 1
Explanation: Single element — base case returns 1.
Example 3
Input: [-5, -1, -3, -2]
Output: -1
Explanation: All negatives; max is -1.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →