Ctrl K

Best Launch Mark

Medium
Marks stand on a line at the positions given by the array of positive integers marks (values may repeat). A grasshopper starts on one of the marks and then only jumps forward, exactly stride each time. It visits v, v + stride, v + 2·stride, … and covers every mark at those positions. The starting point must be a value from marks. Pick the start that covers as many marks as possible (each array element counts separately). If several starts tie, return the smallest one.
Example 1
marks=[4, 10, 7, 1, 13, 2], stride=3 → 1

Starting at 1 gives hops 1, 4, 7, 10, 13, … — five of the six marks are hit, everything but 2.

Example 2
marks=[9, 5, 14, 6], stride=4 → 5

Starting at 5 hits 5 and 9, starting at 6 hits 6 and 14. That is two marks either way, so the smaller value, 5, wins.

Example 3
marks=[8, 3], stride=50 → 3
Constraints
  • 1 ≤ len(marks) ≤ 10⁵
  • 1 ≤ marks[i] ≤ 10⁹
  • 1 ≤ stride ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMore Than a Third of the Votes MediumMost Common Values MediumJobs With a Cooldown MediumMost Searched Words MediumDomain Visit Rollup Medium
loading Python…
Input
marks=[4, 10, 7, 1, 13, 2], stride=3
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 127 tests on the server, hidden ones included.