Given an array A of size N of integers. Your task is to find the minimum and maximum elements in the array.
GeekForGeek Problem - Link | Click Here
class Compute
{
static Pair getMinMax(long a[], long n)
{
// Handle empty array case
if (n <= 0) {
return null;
}
// Initialize minimum and maximum with the first element
long min = a[0];
long max = a[0];
// Traverse through the array to find min and max
for (int i = 1; i < n; i++) {
// Update min if current element is smaller
if (a[i] < min) {
min = a[i];
}
// Update max if current element is larger
else if (a[i] > max) {
max = a[i];
}
}
// Return Pair with min and max values found
return new Pair(min, max);
}
}