2453. Destroy Sequential Targets LeetCode Solution

In this guide, you will get 2453. Destroy Sequential Targets LeetCode Solution with the best time and space complexity. The solution to Destroy Sequential Targets problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Destroy Sequential Targets solution in C++
  4. Destroy Sequential Targets solution in Java
  5. Destroy Sequential Targets solution in Python
  6. Additional Resources
2453. Destroy Sequential Targets LeetCode Solution image

Problem Statement of Destroy Sequential Targets

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 <= 105
1 <= nums[i] <= 109
1 <= space <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2453. Destroy Sequential Targets LeetCode Solution in C++

class Solution {
 public:
  int destroyTargets(vector<int>& nums, int space) {
    int ans = INT_MAX;
    int maxCount = 0;
    unordered_map<int, int> count;

    for (const int num : nums)
      maxCount = max(maxCount, ++count[num % space]);

    for (const int num : nums)
      if (count[num % space] == maxCount)
        ans = min(ans, num);

    return ans;
  }
};
/* code provided by PROGIEZ */

2453. Destroy Sequential Targets LeetCode Solution in Java

class Solution {
  public int destroyTargets(int[] nums, int space) {
    int ans = Integer.MAX_VALUE;
    int maxCount = 0;
    Map<Integer, Integer> count = new HashMap<>();

    for (final int num : nums)
      maxCount = Math.max(maxCount, count.merge(num % space, 1, Integer::sum));

    for (final int num : nums)
      if (count.get(num % space) == maxCount)
        ans = Math.min(ans, num);

    return ans;
  }
}
// code provided by PROGIEZ

2453. Destroy Sequential Targets LeetCode Solution in Python

class Solution:
  def destroyTargets(self, nums: list[int], space: int) -> int:
    count = collections.Counter([num % space for num in nums])
    maxCount = max(count.values())
    return min(num for num in nums if count[num % space] == maxCount)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.