Wednesday, June 29, 2022
HomeSoftware DevelopmentTest if Array might be reordered such that adjoining distinction of a...

# Test if Array might be reordered such that adjoining distinction of a pair is Ok instances of the opposite

Given an array arr[] of size N and a positive integer K, the task is to check if the array can be reordered such that each pair of consecutive differences differ by a factor of K i.e.,

• arr[i] − arr[i − 1] = K*(arr[i + 1] − arr[i]), or
• arr[i + 1] − arr[i] = K*(arr[i] − arr[i − 1])

Note: Different conditions can hold at different indices, the only restriction is that at each index, at least one of the given conditions must hold.

Examples:

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

Approach: The problem can be solved based on the following idea:

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:

• Sort the array in increasing order.
• Run a loop from i = 1 to N-2
• If any one of the below conditions holds true then continue
•  (next – curr) == k*(curr – prev)
•  (curr – prev) == k*(next – curr)
• Otherwise, print No and return from the function.
• If the loop ends and the condition holds true for all the pairs of adjacent differences then return true.

Below is the implementation of the above approach:

## C++

 ` `  `#include ` `using` `namespace` `std;` ` `  `string checkArray(``int``* arr, ``int` `n, ``int` `k)` `{` `    ` `    ``sort(arr, arr + n);` ` `  `    ` `    ``for` `(``int` `i = 1; i <= n - 2; i++) {` ` `  `        ` `        ``int` `prev = arr[i - 1];` ` `  `        ` `        ``int` `curr = arr[i];` ` `  `        ` `        ``int` `next = arr[i + 1];` ` `  `        ` `        ` `        ``if` `(((next - curr) == k * (curr - prev))` `            ``|| ((curr - prev) == k * (next - curr))) {` `            ``continue``;` `        ``}` ` `  `        ` `        ``else` `{` `            ``return` `"No"``;` `        ``}` `    ``}` ` `  `    ` `    ` `    ``return` `"Yes"``;` `}` ` `  `int` `main()` `{` `    ``int` `arr[] = { 16, 19, 18, 21, 24, 22 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``int` `K = 2;` ` `  `    ` `    ``cout << checkArray(arr, N, K);` `    ``return` `0;` `}`

Time Complexity: O(N)
Auxiliary Space: O(1)

RELATED ARTICLES