#C6471. Smallest Subarray with Divisible Element

    ID: 50235 Type: Default 1000ms 256MiB

Smallest Subarray with Divisible Element

Smallest Subarray with Divisible Element

Given a list of integers and an integer (d), find the size of the smallest contiguous subarray that contains at least one element divisible by (d). In this problem, the smallest subarray satisfying the condition naturally has a length of 1 if any element in the list is divisible by (d). If no such element exists, output (-1).

Example: For (d = 3) and the array [3, 5, 7, 9], since (3 % 3 = 0), the answer is 1.

inputFormat

The input is given from standard input (stdin) and consists of two lines:

  • The first line contains a single integer (d).
  • The second line contains a space-separated list of integers representing the array.

outputFormat

Output the size of the smallest contiguous subarray that contains at least one element divisible by (d). If no such element exists, output (-1). The output should be printed to standard output (stdout).## sample

3
3 5 7 9
1