Check if Array can be reordered such that adjacent difference of a pair is K times of the other
Last Updated :
31 May, 2022
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 <bits/stdc++.h>
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[0]);
int K = 2;
cout << checkArray(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static String checkArray( int arr[], int n, int k)
{
Arrays.sort(arr);
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" ;
}
public static void main(String[] args)
{
int arr[] = { 16 , 19 , 18 , 21 , 24 , 22 };
int N = arr.length;
int K = 2 ;
System.out.print(checkArray(arr, N, K));
}
}
|
Python3
def checkArray(arr, n, k):
arr.sort();
for i in range ( 1 , n - 1 ):
prev = arr[i - 1 ];
curr = arr[i];
next = arr[i + 1 ];
if ((( next - curr) = = k * (curr - prev))
or ((curr - prev) = = k * ( next - curr))):
continue
else :
return "No" ;
return "Yes" ;
arr = [ 16 , 19 , 18 , 21 , 24 , 22 ];
N = len (arr)
K = 2 ;
print (checkArray(arr, N, K));
|
C#
using System;
public class GFG
{
public static string checkArray( int [] arr, int n, int k)
{
Array.Sort(arr);
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" ;
}
public static void Main( string [] args)
{
int [] arr = { 16, 19, 18, 21, 24, 22 };
int N = arr.Length;
int K = 2;
Console.WriteLine(checkArray(arr, N, K));
}
}
|
Javascript
<script>
function checkArray(arr, n, k)
{
arr.sort();
for (let i = 1; i <= n - 2; i++) {
let prev = arr[i - 1];
let curr = arr[i];
let next = arr[i + 1];
if (((next - curr) == k * (curr - prev))
|| ((curr - prev) == k * (next - curr))) {
continue ;
}
else {
return "No" ;
}
}
return "Yes" ;
}
let arr = [ 16, 19, 18, 21, 24, 22 ];
let N = arr.length;
let K = 2;
document.write(checkArray(arr, N, K), "</br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...