Max Element in Array
Linear Scan
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven an array of N integers, find and return the maximum element in the array.ReadHide
Problem Statement
Given an array of N integers, find and return the maximum element in the array.
Input / Output
Input:
Line 1: Integer N — size of array
Line 2: N space-separated integers
Output:
Single integer — the maximum element
Example:
Input: 5
3 1 9 2 7
Output: 9
Constraints
1 ≤ N ≤ 10^5
-10^9 ≤ arr[i] ≤ 10^9
Understanding the Problem
- We need to scan every element once and track the largest seen so far
- Values can be negative — so never initialize max to 0, use arr[0]
- N up to 10^5 means we need at most O(n) — no nested loops
Edge Cases
- Single element array → return that element
- All negative numbers → max is the least negative (e.g. [-5, -3, -1] → -1)
- All same values → return that value
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Linear Scan — Walk through the array once, tracking the best value seen. This is the most fundamental array pattern. You will use it in 100+ problems.
Approach
Brute Force — O(n²)
For each element, compare it with every other element to check if it's the max. Works but unnecessarily slow.
Optimized — O(n)
Initialize max_val = arr[0]
For each element from index 1 onward:
If element > max_val:
Update max_val = element
Return max_val
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Brute Force | O(n²) | O(1) | Nested loops |
| Optimized | O(n) | O(1) | Single pass, one variable |
Video Resources
| Topic | Link |
|---|---|
| Arrays Intro — Striver | https://www.youtube.com/watch?v=37E9ckMDdTk |
| Arrays — NeetCode | https://www.youtube.com/watch?v=P5W3pocEz6I |
Online Compiler (for Hive problems)
Solution code
import sys
input = sys.stdin.readline
# ─────────────────────────────────────────────────────────────────────────
# BRUTE FORCE — O(n²) time | O(1) space
# For each element, check if any other element is bigger
# ─────────────────────────────────────────────────────────────────────────
def find_max_brute(arr):
n = len(arr)
for i in range(n):
is_max = True
# compare arr[i] with every element
for j in range(n):
if arr[j] > arr[i]:
is_max = False # found something bigger
break
if is_max:
return arr[i] # nothing bigger → this is the max
return arr[0]
# ─────────────────────────────────────────────────────────────────────────
# OPTIMIZED — O(n) time | O(1) space
# Single pass, track maximum as we go
# ─────────────────────────────────────────────────────────────────────────
def find_max(arr):
# IMPORTANT: init to arr[0], never 0
max_val = arr[0]
# arr could be all negatives!
for num in arr[1:]: # check from second element onward
if num > max_val: # found a new maximum
max_val = num # update our best
return max_val
# ─────────────────────────────────────────────────────────────────────────
# PYTHONIC — O(n) time | O(1) space
# Built-in max() does the same linear scan internally
# ─────────────────────────────────────────────────────────────────────────
def find_max_builtin(arr):
return max(arr) # clean one-liner, same complexity
# ── Input ─────────────────────────────────────────────────────────────────
n = int(input())
arr = list(map(int, input().split())) # read all elements in one line
print(find_max(arr))Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language