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 INPUT Number Line 1 2 3 4 6 nums array: 3 6 2 4 1 i=0 i=1 i=2 i=3 i=4 space = 2 Machine destroys targets: seed + c * space (c = 0, 1, 2, ...) ALGORITHM STEPS 1 Group by Remainder num % space = remainder 3 % 2 = 1 | 6 % 2 = 0 2 % 2 = 0 | 4 % 2 = 0 1 % 2 = 1 hash[0]: [6,2,4] count=3 hash[1]: [3,1] count=2 2 Find Max Count Group 0 has max: 3 targets 3 Find Min Seed In group 0: min(6,2,4) = 2 4 Verify Destruction seed=2: 2+0*2=2 [OK] seed=2: 2+1*2=4 [OK] seed=2: 2+2*2=6 [OK] FINAL RESULT Destroyed Targets: 2 4 6 1 3 (survived) Output: 2 Why seed = 2? - Destroys 3 targets (max) - Targets: 2, 4, 6 - 2 is minimum seed value among group with max count Key Insight: Numbers with the same remainder when divided by 'space' form a destroyable group. Use a hash map to group numbers by (num % space). Find the group with maximum count, then return the minimum value in that group as the optimal seed. Time: O(n), Space: O(n) TutorialsPoint - Destroy Sequential Targets | Hash Map Approach
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