Posted on by Kalkicode
Code Array

# Find the largest and smallest element in array

The problem being addressed is to find the smallest and largest elements in an array of integers. This task involves searching through the array to identify the smallest and largest values present in it. Such an operation is useful in various scenarios, such as determining the lowest and highest temperatures in a dataset or finding the range of values within a dataset.

## Problem Statement and Description

Given an array of integers, the objective is to determine the smallest and largest values present in the array. For example, consider the array `[10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7]`. The smallest element in this array is -1, and the largest element is 44.

## Idea to Solve the Problem

To find the smallest and largest elements in the array, we can iterate through the array while maintaining two variables: one for the smallest value (`smallest`) and one for the largest value (`largest`). As we traverse the array, we compare each element with these variables and update them accordingly. By the end of the iteration, we will have identified the smallest and largest elements.

## Pseudocode

``````find_min_max(arr, size):
if size <= 1:
return
else:
smallest = arr[0]
largest = arr[0]

for i in range(0, size):
if arr[i] < smallest:
smallest = arr[i]
if arr[i] > largest:
largest = arr[i]

print "Smallest: ", smallest
print "Largest: ", largest``````

## Algorithm Explanation

1. Check if the size of the array is less than or equal to 1. If so, there can't be both a smallest and largest element, so we return.
2. Initialize two variables, `smallest` and `largest`, with the value of the first element in the array.
3. Iterate through the array. For each element `arr[i]`:
• Compare `arr[i]` with `smallest`. If it's smaller than `smallest`, update `smallest` with the value of `arr[i]`.
• Compare `arr[i]` with `largest`. If it's larger than `largest`, update `largest` with the value of `arr[i]`.
4. After the iteration, the `smallest` variable will contain the smallest element, and the `largest` variable will contain the largest element.
5. Print the values of `smallest` and `largest`.

## Program

``````//C Program
//Find the largest and smallest element in array
#include <stdio.h>

void find_min_max(int arr[],int size)
{

if(size<=1)
{
return;
}

else{

//Get first element of array
int smallest = arr[0];
int largest  = arr[0];

for (int i = 0; i < size; i++)
{
//compare the array element values
if(smallest > arr[i])
{
//When get a new small element
smallest = arr[i];
}
if(largest < arr[i])
{
//When get a new larger element
largest = arr[i];
}
}

//Display result
printf("Smallest : %d\n",smallest);

printf("Largest  : %d\n",largest);

}
}

int main()
{

//Define collection of array elements
int arr[] ={10,3,44,8,3,2,1,-1,6,8,9,4,5,7};

//Get the size of array
int size=sizeof(arr) / sizeof(arr[0]);

find_min_max(arr,size);

return 0;

}```
```

#### Output

``````Smallest : -1
Largest  : 44``````
``````/*
C++ Program
Find the largest and smallest element in array
*/
#include<iostream>
using namespace std;

class MyArray {
public:
void find_min_max(int arr[], int size) {
if (size <= 1) {
return;
} else {
//Get first element of array
int smallest = arr[0];
int largest = arr[0];
for (int i = 0; i < size; i++) {
//compare the array element values

if (smallest > arr[i]) {
//When get a new small element
smallest = arr[i];
}
if (largest < arr[i]) {
//When get a new larger element
largest = arr[i];
}
}
//Display result

cout << "Smallest : " << smallest << "\n";
cout << "Largest : " << largest << "\n";
}
}
};
int main() {
MyArray obj = MyArray();
int arr[] = {
10,
3,
44,
8,
3,
2,
1,
-1,
6,
8,
9,
4,
5,
7
};
//Get the size of array
int size = sizeof(arr) / sizeof(arr[0]);
obj.find_min_max(arr, size);
return 0;
}```
```

#### Output

``````Smallest : -1
Largest : 44``````
``````/*
Java Program
Find the largest and smallest element in array
*/

public class MyArray {

public void find_min_max(int []arr,int size)
{
if(size<=1)
{
return;
}
else
{
//Get first element of array
int smallest = arr[0];
int largest  = arr[0];

for (int i = 0; i < size; i++)
{
//compare the array element values
if(smallest > arr[i])
{
//When get a new small element
smallest = arr[i];
}
if(largest < arr[i])
{
//When get a new larger element
largest = arr[i];
}
}

//Display result
System.out.print("Smallest : "+smallest+"\n");

System.out.print("Largest  : "+largest+"\n");

}
}
public static void main(String[] args) {

MyArray obj = new MyArray();

//Define collection of array elements
int []arr = {10,3,44,8,3,2,1,-1,6,8,9,4,5,7};

//Get the size of array
int size = arr.length;

obj.find_min_max(arr,size);
}
}
```
```

#### Output

``````Smallest : -1
Largest : 44``````
``````/*
C# Program
Find the largest and smallest element in array
*/
using System;

public class MyArray {
public void find_min_max(int[] arr, int size) {
if (size <= 1) {
return;
} else {
//Get first element of array
int smallest = arr[0];
int largest = arr[0];
for (int i = 0; i < size; i++) {
//compare the array element values

if (smallest > arr[i]) {
//When get a new small element
smallest = arr[i];
}
if (largest < arr[i]) {
//When get a new larger element
largest = arr[i];
}
}
Console.Write("Smallest : " + smallest + "\n");
Console.Write("Largest : " + largest + "\n");
}
}
public static void Main(String[] args) {
MyArray obj = new MyArray();
int[]
//Define collection of array elements
arr = {
10,
3,
44,
8,
3,
2,
1,
-1,
6,
8,
9,
4,
5,
7
};
//Get the size of array
int size = arr.Length;
obj.find_min_max(arr, size);
}
}```
```

