Find Largest and smallest number in an Array containing small as well as large numbers
Given an array arr[] of N small and/or large numbers, the task is to find the largest and smallest number in this array.
Examples:
Input: N = 4, arr[] = {5677856, 657856745356587687698768, 67564, 45675645356576578564435647647}
Output: Smallest: 67564
Largest: 45675645356576578564435647647
Input: N = 5, arr[] = {56, 64, 765, 323, 4764}
Output: Smallest: 56
Largest: 4764
Input: N = 3, arr[] = {56, 56, 56}
Output: Smallest: 56
Largest: 56
Naive Approach: One easy way to solve this problem is use comparison-based sorting on all numbers, stored as strings. If the compared strings are of different length sort them on the basis of small length first. If the lengths are same, use compare function to find the first biggest non-matching character and deduce whether it belongs to first or second string and sort them whose first non-matching character’s ASCII value is smaller.
This way we will get final vector which has increasingly sorted strings on the basis of its numeral representation. The first string will be smallest and last string will be largest.
Time Complexity: O(N*M*log N)
- O(N*log N) to sort the array
- O(M) to compare two numbers digit by digit when their lengths are equal
Auxiliary Space: O(1)
Efficient Approach: To solve the problem efficiently follow the below idea:
This approach is similar to finding the biggest and smallest number in a numeral vector. The only difference is that there is need to check if the length of string as well since strings with big length will always form a bigger number than one with smaller length.
For Example: “3452” with length 4 will always be greater than “345” with length 3.
Similar is the case for smallest string.
Follow the steps to solve the problem:
- Initialize minLen to maximum possible number and maxLen to minimum number possible. Here minLen and maxLen represents length of smallest number and biggest number found till now.
- Traverse all strings one by one with i.
- Find the length of current string as numLen.
- If minLen > numLen assign minLen to numLen and smallest number as numbers[i]. Similarly If maxLen < numLen assign maxLen to numLen and biggest number as numbers[i].
- If minLen == numLen and maxLen == numLen then compare smallest number and biggest number found till now with the numbers[i]. The number with first greater non-matching character will be bigger and other will be smaller.
- Return the pair of smallest and biggest number as the final answer.
Below is the implementation of the above mentioned approach:
C++14
#include <bits/stdc++.h>
using namespace std;
pair<string, string> findLargestAndSmallest(
vector<string>& numbers)
{
int N = numbers.size();
int maxLen = 0, minLen = INT_MAX;
pair<string, string> res;
for ( int i = 0; i < N; i++) {
int numLen = numbers[i].length();
if (minLen > numLen) {
minLen = numLen;
res.first = numbers[i];
}
else if (minLen == numLen)
res.first
= res.first.compare(numbers[i]) < 0
? res.first
: numbers[i];
if (maxLen < numLen) {
maxLen = numLen;
res.second = numbers[i];
}
else if (maxLen == numLen)
res.second
= res.second.compare(numbers[i]) > 0
? res.second
: numbers[i];
}
return res;
}
int main()
{
vector<string> numbers
= { "5677856" , "657856745356587687698768" , "67564" ,
"45675645356576578564435647647" };
pair<string, string> ans
= findLargestAndSmallest(numbers);
cout << "Smallest: " << ans.first;
cout << endl;
cout << "Largest: " << ans.second;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static String[] findLargestAndSmallest(String[] numbers)
{
int N = numbers.length;
int maxLen = 0 , minLen = Integer.MAX_VALUE;
String[] res = { "" , "" };
for ( int i = 0 ; i < N; i++) {
int numLen = numbers[i].length();
if (minLen > numLen) {
minLen = numLen;
res[ 0 ] = numbers[i];
}
else if (minLen == numLen)
res[ 0 ] = ((res[ 0 ].length()
> numbers[i].length())
? res[ 0 ]
: numbers[i]);
if (maxLen < numLen) {
maxLen = numLen;
res[ 1 ] = numbers[i];
}
else if (maxLen == numLen)
res[ 1 ]
= (res[ 1 ].length() > numbers[i].length()
? res[ 1 ]
: numbers[i]);
}
return res;
}
public static void main(String[] args)
{
String[] numbers
= { "5677856" , "657856745356587687698768" ,
"67564" , "45675645356576578564435647647" };
String[] ans = findLargestAndSmallest(numbers);
System.out.println( "Smallest: " + ans[ 0 ]);
System.out.print( "Largest: " + ans[ 1 ]);
}
}
|
Python3
INT_MAX = 2147483647
def findLargestAndSmallest(numbers):
N = len (numbers)
maxLen,minLen = 0 ,INT_MAX
res = ["" for i in range ( 2 )]
for i in range (N):
numLen = len (numbers[i])
if (minLen > numLen):
minLen = numLen
res[ 0 ] = numbers[i]
elif (minLen = = numLen):
res[ 0 ] = res[ 0 ] if (res[ 0 ] < numbers[i]) else numbers[i]
if (maxLen < numLen):
maxLen = numLen
res[ 1 ] = numbers[i]
elif (maxLen = = numLen):
res[ 1 ] = res[ 1 ] if (res[ 1 ] > numbers[i]) else numbers[i]
return res
numbers = [ "5677856" , "657856745356587687698768" , "67564" , "45675645356576578564435647647" ]
ans = findLargestAndSmallest(numbers)
print (f "Smallest: {ans[0]}" )
print (f "Largest: {ans[1]}" )
|
C#
using System;
public class GFG {
static string [] findLargestAndSmallest( string [] numbers)
{
int N = numbers.Length;
int maxLen = 0;
int minLen = Int32.MaxValue;
string [] res = { "" , "" };
for ( int i = 0; i < N; i++) {
int numLen = numbers[i].Length;
if (minLen > numLen) {
minLen = numLen;
res[0] = numbers[i];
}
else if (minLen == numLen)
res[0]
= ((res[0].Length > numbers[i].Length)
? res[0]
: numbers[i]);
if (maxLen < numLen) {
maxLen = numLen;
res[1] = numbers[i];
}
else if (maxLen == numLen)
res[1] = (res[1].Length > numbers[i].Length
? res[1]
: numbers[i]);
}
return res;
}
public static void Main( string [] args)
{
String[] numbers
= { "5677856" , "657856745356587687698768" ,
"67564" , "45675645356576578564435647647" };
String[] ans = findLargestAndSmallest(numbers);
Console.WriteLine( "Smallest: " + ans[0]);
Console.WriteLine( "Largest: " + ans[1]);
}
}
|
Javascript
<script>
const INT_MAX = 2147483647;
const findLargestAndSmallest = (numbers) => {
let N = numbers.length;
let maxLen = 0, minLen = INT_MAX;
let res = new Array(2).fill( "" );
for (let i = 0; i < N; i++) {
let numLen = numbers[i].length;
if (minLen > numLen) {
minLen = numLen;
res[0] = numbers[i];
}
else if (minLen == numLen)
res[0]
= res[0] < numbers[i]
? res[0]
: numbers[i];
if (maxLen < numLen) {
maxLen = numLen;
res[1] = numbers[i];
}
else if (maxLen == numLen)
res[1]
= res[1] > numbers[i]
? res[1]
: numbers[i];
}
return res;
}
let numbers = [ "5677856" , "657856745356587687698768" , "67564" ,
"45675645356576578564435647647" ];
let ans = findLargestAndSmallest(numbers);
document.write(`Smallest: ${ans[0]}<br/>`);
document.write(`Largest: ${ans[1]}`);
</script>
|
Output
Smallest: 67564
Largest: 45675645356576578564435647647
Time Complexity: O(N*M), where N is the size of array, and M is the size of largest number.
- O(N) to traverse each number of the array
- O(M) to compare two numbers digit by digit when their lengths are equal
Auxiliary Space: O(1)
Last Updated :
03 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...