Input: arr[] = {16, 19, 18, 21, 24, 22}, K = 2
Output: Yes
Explanation: After Sorting, arr[] = {16, 18, 19, 21, 22, 24}
For index 1, arr[i] − arr[i − 1] = K * (arr[i + 1] − arr[i]) conditions holds.
Since, arr[i] − arr[i − 1] = 2, K * (arr[i + 1] − arr[i]) = 2*1 = 2.
Similarly, for index 3, above condition hold true.
For, index 2 and 4, arr[i+1]−arr[i] = K*(arr[i]−arr[i−1]) holds.
Input: arr[] = {5, 4, 7, 6}, K = 5
Output: No
As the value of K is positive, so the difference between any adjacent pair should be of only one type - either positive or negative. This type of arrangement is possible only if the array is sorted in ascending or descending order.
Therefore, check the difference between adjacent elements in sorted order to find if a arrangement exists or not.
To solve the problem based on the above idea, follow the steps mentioned below to implement the approach: