#C2570. Subset Sum Divisible by K
Subset Sum Divisible by K
Subset Sum Divisible by K
You are given an integer m representing the number of integers, a positive integer k (the divisor), and a list of m integers. Your task is to determine whether there exists a non-empty contiguous subsequence (subarray) whose sum is divisible by k.
Note: Although the problem statement refers to it as a subset, the intended solution checks for a contiguous segment of numbers. Use the prefix sum modulo technique to efficiently solve the problem.
Example:
For m = 5, k = 3 and the sequence [1, 2, 3, 4, 5], the subarray [1, 2] sums to 3, which is divisible by 3, so the answer is "YES".
inputFormat
The first line of input contains two integers m
and k
, where m
is the number of integers and k
is the divisor. The second line of input contains m
space-separated integers representing the sequence.
outputFormat
Output a single line containing YES
if there exists a non-empty contiguous subsequence whose sum is divisible by k
, or NO
otherwise.
5 3
1 2 3 4 5
YES