Skip to content
CodeKruPro

Find the largest and second largest values

Coding problemEasyAsked in: Capgemini, Infosys, Razorpay, Swiggy, Wipro

Given a non-empty list of non-negative integers, return two values: the largest value, and the second largest distinct value (the largest value that is smaller than the largest, not equal to it).

If every value in the list is the same, there is no second largest value; return -1 in its place.

Examples

Example 1
Input: nums = [12,35,1,10,34,1]
Output: [35,34]
Example 2
Input: nums = [10,10,5]
Output: [10,5]
The second 10 is not a different value.
Example 3
Input: nums = [7,7]
Output: [7,-1]

Constraints

  • The list holds between 1 and 100,000 integers
  • Each value is between 0 and 1,000,000,000

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

to answer and keep your progress. Reading is free for everyone.