Destroy Sequential Targets - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,6,2,4,1], space = 2
Output: 2
💡 Note: Seeding with 2 destroys targets [2,4,6] (3 targets). Seeding with 1 destroys [1,3] (2 targets). Choose 2 for maximum targets.
Example 2 — Tie Breaker
$ Input: nums = [1,3,5,2,4,6], space = 2
Output: 1
💡 Note: Both remainder groups have 3 targets: [1,3,5] and [2,4,6]. Choose seed=1 (minimum value) from the first group.
Example 3 — Single Group
$ Input: nums = [6,2,5], space = 100
Output: 2
💡 Note: Since space=100 is larger than differences, each target forms its own group. Choose minimum value 2.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i], space ≤ 109

Visualization

Tap to expand
Destroy Sequential Targets: Machine with Regular IntervalsTargets:36241space = 2Number Line:12346Seed = 2 destroys: 2 → 4 → 6 (space = 2)Best ChoiceSeed = 2, Destroys 3 targetsOutput: 2Machine seeded with 2 destroys maximum targets (2,4,6)
Understanding the Visualization
1
Input
Array of targets and space interval
2
Process
Group targets that can be destroyed by same seed
3
Output
Minimum seed value that destroys maximum targets
Key Takeaway
🎯 Key Insight: Targets that differ by multiples of 'space' can be destroyed by the same seed
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~15 min Avg. Time
456 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen