#C635. Array Equalization with Limited Operation

    ID: 50100 Type: Default 1000ms 256MiB

Array Equalization with Limited Operation

Array Equalization with Limited Operation

Given an array of N integers and a positive integer K, you are allowed to perform at most one operation. In a single operation, you may choose any contiguous subarray of length K and add an arbitrary integer (which can be positive, negative, or zero) to each element of that subarray. The task is to determine whether it is possible to make all elements of the array equal using at most one such operation.

The key observation is: if K = 1, you can only change one element, so the array must already have all identical elements. For K > 1, the allowed operation only lets you adjust the elements by multiples of the greatest common divisor (gcd) of K and N. That is, if we let ( L = \gcd(K, N) ), then there must exist an integer target ( X ) (with ( \min(arr) \leq X \leq \max(arr) )) such that both ( X - \min(arr) ) and ( \max(arr) - X ) are multiples of ( L ).

Output ( \texttt{YES} ) if such a target exists (i.e. the array can be equalized) and ( \texttt{NO} ) otherwise.

inputFormat

The input is provided via standard input (stdin) in the following format:

  • The first line contains two integers N and K separated by a space, where N is the number of array elements and K is the length of the subarray you can choose for the operation.
  • The second line contains N space-separated integers representing the elements of the array.

outputFormat

Output a single line to standard output (stdout) containing ( \texttt{YES} ) if it is possible to equalize the array with at most one operation, or ( \texttt{NO} ) if it is not possible.## sample

5 3
1 2 3 2 1
YES