package com.thealgorithms.maths;
import java.util.Arrays;
import java.util.Random;
public class FindMinRecursion {
public static void main(String[] args) {
Random rand = new Random();
int size = rand.nextInt(100) + 1;
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = rand.nextInt() % 100;
}
assert min(array, 0, array.length - 1) == Arrays.stream(array).min().getAsInt();
assert min(array) == Arrays.stream(array).min().getAsInt();
}
public static int min(int[] array, int low, int high) {
if (low == high) {
return array[low];
}
int mid = (low + high) >>> 1;
int leftMin = min(array, low, mid);
int rightMin = min(array, mid + 1, high);
return leftMin > rightMin ? rightMin : leftMin;
}
public static int min(int[] array) {
return array.length == 1 ? array[0] : min(array, 0, array.length);
}
}