#K82922. Modify Array Based on Divisibility
Modify Array Based on Divisibility
Modify Array Based on Divisibility
You are given an array of N integers and an integer K. The task is to modify the array as follows:
- If an element is divisible by K, replace it with the maximum element in the array.
- If an element is not divisible by K, replace it with the minimum element in the array.
Formally, for an array \(A = [a_1, a_2, \dots, a_N]\), let \(M = \max(A)\) and \(m = \min(A)\). Then the resulting array \(B = [b_1, b_2, \dots, b_N]\) is defined as:
\( b_i = \begin{cases} M & \text{if } a_i \equiv 0 \pmod{K}, \\ m & \text{otherwise.} \end{cases} \)
Your job is to read the input from standard input and write the modified array to standard output.
inputFormat
The input is given via stdin in the following format:
N K a1 a2 ... aN
Where:
- N is the number of elements in the array.
- K is the divisor used for checking divisibility.
- ai are the integers in the array.
outputFormat
Output the modified array to stdout as a single line of space-separated integers.
## sample5 3
1 3 4 9 10
1 10 1 10 1
</p>