Destroy Sequential Targets - Problem

Imagine you have a powerful targeting system that can destroy targets on a number line. You're given an array nums of positive integers representing target positions, and an integer space that defines the spacing pattern.

Here's the key: when you seed your machine with any target value nums[i], it creates a chain reaction that destroys all targets at positions nums[i] + c ร— space, where c can be any non-negative integer (0, 1, 2, 3, ...).

Your mission: Choose the optimal seed value that destroys the maximum number of targets. If multiple seeds can destroy the same maximum number of targets, return the smallest seed value.

Example: With targets [3, 7, 8, 1, 1, 5] and space = 2, seeding with 1 destroys targets at positions 1, 3, 5, 7, ... which captures 4 targets from our array!

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [3, 7, 8, 1, 1, 5], space = 2
โ€บ Output: 1
๐Ÿ’ก Note: Seeding with 1 destroys targets at positions 1, 3, 5, 7, ... which includes 5 targets from our array: {1, 1, 3, 5, 7}. This is the maximum possible.
example_2.py โ€” Tie Breaker
$ Input: nums = [6, 2, 5], space = 100
โ€บ Output: 2
๐Ÿ’ก Note: Each target forms its own group since space=100 is larger than the differences. All seeds destroy exactly 1 target, so we return the smallest: 2.
example_3.py โ€” All Same Remainder
$ Input: nums = [1, 3, 5, 7], space = 2
โ€บ Output: 1
๐Ÿ’ก Note: All targets have remainder 1 when divided by 2, so any target can destroy all others. We return the minimum: 1.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • 1 โ‰ค space โ‰ค 109
  • All elements in nums are positive integers

Visualization

Tap to expand
Target Destruction Visualization0113578Chain Reaction: Space = 2Remainder GroupsGroup 1 (odd): {1, 1, 3, 5, 7}Count: 5, Min: 1Group 0 (even): {8}Count: 1, Min: 8Answer: Seed = 1 (Max targets = 5)
Understanding the Visualization
1
Position targets
Place all targets on a number line: [1, 1, 3, 5, 7, 8]
2
Group by pattern
Group by remainder mod 2: odd positions {1,1,3,5,7} vs even {8}
3
Chain reaction
Seeding at position 1 destroys all odd positions in the sequence
Key Takeaway
๐ŸŽฏ Key Insight: Targets with the same remainder (mod space) form destruction chains. Find the largest chain and return its minimum seed value.
Asked in
Amazon 35 Google 28 Meta 22 Microsoft 18
23.5K Views
Medium-High Frequency
~15 min Avg. Time
847 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