Check if Array can be reordered such that adjacent difference of a pair is K times of the other
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 any one of the below conditions holds true then continue
- 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++
// C++ code to implement the above approach #include <bits/stdc++.h> using namespace std; // Function to check the conditions string checkArray( int * arr, int n, int k) { // Sort the array in increasing order sort(arr, arr + n); // Run a loop from index 1 to N -2 for ( int i = 1; i <= n - 2; i++) { // Store previous element in prev int prev = arr[i - 1]; // Store current element in curr int curr = arr[i]; // Store next element in next int next = arr[i + 1]; // If any conditions holds true // then continue if (((next - curr) == k * (curr - prev)) || ((curr - prev) == k * (next - curr))) { continue ; } // Else print No and return else { return "No" ; } } // We reach here only if the condition is valid // for all index except 0 and N-1 return "Yes" ; } // Driver Code int main() { int arr[] = { 16, 19, 18, 21, 24, 22 }; int N = sizeof (arr) / sizeof (arr[0]); int K = 2; // Function call cout << checkArray(arr, N, K); return 0; } |
Java
// Java code to implement the above approach import java.io.*; import java.util.*; class GFG { // Function to check the conditions public static String checkArray( int arr[], int n, int k) { // Sort the array in increasing order Arrays.sort(arr); // Run a loop from index 1 to N -2 for ( int i = 1 ; i <= n - 2 ; i++) { // Store previous element in prev int prev = arr[i - 1 ]; // Store current element in curr int curr = arr[i]; // Store next element in next int next = arr[i + 1 ]; // If any conditions holds true // then continue if (((next - curr) == k * (curr - prev)) || ((curr - prev) == k * (next - curr))) { continue ; } // Else print No and return else { return "No" ; } } // We reach here only if the condition is valid // for all index except 0 and N-1 return "Yes" ; } // Driver Code public static void main(String[] args) { int arr[] = { 16 , 19 , 18 , 21 , 24 , 22 }; int N = arr.length; int K = 2 ; // Function call System.out.print(checkArray(arr, N, K)); } } // This code is contributed by Rohit Pradhan |
Python3
# Python code to implement the approach # Function to check the conditions def checkArray(arr, n, k): # Sort the array in increasing order arr.sort(); # Run a loop from index 1 to N -2 for i in range ( 1 , n - 1 ): # Store previous element in prev prev = arr[i - 1 ]; # Store current element in curr curr = arr[i]; # Store next element in next next = arr[i + 1 ]; # If any conditions holds true # then continue if ((( next - curr) = = k * (curr - prev)) or ((curr - prev) = = k * ( next - curr))): continue # Else print No and return else : return "No" ; # We reach here only if the condition is valid # for all index except 0 and N-1 return "Yes" ; # Driver Code arr = [ 16 , 19 , 18 , 21 , 24 , 22 ]; N = len (arr) K = 2 ; # Function call print (checkArray(arr, N, K)); # This code is contributed by Saurabh Jaiswal |
C#
// C# code to implement the above approach using System; public class GFG { // Function to check the conditions public static string checkArray( int [] arr, int n, int k) { // Sort the array in increasing order Array.Sort(arr); // Run a loop from index 1 to N -2 for ( int i = 1; i <= n - 2; i++) { // Store previous element in prev int prev = arr[i - 1]; // Store current element in curr int curr = arr[i]; // Store next element in next int next = arr[i + 1]; // If any conditions holds true // then continue if (((next - curr) == k * (curr - prev)) || ((curr - prev) == k * (next - curr))) { continue ; } // Else print No and return else { return "No" ; } } // We reach here only if the condition is valid // for all index except 0 and N-1 return "Yes" ; } // Driver Code public static void Main( string [] args) { int [] arr = { 16, 19, 18, 21, 24, 22 }; int N = arr.Length; int K = 2; // Function call Console.WriteLine(checkArray(arr, N, K)); } } // this code is a contributed by phasing17 |
Javascript
<script> // JavaScript code to implement the approach // Function to check the conditions function checkArray(arr, n, k) { // Sort the array in increasing order arr.sort(); // Run a loop from index 1 to N -2 for (let i = 1; i <= n - 2; i++) { // Store previous element in prev let prev = arr[i - 1]; // Store current element in curr let curr = arr[i]; // Store next element in next let next = arr[i + 1]; // If any conditions holds true // then continue if (((next - curr) == k * (curr - prev)) || ((curr - prev) == k * (next - curr))) { continue ; } // Else print No and return else { return "No" ; } } // We reach here only if the condition is valid // for all index except 0 and N-1 return "Yes" ; } // Driver Code let arr = [ 16, 19, 18, 21, 24, 22 ]; let N = arr.length; let K = 2; // Function call document.write(checkArray(arr, N, K), "</br>" ); // This code is contributed by shinjanpatra </script> |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)
Contact Us