Check if it is possible to sort an array with conditional swapping of elements at distance K
Last Updated :
29 Jun, 2022
Given an array arr[] of n elements, we have to swap an index i with another index i + k any number of times and check whether it is possible to sort the given array arr[]. If it is then print “yes” otherwise print “no”.
Examples:
Input: K = 2, arr = [4, 3, 2, 6, 7]
Output: Yes
Explanation:
Choose index i = 0 and swap index i with i + k then the array becomes [2, 3, 4, 6, 7] which is sorted hence the output is “yes”.
Input : K = 2, arr = [4, 2, 3, 7, 6]
Output : No
Explanation:
It is not possible to obtain sorted array.
Approach:
To solve the problem mentioned above we have to take the elements starting from index 0 and add the multiples of K to it, that is 0, 0 + k, 0 + (2*k), and so on. Swap these positions for all the indexes from 0 to K-1 and check if the final array is sorted. If it is, then return “yes” otherwise “no”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool fun( int arr[], int n, int k)
{
vector< int > v;
for ( int i = 0; i < k; i++) {
for ( int j = i; j < n; j += k) {
v.push_back(arr[j]);
}
sort(v.begin(), v.end());
int x = 0;
for ( int j = i; j < n; j += k) {
arr[j] = v[x];
x++;
}
v.clear();
}
for ( int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1])
return false ;
}
return true ;
}
int main()
{
int arr[] = { 4, 2, 3, 7, 6 };
int K = 2;
int n = sizeof (arr) / sizeof (arr[0]);
if (fun(arr, n, K))
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}
|
Java
import java.lang.*;
import java.io.*;
import java.util.*;
class GFG{
public static boolean fun( int [] arr, int n,
int k)
{
Vector<Integer> v = new Vector<Integer>();
for ( int i = 0 ; i < k; i++)
{
for ( int j = i; j < n; j += k)
{
v.add(arr[j]);
}
Collections.sort(v);
int x = 0 ;
for ( int j = i; j < n; j += k)
{
arr[j] = v.get(x);
x++;
}
v.clear();
}
for ( int i = 0 ; i < n - 1 ; i++)
{
if (arr[i] > arr[i + 1 ])
{
return false ;
}
}
return true ;
}
public static void main (String args[])
{
int [] arr = { 4 , 2 , 3 , 7 , 6 };
int K = 2 ;
int n = arr.length;
if (fun(arr, n, K))
{
System.out.println( "yes" );
}
else
{
System.out.println( "no" );
}
}
}
|
Python3
def fun(arr, n, k):
v = []
for i in range (k):
for j in range (i, n, k):
v.append(arr[j]);
v.sort();
x = 0
for j in range (i, n, k):
arr[j] = v[x];
x + = 1
v = []
for i in range (n - 1 ):
if (arr[i] > arr[i + 1 ]):
return False
return True
arr = [ 4 , 2 , 3 , 7 , 6 ]
K = 2 ;
n = len (arr)
if (fun(arr, n, K)):
print ( "yes" )
else :
print ( "no" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static bool fun( int [] arr,
int n, int k)
{
List< int > v = new List< int >();
for ( int i = 0; i < k; i++)
{
for ( int j = i; j < n; j += k)
{
v.Add(arr[j]);
}
v.Sort();
int x = 0;
for ( int j = i; j < n; j += k)
{
arr[j] = v[x];
x++;
}
v.Clear();
}
for ( int i = 0; i < n - 1; i++)
{
if (arr[i] > arr[i + 1])
{
return false ;
}
}
return true ;
}
public static void Main(String []args)
{
int [] arr = {4, 2, 3, 7, 6};
int K = 2;
int n = arr.Length;
if (fun(arr, n, K))
{
Console.WriteLine( "yes" );
}
else
{
Console.WriteLine( "no" );
}
}
}
|
Javascript
<script>
function fun(arr, n, k)
{
let v = [];
for (let i = 0; i < k; i++) {
for (let j = i; j < n; j += k) {
v.push(arr[j]);
}
v.sort();
let x = 0;
for (let j = i; j < n; j += k) {
arr[j] = v[x];
x++;
}
v = [];
}
for (let i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1])
return false ;
}
return true ;
}
let arr = [ 4, 2, 3, 7, 6 ];
let K = 2;
let n = arr.length;
if (fun(arr, n, K))
document.write( "yes" );
else
document.write( "no" );
</script>
|
Time Complexity: O(k*n*log(n))
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...