Find Maximum

Easy~5 min

Given an array of integers nums, return the maximum value.

Constraint: Your solution must run in O(n) time. Do not sort the array (sorting is O(n log n)).

Examples

Example 1
Input: nums = [3, 1, 4, 1, 5, 9, 2, 6]
Output: 9
Explanation: 9 is the largest value in the array.
Example 2
Input: nums = [-5, -2, -8, -1]
Output: -1
Explanation: -1 is the largest (least negative) value.
Example 3
Input: nums = [7]
Output: 7
Explanation: A single element is the maximum.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Expected time complexity: O(n)
  • Do not use sort
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run