Given an array arr[] of n non-negative integers, find the largest element in…
20262025
Given an array arr[] of n non-negative integers, find the largest element in the array and print it.
Input Format
The first line contains a single integer
n— the number of elements in the array.The second line contains
nspace-separated integersarr[0] arr[1] ... arr[n-1].
Output Format
Print a single integer — the value of the largest element of arr[].
Examples
Input | Output | Explanation |
|---|---|---|
5 | 90 | The largest element of the given array is 90. |
4 | 5 | Every element is equal, so the largest element is 5. |
1 | 10 | There is only one element, which is therefore the largest. |
Constraints
1 ≤
n≤ 1060 ≤
arr[i]≤ 106
Show answer & explanation
Concept
The maximum of a finite, non-empty sequence can be accumulated in a single left-to-right pass, because the maximum operation is associative: max(a, b, c) = max(max(a, b), c). So a running "best so far" value carries all the information from the elements already seen, and no element ever has to be revisited.
The standard form of this idea is: initialise the running best from the first element, then walk the remaining elements once and replace the running best whenever a strictly larger element appears. After the pass, the running best is the maximum of the whole sequence.
Application
Read
n, then read thenarray values.Set
best = arr[0]. The constraint 1 ≤nguarantees the array is non-empty, so this first element always exists.For each remaining element
x, compare it withbest; ifx > best, assignbest = x. Otherwise leavebestunchanged.When the pass ends,
bestholds the largest element. Print it.
Tracing the first example, arr[] = [1, 8, 7, 56, 90]:
Element read | Comparison | best after step |
|---|---|---|
1 | initial value | 1 |
8 | 8 > 1 → replace | 8 |
7 | 7 > 8 is false → keep | 8 |
56 | 56 > 8 → replace | 56 |
90 | 90 > 56 → replace | 90 |
The pass ends with best = 90, which is the printed answer.
Cross-check and pitfalls
Duplicates: for
[5, 5, 5, 5]the strict testx > bestnever fires, sobeststays at 5 — correct, because equal elements cannot raise the maximum. Using>=instead would return the same value here; it changes only which position is remembered, not the maximum itself.Single element: for
[10]the loop body never runs, andbestkeeps its initial value 10. This is exactly why the running best is initialised fromarr[0]rather than from a placeholder.Initialising
best = 0happens to work only because this problem restrictsarr[i]to non-negative values. On any variant that allows negative numbers it silently returns 0 for an all-negative array. Initialising from the first element is the habit that generalises.Complexity: the scan touches every element exactly once, so the algorithm runs in
O(n)time while holding just one running value —O(1)auxiliary state beyond the elements it reads. Sorting the array and reading the last value also produces the answer, but costsO(n log n)time for no additional information.Input size and memory: with
nas large as 106, reading the numbers costs more than comparing them. Reading the whole stream at once and splitting it — what the reference solution does — is the simplest way to stay inside the time limit, but it stores every token, so total memory grows toO(n). A reader that consumes one token at a time keeps total memory atO(1), which matters when memory rather than speed is the binding constraint. Either way, avoid a slow per-token reader, or the program can exceed the time limit even though the algorithm is optimal.