Find smallest and largest element in an array | faceprep

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

  1. Input the array elements.
  2. Initialize small = large = arr[0]
  3. Repeat from i = 2 to n
  4.                if(arr[i] > large)
  5.                     large = arr[i]
  6.                if(arr[i] < small)
  7.                     small = arr[i]
  8. Print small and large.

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

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

#include<stdio.h>

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<n;++i)
scanf(“%d”,&a[i]);

large=small=a[0];

for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];

if(a[i]<small)
small=a[i];
}

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

return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,*n,i;
int max(int a,int*n);
scanf(“%d”,&a);
n=(int*)malloc(a*sizeof(int));
for(i=0;i<a;i++)
{
scanf(“%d”,n+i);
}
printf(“%d”,max(a,n));
return 0;
}
int max(int a,int *n)
{
int i,m=*n;
for(i=0;i<a;i++)
{
if(m<=(*(n+i)))
{
m=*(n+i);
}
}
return m;
}

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

#include<iostream>
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<n;++i)
cin >> a[i];

large=small=a[0];

for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];

if(a[i]<small)
small=a[i];
}

cout << “\nThe smallest element is ” << small << endl;
cout << “\nThe largest element is ” << large << endl;

return 0;
}

// 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<n;++i)
{
if(a[i]>large)
large=a[i];

if(a[i]<small)
small=a[i];
}

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

# 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 number ‘))
arr.append(numbers)
print(“Maximum element : “, max(arr), “\nMinimum element : “, min(arr))

Output:

smallest and largest elements in an array

Time complexity: O(n)

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

// 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 <stdio.h>
#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[index + 1]) ? array[index] : array[index + 1];
}

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

return (array[index] < min) ? array[index] : min;
}

int main()
{
int array[MAX], N, max, min;
int i;
printf(“Enter size of the array : “);
scanf(“%d”, &N);
printf(“Input teh array elements : “, N);
for(i=0; i<N; 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++ program to find smallest and largest elements in array using recursion */

#include <iostream>
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[index + 1]) ? array[index] : array[index + 1];
}

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

return (array[index] < min) ? 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<N; i++)
{
cin >> array[i];
}

max = maximum(array, 0, N);
min = minimum(array, 0, N);

cout << “Smallest element in array is ” << min << endl;
cout << “Largest element in array is ” << max << endl;

return 0;
}

// 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[index + 1]) ? array[index] : array[index + 1];
}

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

return (array[index] < min) ? 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 );
}
}

Time complexity: O(n)

 

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

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

#include<bits/stdc++.h>
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<n;++i)
cin >> a[i];

sort(a,a+n);

cout << “\nThe smallest element is ” << a[0] << endl;
cout << “\nThe largest element is ” << a[n-1] << endl;

return 0;
}

// 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);
}
}

Time complexity: O(n log n)

Output: 

smallest and largest elements in an array