Find Minimum Using Recursion in Java
beginner· Recursion · Recursion
Problem
Finding an array's minimum recursively means comparing the current element against the smallest value in everything after it, reducing the whole array to a chain of two-way comparisons.
Given an array of integers, find its smallest element using recursion.
Input
[12, 45, 7, 23, 56, 3]
Output
Minimum: 3
Java Program
Java
public class FindMinimumRecursion {
static int min(int[] arr, int index) {
if (index == arr.length - 1) return arr[index]; // last element is its own minimum
int restMin = min(arr, index + 1);
return Math.min(arr[index], restMin);
}
public static void main(String[] args) {
int[] arr = {12, 45, 7, 23, 56, 3};
System.out.println("Minimum: " + min(arr, 0));
}
}Output
Minimum: 3
Core Logic
Comparing the current element against the minimum of the remaining elements, and keeping whichever is smaller, works down to the last element and then unwinds with the running minimum.
How It Works
- 1
min(arr, index)tracks how far into the array the current call has reached. - 2The base case
if (index == arr.length - 1) return arr[index];fires at the last index, since a single element is trivially its own minimum. - 3Every other call first finds
restMin, the minimum of everything after the current index, then returnsMath.min(arr[index], restMin). - 4The very first call,
min(arr, 0), ends up holding the overall minimum once every deeper call has returned.
For
[12, 45, 7, 23, 56, 3], the base case returns 3 at the last index, and every call unwinding above it compares its own element against 3 — none of them are smaller, so 3 stays the minimum all the way back to the top.💡
Key Point: This mirrors Find Maximum Using Recursion exactly, with Math.min() in place of Math.max() — the same shrink-then-unwind structure works for either extreme.
Complexity
Time Complexity: O(n)Space Complexity: O(n)
Why: One recursive call handles one array element, so both the call count and the stack depth grow with the array's length n.
Key Concepts
recursionindex parameterbase case