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 array nums consisting of positive integers, representing targets on a number line. You are also given an integer space.
You have a machine which can destroy targets. Seeding the machine with some nums[i] allows it to destroy all targets with values that can be represented as nums[i] + c * space, where c is any non-negative integer. You want to destroy the maximum number of targets in nums.
Return the minimum value of nums[i] you can seed the machine with to destroy the maximum number of targets.
Example 1:
Input: nums = [3,7,8,1,1,5], space = 2 Output: 1 Explanation: If we seed the machine with nums[3], then we destroy all targets equal to 1,3,5,7,9,... In this case, we would destroy 5 total targets (all except for nums[2]). It is impossible to destroy more than 5 targets, so we return nums[3].
Example 2:
Input: nums = [1,3,5,2,4,6], space = 2 Output: 1 Explanation: Seeding the machine with nums[0], or nums[3] destroys 3 targets. It is not possible to destroy more than 3 targets. Since nums[0] is the minimal integer that can destroy 3 targets, we return 1.
Example 3:
Input: nums = [6,2,5], space = 100 Output: 2 Explanation: Whatever initial seed we select, we can only destroy 1 target. The minimal seed is nums[1].
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1091 <= space <= 109Problem summary: You are given a 0-indexed array nums consisting of positive integers, representing targets on a number line. You are also given an integer space. You have a machine which can destroy targets. Seeding the machine with some nums[i] allows it to destroy all targets with values that can be represented as nums[i] + c * space, where c is any non-negative integer. You want to destroy the maximum number of targets in nums. Return the minimum value of nums[i] you can seed the machine with to destroy the maximum number of targets.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[3,7,8,1,1,5] 2
[1,3,5,2,4,6] 2
[6,2,5] 100
arithmetic-slices-ii-subsequence)pairs-of-songs-with-total-durations-divisible-by-60)longest-arithmetic-subsequence)longest-arithmetic-subsequence-of-given-difference)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2453: Destroy Sequential Targets
class Solution {
public int destroyTargets(int[] nums, int space) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int v : nums) {
v %= space;
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
}
int ans = 0, mx = 0;
for (int v : nums) {
int t = cnt.get(v % space);
if (t > mx || (t == mx && v < ans)) {
ans = v;
mx = t;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2453: Destroy Sequential Targets
func destroyTargets(nums []int, space int) int {
cnt := map[int]int{}
for _, v := range nums {
cnt[v%space]++
}
ans, mx := 0, 0
for _, v := range nums {
t := cnt[v%space]
if t > mx || (t == mx && v < ans) {
ans = v
mx = t
}
}
return ans
}
# Accepted solution for LeetCode #2453: Destroy Sequential Targets
class Solution:
def destroyTargets(self, nums: List[int], space: int) -> int:
cnt = Counter(v % space for v in nums)
ans = mx = 0
for v in nums:
t = cnt[v % space]
if t > mx or (t == mx and v < ans):
ans = v
mx = t
return ans
// Accepted solution for LeetCode #2453: Destroy Sequential Targets
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2453: Destroy Sequential Targets
// class Solution {
// public int destroyTargets(int[] nums, int space) {
// Map<Integer, Integer> cnt = new HashMap<>();
// for (int v : nums) {
// v %= space;
// cnt.put(v, cnt.getOrDefault(v, 0) + 1);
// }
// int ans = 0, mx = 0;
// for (int v : nums) {
// int t = cnt.get(v % space);
// if (t > mx || (t == mx && v < ans)) {
// ans = v;
// mx = t;
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2453: Destroy Sequential Targets
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2453: Destroy Sequential Targets
// class Solution {
// public int destroyTargets(int[] nums, int space) {
// Map<Integer, Integer> cnt = new HashMap<>();
// for (int v : nums) {
// v %= space;
// cnt.put(v, cnt.getOrDefault(v, 0) + 1);
// }
// int ans = 0, mx = 0;
// for (int v : nums) {
// int t = cnt.get(v % space);
// if (t > mx || (t == mx && v < ans)) {
// ans = v;
// mx = t;
// }
// }
// return ans;
// }
// }
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.
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.