#K36972. Rotated Array Maximum-Minimum Difference
Rotated Array Maximum-Minimum Difference
Rotated Array Maximum-Minimum Difference
You are given an array of integers and an integer k. Your task is to rotate the array to the right by k positions and then compute the difference between the maximum and minimum element in the rotated array.
The rotation is defined as moving the last k elements of the array to the front while shifting the rest to the right. For example, given the array [3, 8, 9, 7, 6]
and k = 3, after rotation the array becomes [9, 7, 6, 3, 8]
. The answer in this case is 9 - 3 = 6
.
Note: If k is greater than the length of the array, perform k mod n rotations where n is the length of the array.
inputFormat
The input is read from standard input and has the following format:
- The first line contains two integers n and k, where n is the number of elements in the array and k is the number of positions to rotate the array.
- The second line contains n space-separated integers representing the array.
outputFormat
Print a single integer to standard output: the difference between the maximum and minimum element in the rotated array.
## sample5 3
3 8 9 7 6
6
</p>