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.


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


Time Complexity – Single Traversal – O(n)

Space complexity – O(1)

Recommended Posts


Leave a Reply

Notify of