#K15286. Subarray Sum Divisibility
Subarray Sum Divisibility
Subarray Sum Divisibility
You are given an array of N integers and an integer K. Your task is to determine whether there exists a contiguous subarray (of non-zero length) such that the sum of its elements is divisible by K.
In mathematical terms, you need to find if there exists indices \(l\) and \(r\) with \(l < r\) such that:
$$ \sum_{i=l}^{r} a_i \equiv 0 \pmod{K} $$
If such a subarray exists, print Yes
; otherwise, print No
.
Note: The subarray should have at least one element.
inputFormat
The input is given via standard input (stdin) and contains the following:
- The first line contains two integers N (the number of elements in the array) and K separated by a space.
- The second line contains N integers separated by spaces representing the array elements.
outputFormat
Output a single line to standard output (stdout):
Yes
if there exists a contiguous subarray whose sum is divisible by K.No
otherwise.
5 3
1 2 3 4 5
Yes