Tutorialarray
Find the Maximum
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given an integer array nums, return its largest value.
Examples
| nums | result |
|---|---|
[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
-
nums.length -
nums[i]
Expected complexity
- Time
- O(N)
- Space
- O(1)
Examples
| nums | Expected |
|---|---|
[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
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1200 ms | estimated |
| C# 14 (.NET 10) | 1200 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 100 | 12 | verdict only |