DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
easy
Array

Third Maximum Number

**Problem Statement:** Given an integer array `nums`, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number. **Hint / Expected Approach:** Three-variable tracking (handle duplicates) **Edge Cases to Consider:** - (1) Fewer than 3 distinct numbers - (2) Negative max values - (3) Array with all duplicates

Examples

Example 1
Input: [3, 2, 1]
Output: 1
Example 2
Input: [1, 2]
Output: 2
Example 3
Input: [2, 2, 3, 1]
Output: 1

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪Fewer than 3 distinct numbers
  • ▪Negative max values
  • ▪Array with all duplicates
Frequently Asked At
AmazonMicrosoftTCS