Find the third maximum number in array.

Find the third maximum number in array.

Given a non-empty array of integers, return the third maximum number in this array.If it does not exist, return the maximum number, and algorithm must work in O(n).

Solution:

  • Create three buckets for variable to hold first, second and third max values.
  • When updating first bucket with new value then update second bucket with old first bucket value.
  • When updating second bucket with new value then, update third bucket with old second bucket value

Latest Source Code:
Github: ThirdMaximumNumberInArray.java


Output:

Array : [3, 2, 1]
Third Smallest: 1
Array : [-2147483648, -2147483648, -2147483648, -2147483648, 1, 1, 1]
Third Smallest: 1
Array : [2, 2, 3, 1]
Third Smallest: 1
Author: Hrishikesh Mishra