Menu
CoddyTech

Find the Largest Number

You get a non-empty list of integers nums. Return the largest value in it. The values can be negative, so the answer can be negative too. Find it with your own comparisons, without a built-in maximum function such as max.

Function

findMax(nums: integer-array) → integer
numsinteger-array
the list of integers to search
Returnsinteger
the largest value in nums

Constraints

  • 1 ≤ nums.length ≤ 5000
  • -109 ≤ nums[i] ≤ 109

Examples

Input
nums = [3, 17, 4, 12, 9]
Output
17
Explanation
Reading from the left, the largest value so far is 3, then 17. None of 4, 12 or 9 beats 17, so the answer is 17.

lock icon+13 hidden tests on Submit

challenge icon

Follow-up

Can you return both the largest and the smallest value with about 3n/2 comparisons instead of 2n, by comparing the values in pairs first?

Reset code
def findMax(nums):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

nums = [3, 17, 4, 12, 9]

Expected

17