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 array arr of integers, check if there exist two indices i and j such that :
i != j0 <= i, j < arr.lengtharr[i] == 2 * arr[j]Example 1:
Input: arr = [10,2,5,3] Output: true Explanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]
Example 2:
Input: arr = [3,1,7,11] Output: false Explanation: There is no i and j that satisfy the conditions.
Constraints:
2 <= arr.length <= 500-103 <= arr[i] <= 103Problem summary: Given an array arr of integers, check if there exist two indices i and j such that : i != j 0 <= i, j < arr.length arr[i] == 2 * arr[j]
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Two Pointers · Binary Search
[10,2,5,3]
[3,1,7,11]
keep-multiplying-found-values-by-two)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1346: Check If N and Its Double Exist
class Solution {
public boolean checkIfExist(int[] arr) {
Set<Integer> s = new HashSet<>();
for (int x : arr) {
if (s.contains(x * 2) || ((x % 2 == 0 && s.contains(x / 2)))) {
return true;
}
s.add(x);
}
return false;
}
}
// Accepted solution for LeetCode #1346: Check If N and Its Double Exist
func checkIfExist(arr []int) bool {
s := map[int]bool{}
for _, x := range arr {
if s[x*2] || (x%2 == 0 && s[x/2]) {
return true
}
s[x] = true
}
return false
}
# Accepted solution for LeetCode #1346: Check If N and Its Double Exist
class Solution:
def checkIfExist(self, arr: List[int]) -> bool:
s = set()
for x in arr:
if x * 2 in s or (x % 2 == 0 and x // 2 in s):
return True
s.add(x)
return False
// Accepted solution for LeetCode #1346: Check If N and Its Double Exist
struct Solution;
use std::collections::HashSet;
impl Solution {
fn check_if_exist(arr: Vec<i32>) -> bool {
let mut hs: HashSet<i32> = HashSet::new();
let mut zero = 0;
for &x in &arr {
if x == 0 {
if zero > 0 {
return true;
} else {
zero += 1;
}
} else {
hs.insert(x);
}
}
for x in arr {
if hs.contains(&(2 * x)) {
return true;
}
}
false
}
}
#[test]
fn test() {
let arr = vec![10, 2, 5, 3];
let res = true;
assert_eq!(Solution::check_if_exist(arr), res);
let arr = vec![7, 1, 14, 11];
let res = true;
assert_eq!(Solution::check_if_exist(arr), res);
let arr = vec![3, 1, 7, 11];
let res = false;
assert_eq!(Solution::check_if_exist(arr), res);
let arr = vec![-2, 0, 10, -19, 4, 6, -8];
let res = false;
assert_eq!(Solution::check_if_exist(arr), res);
}
// Accepted solution for LeetCode #1346: Check If N and Its Double Exist
function checkIfExist(arr: number[]): boolean {
const s: Set<number> = new Set();
for (const x of arr) {
if (s.has(x * 2) || (x % 2 === 0 && s.has((x / 2) | 0))) {
return true;
}
s.add(x);
}
return false;
}
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
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.