smallest and largest number

Find Smallest and Largest Number in Unsorted Array – O(n) Time

Given an input array of integers, your goal is to find both smallest and largest number present in the array in an effective way. Note, you need to achieve this in O(n) time i.e., you should be able to find the results by traversing through the array only once.

Algorithm:

Below is the efficient algorithm for doing this. While traversing through the array keep track of maximum and minimum numbers found so far and when you reach the end of the array, then you will get smallest and largest numbers present in the array.

Solution to find smallest and largest number

Output

Time Complexity – Single Traversal – O(n)

Space complexity – O(1)

Recommended Posts

Reference

Leave a Reply

avatar
  Subscribe  
Notify of