Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.
Example 1:
Input: arr = [1,2,2,6,6,6,6,7,10] Output: 6
Example 2:
Input: arr = [1,1] Output: 1
Constraints:
1 <= arr.length <= 1040 <= arr[i] <= 105Problem summary: Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,2,2,6,6,6,6,7,10]
[1,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1287: Element Appearing More Than 25% In Sorted Array
class Solution {
public int findSpecialInteger(int[] arr) {
for (int i = 0;; ++i) {
if (arr[i] == (arr[i + (arr.length >> 2)])) {
return arr[i];
}
}
}
}
// Accepted solution for LeetCode #1287: Element Appearing More Than 25% In Sorted Array
func findSpecialInteger(arr []int) int {
for i := 0; ; i++ {
if arr[i] == arr[i+len(arr)/4] {
return arr[i]
}
}
}
# Accepted solution for LeetCode #1287: Element Appearing More Than 25% In Sorted Array
class Solution:
def findSpecialInteger(self, arr: List[int]) -> int:
n = len(arr)
for i, x in enumerate(arr):
if x == arr[(i + (n >> 2))]:
return x
// Accepted solution for LeetCode #1287: Element Appearing More Than 25% In Sorted Array
struct Solution;
impl Solution {
fn find_special_integer(arr: Vec<i32>) -> i32 {
let n = arr.len();
let t = n / 4;
for i in 0..n - t {
if arr[i] == arr[i + t] {
return arr[i];
}
}
0
}
}
#[test]
fn test() {
let arr = vec![1, 2, 2, 6, 6, 6, 6, 7, 10];
assert_eq!(Solution::find_special_integer(arr), 6);
}
// Accepted solution for LeetCode #1287: Element Appearing More Than 25% In Sorted Array
function findSpecialInteger(arr: number[]): number {
const n = arr.length;
for (let i = 0; ; ++i) {
if (arr[i] === arr[i + (n >> 2)]) {
return arr[i];
}
}
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.