#2453

Destroy Sequential Targets

specialist · 990 · lc medium +32 · verified · 41.8% accepted · 610 likes · top 22%

Description

You are given a 0-indexed array nums of positive integers and an integer space.

A machine seeded with nums[i] can destroy all values congruent to nums[i] modulo space. Targets with equal nums[i] mod space values are destroyed in a single seed operation.

You want to destroy as many targets as possible with one seed. Return the seed value from nums that destroys the most targets, using the smallest such value if there is a tie.

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].

Code

1
2
3