701. Find Minimum Element in Array Recursively

EasyRecursionRecursion

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

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

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

Examples

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

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →