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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed integer array nums.
Initially, all of the indices are unmarked. You are allowed to make this operation any number of times:
i and j such that 2 * nums[i] <= nums[j], then mark i and j.Return the maximum possible number of marked indices in nums using the above operation any number of times.
Example 1:
Input: nums = [3,5,2,4] Output: 2 Explanation: In the first operation: pick i = 2 and j = 1, the operation is allowed because 2 * nums[2] <= nums[1]. Then mark index 2 and 1. It can be shown that there's no other valid operation so the answer is 2.
Example 2:
Input: nums = [9,2,5,4] Output: 4 Explanation: In the first operation: pick i = 3 and j = 0, the operation is allowed because 2 * nums[3] <= nums[0]. Then mark index 3 and 0. In the second operation: pick i = 1 and j = 2, the operation is allowed because 2 * nums[1] <= nums[2]. Then mark index 1 and 2. Since there is no other operation, the answer is 4.
Example 3:
Input: nums = [7,6,8] Output: 0 Explanation: There is no valid operation to do, so the answer is 0.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 109Problem summary: You are given a 0-indexed integer array nums. Initially, all of the indices are unmarked. You are allowed to make this operation any number of times: Pick two different unmarked indices i and j such that 2 * nums[i] <= nums[j], then mark i and j. Return the maximum possible number of marked indices in nums using the above operation any number of times.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search · Greedy
[3,5,2,4]
[9,2,5,4]
[7,6,8]
minimum-array-length-after-pair-removals)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2576: Find the Maximum Number of Marked Indices
class Solution {
public int maxNumOfMarkedIndices(int[] nums) {
Arrays.sort(nums);
int i = 0, n = nums.length;
for (int j = (n + 1) / 2; j < n; ++j) {
if (nums[i] * 2 <= nums[j]) {
++i;
}
}
return i * 2;
}
}
// Accepted solution for LeetCode #2576: Find the Maximum Number of Marked Indices
func maxNumOfMarkedIndices(nums []int) (ans int) {
sort.Ints(nums)
i, n := 0, len(nums)
for _, x := range nums[(n+1)/2:] {
if nums[i]*2 <= x {
i++
}
}
return i * 2
}
# Accepted solution for LeetCode #2576: Find the Maximum Number of Marked Indices
class Solution:
def maxNumOfMarkedIndices(self, nums: List[int]) -> int:
nums.sort()
i, n = 0, len(nums)
for x in nums[(n + 1) // 2 :]:
if nums[i] * 2 <= x:
i += 1
return i * 2
// Accepted solution for LeetCode #2576: Find the Maximum Number of Marked Indices
impl Solution {
pub fn max_num_of_marked_indices(mut nums: Vec<i32>) -> i32 {
nums.sort();
let mut i = 0;
let n = nums.len();
for j in (n + 1) / 2..n {
if nums[i] * 2 <= nums[j] {
i += 1;
}
}
(i * 2) as i32
}
}
// Accepted solution for LeetCode #2576: Find the Maximum Number of Marked Indices
function maxNumOfMarkedIndices(nums: number[]): number {
nums.sort((a, b) => a - b);
const n = nums.length;
let i = 0;
for (let j = (n + 1) >> 1; j < n; ++j) {
if (nums[i] * 2 <= nums[j]) {
++i;
}
}
return i * 2;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
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.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.