Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Scan and increment a counter when `nums[i] > x`. O(n). (If sorted, binary search gives O(log n).)
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you match strictness to the spec and spot the sorted-data optimisation.
Where candidates slip
`>=` off-by-one, or ignoring sortedness.
How to stand out
Give the O(log n) binary-search count for sorted input.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Count with predicate
1Hint 1Explore hintHide hint
Bump a counter on which condition?
2Hint 2Explore hintHide hint
Strict > excludes elements equal to x.
3Hint 3Explore hintHide hint
If the array were sorted, could you avoid scanning all of it?
A · Core idea
Count elements strictly greater than x: a filtered count — bump a counter whenever nums[i] > x.
Why strict `>`. 'Strictly greater' excludes elements equal to x; using >= would wrongly include them. Match the comparison to the wording.
Why O(n) for unsorted. With no order you must examine every element. But if the array were sorted, binary-searching the first index greater than x gives the count as n − firstIndex in O(log n) — the key optimisation to name.
This is the same predicate-count template as count-occurrences and sum-of-evens, with > x as the predicate.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 5, 2, 8], x = 31
no
c=02C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
If the array were sorted, binary-searching the first element > x makes this O(log n).
ComplexityO(n) time, O(1) space — one comparison per element for unsorted data; O(log n) via binary search if sorted.
E · Edge cases and pitfalls5nums = [1, 2], x = 5 — None → 0.Explore edge casesHide edge cases
Edge cases handled
nums = [1, 2], x = 5None → 0.nums = [9], x = 09 > 0 → 1.nums = [3, 3], x = 3Strict > excludes equals → 0.
Pitfalls
- Using `>=` and counting elements equal to x.
- Scanning a sorted array linearly when binary search would do.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int countGreater(int[] nums, int x) { int c = 0; for (int v : nums) { if (v > x) c++; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Using `>=` and counting elements equal to x.
- Scanning a sorted array linearly when binary search would do.
Revise
Carry the pattern into the interview
Pattern to remember
Predicate count
Code pattern to memorize
c=0; for v: if v>x: c++Rules to remember
- 1Bump on v > x.
- 2Strict > excludes equals.
- 3Binary search if sorted.
Use this pattern when
- Predicate counts
Choose another pattern when
- Sorted data — binary-search the boundary
Interview takeaway
Counting under a predicate is one guarded increment per element.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Predicate count. Master it on small inputs and it transfers to harder problems.