Skip to main content
Back to problems
#2453
Medium Algorithms

Destroy sequential targets

Array Hash Table Counting
41.7% acceptance
Feb 25, 2026
610
34
You are given a 0-indexed array nums consisting of positive integers, represe nting 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 n ums[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. *

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn destroy_targets(nums: Vec<i32>, space: i32) -> i32 {
    use std::collections::HashMap;
    let space = space as i64;
    // group by nums[i] % space
    let mut groups: HashMap<i64, (usize, i32)> = HashMap::new(); // (count, min_seed)
    for &v in &nums {
      let key = v as i64 % space;
      let entry = groups.entry(key).or_insert((0, v));
      entry.0 += 1;
      entry.1 = entry.1.min(v);
    }
    let max_count = groups.values().map(|&(c, _)| c).max().unwrap();
    groups.values().filter(|&&(c, _)| c == max_count).map(|&(_, s)| s).min().unwrap()
  }
}