Smallest and Largest element in an Array

Definition & Explanation

This program aims to find the smallest and largest elements in a given array of integers. It implements a simple algorithm that iterates through the array, updating variables to keep track of the smallest and largest values encountered. The program begins by initializing these variables with the first element of the array and then traverses the remaining elements, updating the variables as necessary. Once all elements have been checked, the program outputs the smallest and largest values found. This functionality is encapsulated within the find_min_max function, which takes an array as input and returns the smallest and largest elements. The program demonstrates the use of basic iteration and conditional statements to solve the problem efficiently.

Program Logic

  1. Initialize Variables:
    • Create variables min_val and max_val to store the smallest and largest values, respectively.
    • Set both variables to the first element of the input array.
  2. Iterate Through the Array:
    • Start iterating through the input array from the second element onwards (index 1).
  3. Update Smallest and Largest:
    • For each element in the array:
      • Check if the current element is less than min_val.
        • If true, update min_val to the current element.
      • Check if the current element is greater than max_val.
        • If true, update max_val to the current element.
  4. Repeat:
    • Repeat step 3 for all elements in the array.
  5. Output:
    • Once the iteration is complete, the variables min_val and max_val will hold the smallest and largest values found in the array, respectively.
    • Output these values as the result.

This logic ensures that the program efficiently finds the smallest and largest elements in the given array by iterating through the array only once and updating the variables accordingly.

C

Method 1 :

#include <stdio.h>

void findMinMax(int arr[], int n, int *min_val, int *max_val) {
    if (n <= 0) {
        *min_val = *max_val = 0; // Handle empty array case
        return;
    }

    *min_val = *max_val = arr[0]; // Initialize min_val and max_val with first element

    for (int i = 1; i < n; i++) {
        if (arr[i] < *min_val) {
            *min_val = arr[i]; // Update min_val
        } else if (arr[i] > *max_val) {
            *max_val = arr[i]; // Update max_val
        }
    }
}

int main() {
    int arr[] = {3, 5, 1, 9, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    int min_val, max_val;

    findMinMax(arr, n, &min_val, &max_val);

    printf("Smallest: %d\n", min_val);
    printf("Largest: %d\n", max_val);

    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
#include <climits>

void findMinMax(int arr[], int n, int &min_val, int &max_val) {
    if (n <= 0) {
        min_val = max_val = 0; // Handle empty array case
        return;
    }

    min_val = max_val = arr[0]; // Initialize min_val and max_val with first element

    for (int i = 1; i < n; i++) {
        if (arr[i] < min_val) {
            min_val = arr[i]; // Update min_val
        } else if (arr[i] > max_val) {
            max_val = arr[i]; // Update max_val
        }
    }
}

int main() {
    int arr[] = {3, 5, 1, 9, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    int min_val, max_val;

    findMinMax(arr, n, min_val, max_val);

    std::cout << "Smallest: " << min_val << std::endl;
    std::cout << "Largest: " << max_val << std::endl;

    return 0;
}

Output :

JAVA

Method 1 :

public class MinMaxInArray {
    public static void findMinMax(int[] arr, int[] result) {
        if (arr.length == 0) {
            result[0] = result[1] = 0; // Handle empty array case
            return;
        }

        int minVal = Integer.MAX_VALUE;
        int maxVal = Integer.MIN_VALUE;

        for (int num : arr) {
            if (num < minVal) {
                minVal = num; // Update minVal
            }
            if (num > maxVal) {
                maxVal = num; // Update maxVal
            }
        }

        result[0] = minVal;
        result[1] = maxVal;
    }

    public static void main(String[] args) {
        int[] arr = {3, 5, 1, 9, 2};
        int[] result = new int[2];

        findMinMax(arr, result);

        System.out.println("Smallest: " + result[0]);
        System.out.println("Largest: " + result[1]);
    }
}

Output :

Python

Method 1 :

arr=[10,89,9,56,4,80,8]
mini=arr[0]
maxi=arr[0]

for i in range(len(arr)):
    if arr[i]<mini:
        mini=arr[i]
    if arr[i]>maxi:
        maxi=arr[i]


print(mini)
print(maxi)

Output :

4
89