educative.io

Another Algo to find second max number

Sort the array in Ascending form and pick the second last element from the array.


Course: https://www.educative.io/courses/data-structures-interviews-cs
Lesson: Solution Review: Finding the Second Maximum Value in an Array - Data Structures for Coding Interviews in C#

Hello Ahmed,
The suggested solution is totally fine, but we can’t use it because:
1- Sorting the array will increase the time complexity it gives time complexity of O(nlogn).
2- Sorting will also increase the space complexity.
The given solution in the course are the best case solution in time complexity of O(n) and space complexity of O(1).

1 Like