Find the Largest and smallest number in an Array containing small as well as large numbers


We are given an array of strings and each string represents a number that could be more than the range of the maximum integer limit. We have to find the largest and the smallest element from the given array. We cannot use the simple less than or greater than operators to check which string is greater as it won't works find with the string so we will make our own comparison function.

Example

Let's understand the problem with the help of an example −

Input

string arr[] = {"2", "3", "12", "23", "22", "0", "7"}

Output

The smallest element in the given array is: 0
The largest element in the given array is: 23

Sorting Approach

In this approach, we are going to sort the given string using the STL sorting function with additional comparison function which we are going to define.

  • First, we will create a function which will compare the string by taking two string as the parameter and will return weather the first string which represents a number is smaller as compare to another string not in the dictionary order but in the numeric order.

  • We will create another function and in that function we are going to pass the given array and will sort it by using the STL sort function with the comparison function created above.

  • After sorting the first string will represent the smallest number and the last string will represent the largest number and we will print both of them.

Example

#include <bits/stdc++.h>
using namespace std;

// comparision function 
bool cmp(string& a, string& b){
   if(a.size() == b.size()){
      return a < b;
   } else {
      return a.size() < b.size();
   }   
}
void getValue(vector<string> arr){
   int len = arr.size(); // getting size of the array     
   // sorting the given array 
   sort(arr.begin(), arr.end(), cmp);    
   // first element is the smallest 
   cout<<"The smallest element in the given array is: " << arr[0]<<endl;    
   // last element is the largest 
   cout<<"The largest element in the given array is: " << arr[len-1]<<endl;
}
int main(){
   // given array 
   vector<string> arr = { "2", "3", "12", "23", "22", "0", "7"};
   // calling to the required function 
   getValue(arr);
   return 0;
}

Output

The smallest element in the given array is: 0
The largest element in the given array is: 23

Time and Space Complexity

The time complexity of the above code is O(N*M*log(N)), where N is the number of the given elements in the array, M is the maximum size of the given numbers and logarithmic factor is due to the sorting.

The space complexity of the above code is O(1) or constant, as we are not using any extra space in the above code.

Comparison Approach

In this approach, we are going to maintain two variables both will contains the first element of the given array and we will compare both the variables with the each element of the given array to get the smallest and the largest number among the given numbers.

We will create a function for the comparison of the numbers present in the from of the strings with each other by using the comparison function we have used in the previous code for the comparison.

By traversing over the array we will get the required string and print them at the end in the same function.

Example

#include <bits/stdc++.h>
using namespace std;

// comparision function 
bool isSmall(string& a, string& b){
   if(a.size() == b.size()){
      return a < b;
   } else {
      return a.size() < b.size();
   }   
}
void getValue(vector<string> arr){
   int len = arr.size(); // getting size of the array     
   // variable to store the current smallest number 
   string cur = arr[0];    
   // traversing over the array 
   for(int i=1; i<len; i++){
      if(isSmall(cur, arr[i]) == false){
         cur = arr[i];
      }
   }
   // printing the smallest element in the given array 
   cout<<"The smallest element in the given array is: " << cur<<endl;    
   // updating the value of cur to first 
   // now it will store the largest value 
   cur = arr[0];    
   // traversing over the array 
   for(int i=1; i<len; i++){
      if(isSmall(cur, arr[i])){
         cur = arr[i];
      }
   }    
   // printing the largest element in the given array 
   cout<<"The largest element in the given array is: " << cur <<endl;
}
int main(){
   // given array 
   vector<string> arr	= { "2", "3", "12", "23", "22", "0", "7"};
   // calling to the required function 
   getValue(arr);
   return 0;
}

Output

The smallest element in the given array is: 0
The largest element in the given array is: 23

Time and Space Complexity

We are traversing over the array only twice or linearly and for each comparison we are getting the more linear time so the time complexity will be O(N*M), where N is the size of the given array and M is the size of the largest number.

We are using an extra space here, make the space complexity factor of size of the largest integer that is O(M).

Conclusion

In this tutorial, we have learned how to find the smallest and the largest number among all the numbers present in the given array of string where each string represents a number that can be larger than the range of the integers. We have used two methods: by using the STL custom sort function with O(N*M*log(N)) time complexity and another method with the O(N*M) time complexity.

Updated on: 24-Aug-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements