### Select Articles

Finding the Smallest and Largest Element in an Array

Program to find the smallest and largest elements in an array is discussed here. Given an array, the task is to find the largest and smallest elements of the array.

Method 1: Traverse the array iteratively and keep track of the smallest and largest element until the end of the array.

Method 2: Traverse the array recursively and keep track of the smallest and largest element until the end of the array.

Method 3: Sort the array using STL and return the first element as the smallest element and the last element as the largest element.

For example, consider the array.

arr = {1, 2, 3, 4, 5}

Smallest element : 1

Largest element : 5

Algorithm to find the smallest and largest numbers in an array

Input the array elements.

Initialize small = large = arr[0]

Repeat from i = 2 to n

if(arr[i] > large)

large = arr[i]

if(arr[i] < small>

small = arr[i]

Print small and large.

Iterative program to find the smallest and largest elements in an array

C

/ C program to find the smallest and largest element in an array

#include

int main()

{

int a[50],i,n,large,small;

printf(“\nEnter the number of elements : “);

scanf(“%d”,&n);

printf(“\nInput the array elements : “);

for(i=0;i

scanf(“%d”,&a[i]);

large=small=a[0];

for(i=1;i

{

if(a[i]>large)

large=a[i];

if(a[i]

small=a[i];

}

printf(“\nThe smallest element is %d\n”,small);

printf(“\nThe largest element is %d\n”,large);

return 0;

}

C++

// C++ program to find the smallest and largest element in an array

#include

using namespace std;

int main()

{

int a[50],i,n,large,small;

cout << “\nEnter the number of elements : “;

cin >> n;

cout << “\nInput the array elements : “;

for(i=0;i

cin >> a[i];

large=small=a[0];

for(i=1;i

{

if(a[i]>large)

large=a[i];

if(a[i]

small=a[i];

}

cout << “\nThe smallest element is ” << small>

cout << “\nThe largest element is ” << large>

return 0;

}

JAVA

// Java program to find the smallest and largest element in an array

import java.util.*;

class Main

{

public static void main(String args[])

{

int large,small,i;

int a[] = new int[]{1, 2, 3, 4, 5};

int n = a.length;

large=small=a[0];

for(i=1;i

{

if(a[i]>large)

large=a[i];

if(a[i]

small=a[i];

}

System.out.print(“\nThe smallest element is ” + small );

System.out.print(“\nThe largest element is ” + large );

}

}

PYTHON 3

# Python program to find the smallest and largest elements in an array

arr = []

num = int(input(‘How many numbers: ‘))

for n in range(num):

numbers = int(input(‘Enter numbers ‘))

arr.append(numbers)

print(“Maximum element : “, max(arr), “\nMinimum element : “, min(arr))

Output

How many numbers : 5

Enter numbers : 1 2 3 4 5

Maximum element : 5

Minimum element : 1

Time complexity: O(n)

Recursive program to find the smallest and largest element in an array

C

// C program to find the smallest and largest element in an array using recursion

/* C program to find smallest and largest elements in array using recursion */

#include

#define MAX 50 //Maximum size of the array

/* Recursive function to find the largest element in the given array */

int maximum(int array[], int index, int len)

{

int max;

if(index >= len-2)

return (array[index] > array[index + 1]) ? array[index] : array[index + 1];

max = maximum(array, index + 1, len);

return (array[index] > max) ? array[index] : max;

}

/* Recursive function to find the smallest element in the array */

int minimum(int array[], int index, int len)

{

int min;

if(index >= len-2)

{

return (array[index] < array>

}

min = minimum(array, index + 1, len);

return (array[index] < min>

}

int main()

{

int array[MAX], N, max, min;

int i;

printf(“Enter size of the array : “);

scanf(“%d”, &N);

printf(“Input the array elements : “, N);

for(i=0; i

{

scanf(“%d”, &array[i]);

}

max = maximum(array, 0, N);

min = minimum(array, 0, N);

printf(“Smallest element in array is %d\n”, min);

printf(“Largest element in array is %d\n”, max);

return 0;

}

C++

/* C++ program to find smallest and largest elements in array using recursion */

#include

using namespace std;

#define MAX 50 //Maximum size of the array

/* Recursive function to find the largest element in the given array */

int maximum(int array[], int index, int len)

{

int max;

if(index >= len-2)

return (array[index] > array[index + 1]) ? array[index] : array[index + 1];

max = maximum(array, index + 1, len);

return (array[index] > max) ? array[index] : max;

}

/* Recursive function to find the smallest element in the array */

int minimum(int array[], int index, int len)

{

int min;

if(index >= len-2)

{

return (array[index] < array>

}

min = minimum(array, index + 1, len);

return (array[index] < min>

}

int main()

{

int array[MAX], N, max, min;

int i;

cout << “Enter size of the array : “;

cin >> N;

printf(“Input the array elements : “, N);

for(i=0; i

{

cin >> array[i];

}

max = maximum(array, 0, N);

min = minimum(array, 0, N);

cout << “Smallest element in array is ” << min>

cout << “Largest element in array is ” << max>

return 0;

}

JAVA

// Java program to find the smallest and largest element in an array

import java.util.*;

class Main

{

// Recursive function to find the largest element

static int maximum(int array[], int index, int len)

{

int max;

if(index >= len-2)

return (array[index] > array[index + 1]) ? array[index] : array[index + 1];

max = maximum(array, index + 1, len);

return (array[index] > max) ? array[index] : max;

}

//Recursive function to find the smallest element

static int minimum(int array[], int index, int len)

{

int min;

if(index >= len-2)

{

return (array[index] < array>

}

min = minimum(array, index + 1, len);

return (array[index] < min>

}

public static void main(String args[])

{

int array[] = new int[]{1, 2, 3, 4, 5};

int max, min;

int N = 5;

max = maximum(array, 0, N);

min = minimum(array, 0, N);

System.out.print(“\nThe smallest element is ” + min );

System.out.print(“\nThe largest element is ” + max );

}

}

Output

Smallest element in array is 1

Largest element in array is 5

Time complexity: O(n)

Program to find the smallest and largest element in an array using STL?

C++

// C++ program to find the largest and smallest elements in an array using STL

#include

using namespace std;

int main()

{

int a[50],i,n,large,small;

cout << “\nEnter the number of elements : “;

cin >> n;

cout << “\nInput the array elements : “;

for(i=0;i

cin >> a[i];

sort(a,a+n);

cout << “\nThe smallest element is ” << a>

cout << “\nThe largest element is ” << a>

return 0;

}

JAVA

// Java program to find the smallest and largest element in an array

import java.util.Arrays;

import java.util.Collections;

public class Main {

public static void main(String[] args)

{

// Initializing array of integers

Integer[] num = { 1, 2, 3, 4, 5};

// using Collections.min() to find minimum element

int min = Collections.min(Arrays.asList(num));

// using Collections.max() to find maximum element

int max = Collections.max(Arrays.asList(num));

// printing minimum and maximum numbers

System.out.println(“Minimum number of array is : ” + min);

System.out.println(“Maximum number of array is : ” + max);

}

}

Output

Minimum number of array is : 1

Maximum number of array is : 5

Time complexity: O(n log n)