
Problem
Solution
Submissions
Find the Second Largest Number in a List
Certification: Basic Level
Accuracy: 44.44%
Submissions: 81
Points: 10
Write a Python program that finds the second largest number in a given list of integers. If all elements are equal or there are fewer than 2 elements, return -1.
Example 1
- Input: nums = [10, 5, 7, 15, 20]
- Output: 15
- Explanation:
- Step 1: Find the largest number in the list: 20.
- Step 2: Find the largest number that is not equal to the largest: 15.
- Step 3: Return 15 as the second largest number.
Example 2
- Input: nums = [3, 3, 3, 3]
- Output: -1
- Explanation:
- Step 1: Find the largest number in the list: 3.
- Step 2: Try to find a number less than 3, but all elements are equal.
- Step 3: Since all elements are equal, return -1.
Constraints
- 0 ≤ len(list) ≤ 10^6
- List elements are integers
- Time Complexity: O(n)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Sort the list and return the second last element if there are at least two unique numbers
- Track both maximum and second maximum while iterating through the list
- Handle edge cases like empty lists, lists with one element, or lists with all identical elements