#### Output

``````Smallest : -1
Largest : 44``````
``````<?php
/*
Php Program
Find the largest and smallest element in array
*/
class MyArray {
public  function find_min_max(\$arr, \$size) {
if (\$size <= 1) {
return;
} else {
//Get first element of array
\$smallest = \$arr[0];
\$largest = \$arr[0];
for (\$i = 0; \$i < \$size; \$i++) {
//compare the array element values

if (\$smallest > \$arr[\$i]) {
//When get a new small element
\$smallest = \$arr[\$i];
}
if (\$largest < \$arr[\$i]) {
//When get a new larger element
\$largest = \$arr[\$i];
}
}
//Display result

echo("Smallest : ". \$smallest ."\n");
echo("Largest : ". \$largest ."\n");
}
}
}

function main() {
\$obj = new MyArray();
//Define collection of array elements
\$arr = array(10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7);
//Get the size of array
\$size = count(\$arr);
\$obj->find_min_max(\$arr, \$size);

}
main();```
```

#### Output

``````Smallest : -1
Largest : 44``````
``````/*
Node Js Program
Find the largest and smallest element in array
*/
class MyArray {
find_min_max(arr, size) {
if (size <= 1) {
return;
} else {
//Get first element of array
var smallest = arr[0];
var largest = arr[0];
for (var i = 0; i < size; i++) {
//compare the array element values

if (smallest > arr[i]) {
//When get a new small element
smallest = arr[i];
}

if (largest < arr[i]) {
//When get a new larger element
largest = arr[i];
}
}

//Display result

process.stdout.write("Smallest : " + smallest + "\n");
process.stdout.write("Largest : " + largest + "\n");
}
}
}

function main(args) {
var obj = new MyArray();
//Define collection of array elements
var arr = [10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7];
//Get the size of array
var size = arr.length;
obj.find_min_max(arr, size);
}

main();```
```

#### Output

``````Smallest : -1
Largest : 44``````
``````# Python 3 Program
# Find the largest and smallest element in array
class MyArray :
def find_min_max(self, arr, size) :
if (size <= 1) :
return
else :
smallest = arr[0]
largest = arr[0]
i = 0
while (i < size) :
# compare the array element values
if (smallest > arr[i]) :
# When get a new small element
smallest = arr[i]

if (largest < arr[i]) :
# When get a new larger element
largest = arr[i]

i += 1

print("Smallest : ", smallest ,"\n", end = "")
print("Largest : ", largest ,"\n", end = "")

def main() :
obj = MyArray()
# Define collection of array elements
arr = [10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7]
# Get the size of array
size = len(arr)
obj.find_min_max(arr, size)

if __name__ == "__main__":
main()```
```

#### Output

``````Smallest :  -1
Largest :  44``````
``````# Ruby Program
# Find the largest and smallest element in array
class MyArray
def find_min_max(arr, size)
if (size <= 1)
return
else
smallest = arr[0]
largest = arr[0]
i = 0
while (i < size)
# compare the array element values

if (smallest > arr[i])
# When get a new small element
smallest = arr[i]
end
if (largest < arr[i])
# When get a new larger element
largest = arr[i]
end
i += 1
end
print("Smallest  : ", smallest ,"\n")
print("Largest  : " , largest ,"\n")
end
end
end
def main()
obj = MyArray.new()
# Define collection of array elements
arr = [10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7]
# Get the size of array
size = arr.length
obj.find_min_max(arr, size)
end
main()```
```

#### Output

``````Smallest  : -1
Largest  : 44
``````
``````/*
Scala Program
Find the largest and smallest element in array
*/
class MyArray {
def find_min_max(arr: Array[Int], size: Int): Unit = {
if (size <= 1) {
return;
} else {
var smallest: Int = arr(0);
var largest: Int = arr(0);
var i: Int = 0;
while (i < size) {
//compare the array element values

if (smallest > arr(i)) {
//When get a new small element
smallest = arr(i);
}
if (largest < arr(i)) {
//When get a new larger element
largest = arr(i);
}
i += 1;
}
print("Smallest : " + smallest + "\n");
print("Largest : " + largest + "\n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyArray = new MyArray();

//Define collection of array elements
val arr: Array[Int] = Array(10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7);

//Get the size of array
val size: Int = arr.length;
obj.find_min_max(arr, size);
}
}```
```

#### Output

``````Smallest : -1
Largest : 44``````
``````/*
Swift Program
Find the largest and smallest element in array
*/
class MyArray {
func find_min_max(_ arr: [Int], _ size: Int) {
if (size <= 1) {
return;
} else {
var smallest: Int = arr[0];
var largest: Int = arr[0];
var i: Int = 0;
while (i < size) {
//compare the array element values

if (smallest > arr[i]) {
//When get a new small element
smallest = arr[i];
}
if (largest < arr[i]) {
//When get a new larger element
largest = arr[i];
}
i += 1;
}
print("Smallest : ", smallest ,"\n", terminator: "");
print("Largest : ", largest ,"\n", terminator: "");
}
}
}
func main() {
let obj: MyArray = MyArray();
//Define collection of array elements
let arr: [Int] = [10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7];
//Get the size of array
let size: Int = arr.count;
obj.find_min_max(arr, size);
}
main();```
```

#### Output

``````Smallest :  -1
Largest :  44``````

## Time Complexity Analysis

The algorithm iterates through the array once, performing constant-time comparisons and updates for each element. As a result, the time complexity of the algorithm is O(n), where n is the size of the input array. This linear time complexity indicates that the algorithm's execution time increases linearly with the size of the input.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post