Funcia
Tutorialarray

Find the Maximum

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

Given an integer array nums, return its largest value.

Examples

numsresult
[3, 9, 2, 7]9
[-5, -2, -8]-2
[4]4

Example 1 — 9 is the largest.

Example 2 — All negative: -2 is the largest.

Example 3 — Only one element.

This is a tutorial problem: there are no performance tests, just return the correct value.

Function signature

solution(nums: int[]) → int

Constraints

  • 1≤1 \le nums.length ≤1,000\le 1{,}000
  • −109≤-10^9 \le nums[i] ≤109\le 10^9

Expected complexity

Time
O(N)
Space
O(1)

Examples

numsExpected
[3,9,2,7]9
[-5,-2,-8]-2
[4]4

Hint

Hint

Scan the array keeping the largest value so far in best. Starting best at 0 breaks when every element is negative; start from nums[0] instead.

Time limits per language

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251000 msderived
Kotlin 2.4 (JVM)1200 msestimated
C# 14 (.NET 10)1200 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness10012verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.