Given an array arr[] of positive integers and a positive number K, the task is to find the minimum swaps of elements required such that for every element at index i, the following condition holds true:
arr[i] % K = i % K
Example:
Input: arr = {4, 3, 5, 2, 9, 7}, K=3
Output: 3
Explanation: Index % 3 = 0 1 2 0 1 2 and arr[i] % 3 = 1 0 2 2 0 1
swap index 0 with index 1 => 0 1 2 2 0 1
swap index 3 with index 4 => 0 1 2 0 2 1
swap index 4 with index 5 => 1 0 2 0 1 2Input: arr = {0, 1, 2, 3, 4, 5}, K=1
Output: 0
Approach: The given problem can be solved using a greedy approach. The idea is to traverse the array and make swaps such that two elements are brought to their accurate positions if possible, or else the correct element is brought at the current position. Below steps can be followed to solve the problem:
- Check if it is possible to complete the task by comparing frequencies of index % k with element % k
- If the frequencies do not match then return -1
-
Iterate the array and at every index i:
- If arr[i] % 3 == i % 3 then continue to the next index
- Else if arr[i] % 3 != i % 3 then find the index j from i+1 to N-1, where i % 3 = arr[j] % 3 and j % 3 = arr[i] % 3 that will bring two elements to their accurate positions
- Else find the index k, by iterating from i+1 to N-1, where i % 3 = arr[k] % 3, and swap the elements
Below is the implementation of the above approach:
// C++ implementation for the above approach #include <iostream> using namespace std;
// Function to swap the values void swapping( int arr[], int i, int j)
{ int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
} // Function to find the minimum swaps // required such that arr[i] % k = i % k int CountMinSwaps( int arr[], int N, int K)
{ // initialize matrix with 0 values
int mat[K][2] = { 0 };
int i, j, count = 0;
for (i = 0; i < N; i++) {
// Count the frequency of
// index % k
mat[i % K][0] += 1;
// Count the frequency of
// index % k
mat[arr[i] % K][1] += 1;
}
// If the count of indexes % k and
// array elements % K are not same
// then the task is not possible
// therefore return -1
for (i = 0; i < K; i++) {
if (mat[i][0] != mat[i][1])
return -1;
}
// Count the swaps
for (i = 0; i < N; i++) {
// If condition is already true
// move to the next index
if (i % K == arr[i] % K)
continue ;
// Current index remainder
int ind = i % K;
// Current element remainder
int ele = arr[i] % K;
// Boolean variable to indicate
// if the swap was made with the
// element such that both the swapped
// elements would be at correct place
bool swapped = false ;
// Search for the element from
// i + 1 till end of the array
for (j = i + 1; j < N; j++) {
// Expected index remainder
int ind_exp = j % K;
// Expected element remainder
int ele_exp = arr[j] % K;
if (ind == ele_exp
&& ele == ind_exp) {
// Swap the element if found
swapping(arr, i, j);
// Update the boolean
// variable swap to true
swapped = true ;
// Increment count of swaps
count++;
break ;
}
}
// If the swap didnt take place
if (swapped == false ) {
// Iterate from i+1 till end and
// find the accurate element for
// the current index
for (j = i + 1; j < N; j++) {
// Expected element remainder
int ele_exp = arr[j] % K;
if (ind == ele_exp) {
// Swap after finding
// the element
swapping(arr, i, j);
// Increment the count
count++;
break ;
}
}
}
}
// Return the result
return count;
} // Driver code int main()
{ int arr[6] = { 0, 1, 2, 3, 4, 5 };
int K = 1;
int N = sizeof (arr) / sizeof (arr[0]);
// Call the function
int swaps = CountMinSwaps(arr, N, K);
// Print the answer
cout << swaps << endl;
} |
// Java implementation for the above approach public class GFG {
// Function to swap the values
static void swapping( int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Function to find the minimum swaps
// required such that arr[i] % k = i % k
static int CountMinSwaps( int arr[], int N, int K)
{
// initialize matrix with 0 values
int mat[][] = new int [K][ 2 ] ;
int i, j, count = 0 ;
for (i = 0 ; i < N; i++) {
// Count the frequency of
// index % k
mat[i % K][ 0 ] += 1 ;
// Count the frequency of
// index % k
mat[arr[i] % K][ 1 ] += 1 ;
}
// If the count of indexes % k and
// array elements % K are not same
// then the task is not possible
// therefore return -1
for (i = 0 ; i < K; i++) {
if (mat[i][ 0 ] != mat[i][ 1 ])
return - 1 ;
}
// Count the swaps
for (i = 0 ; i < N; i++) {
// If condition is already true
// move to the next index
if (i % K == arr[i] % K)
continue ;
// Current index remainder
int ind = i % K;
// Current element remainder
int ele = arr[i] % K;
// Boolean variable to indicate
// if the swap was made with the
// element such that both the swapped
// elements would be at correct place
boolean swapped = false ;
// Search for the element from
// i + 1 till end of the array
for (j = i + 1 ; j < N; j++) {
// Expected index remainder
int ind_exp = j % K;
// Expected element remainder
int ele_exp = arr[j] % K;
if (ind == ele_exp
&& ele == ind_exp) {
// Swap the element if found
swapping(arr, i, j);
// Update the boolean
// variable swap to true
swapped = true ;
// Increment count of swaps
count++;
break ;
}
}
// If the swap didnt take place
if (swapped == false ) {
// Iterate from i+1 till end and
// find the accurate element for
// the current index
for (j = i + 1 ; j < N; j++) {
// Expected element remainder
int ele_exp = arr[j] % K;
if (ind == ele_exp) {
// Swap after finding
// the element
swapping(arr, i, j);
// Increment the count
count++;
break ;
}
}
}
}
// Return the result
return count;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 0 , 1 , 2 , 3 , 4 , 5 };
int K = 1 ;
int N = arr.length;
// Call the function
int swaps = CountMinSwaps(arr, N, K);
// Print the answer
System.out.println(swaps);
}
} // This code is contributed by AnkThon |
# python implementation for the above approach # Function to swap the values def swapping(arr, i, j):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
# Function to find the minimum swaps # required such that arr[i] % k = i % k def CountMinSwaps(arr, N, K):
# initialize matrix with 0 values
mat = [[ 0 for _ in range ( 2 )] for _ in range (K)]
count = 0
for i in range ( 0 , N):
# Count the frequency of
# index % k
mat[i % K][ 0 ] + = 1
# Count the frequency of
# index % k
mat[arr[i] % K][ 1 ] + = 1
# If the count of indexes % k and
# array elements % K are not same
# then the task is not possible
# therefore return -1
for i in range ( 0 , K):
if (mat[i][ 0 ] ! = mat[i][ 1 ]):
return - 1
# Count the swaps
for i in range ( 0 , N):
# If condition is already true
# move to the next index
if (i % K = = arr[i] % K):
continue
# Current index remainder
ind = i % K
# Current element remainder
ele = arr[i] % K
# Boolean variable to indicate
# if the swap was made with the
# element such that both the swapped
# elements would be at correct place
swapped = False
# Search for the element from
# i + 1 till end of the array
for j in range (i + 1 , N):
# Expected index remainder
ind_exp = j % K
# Expected element remainder
ele_exp = arr[j] % K
if (ind = = ele_exp and ele = = ind_exp):
# Swap the element if found
swapping(arr, i, j)
# Update the boolean
# variable swap to true
swapped = True
# Increment count of swaps
count + = 1
break
# If the swap didnt take place
if (swapped = = False ):
# Iterate from i+1 till end and
# find the accurate element for
# the current index
for j in range (i + 1 , N):
# Expected element remainder
ele_exp = arr[j] % K
if (ind = = ele_exp):
# Swap after finding
# the element
swapping(arr, i, j)
# Increment the count
count + = 1
break
# Return the result
return count
# Driver code if __name__ = = "__main__" :
arr = [ 0 , 1 , 2 , 3 , 4 , 5 ]
K = 1
N = len (arr)
# Call the function
swaps = CountMinSwaps(arr, N, K)
# Print the answer
print (swaps)
# This code is contributed by rakeshsahni |
// C# implementation for the above approach using System;
public class GFG
{ // Function to swap the values
static void swapping( int [] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Function to find the minimum swaps
// required such that arr[i] % k = i % k
static int CountMinSwaps( int [] arr, int N, int K)
{
// initialize matrix with 0 values
int [,] mat = new int [K, 2];
int i, j, count = 0;
for (i = 0; i < N; i++)
{
// Count the frequency of
// index % k
mat[i % K, 0] += 1;
// Count the frequency of
// index % k
mat[arr[i] % K, 1] += 1;
}
// If the count of indexes % k and
// array elements % K are not same
// then the task is not possible
// therefore return -1
for (i = 0; i < K; i++)
{
if (mat[i, 0] != mat[i, 1])
return -1;
}
// Count the swaps
for (i = 0; i < N; i++)
{
// If condition is already true
// move to the next index
if (i % K == arr[i] % K)
continue ;
// Current index remainder
int ind = i % K;
// Current element remainder
int ele = arr[i] % K;
// Boolean variable to indicate
// if the swap was made with the
// element such that both the swapped
// elements would be at correct place
bool swapped = false ;
// Search for the element from
// i + 1 till end of the array
for (j = i + 1; j < N; j++)
{
// Expected index remainder
int ind_exp = j % K;
// Expected element remainder
int ele_exp = arr[j] % K;
if (ind == ele_exp
&& ele == ind_exp)
{
// Swap the element if found
swapping(arr, i, j);
// Update the boolean
// variable swap to true
swapped = true ;
// Increment count of swaps
count++;
break ;
}
}
// If the swap didnt take place
if (swapped == false )
{
// Iterate from i+1 till end and
// find the accurate element for
// the current index
for (j = i + 1; j < N; j++)
{
// Expected element remainder
int ele_exp = arr[j] % K;
if (ind == ele_exp)
{
// Swap after finding
// the element
swapping(arr, i, j);
// Increment the count
count++;
break ;
}
}
}
}
// Return the result
return count;
}
// Driver code
public static void Main()
{
int [] arr = { 0, 1, 2, 3, 4, 5 };
int K = 1;
int N = arr.Length;
// Call the function
int swaps = CountMinSwaps(arr, N, K);
// Print the answer
Console.Write(swaps);
}
} // This code is contributed by Saurabh Jaiswal |
<script> // JavaScript Program to implement
// the above approach
// Function to swap the values
function swapping(arr, i, j) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Function to find the minimum swaps
// required such that arr[i] % k = i % k
function CountMinSwaps(arr, N, K) {
// initialize matrix with 0 values
let mat = new Array(K);
for (let i = 0; i < mat.length; i++) {
mat[i] = new Array(2).fill(0);
}
let i, j, count = 0;
for (i = 0; i < N; i++) {
// Count the frequency of
// index % k
mat[i % K][0] += 1;
// Count the frequency of
// index % k
mat[arr[i] % K][1] += 1;
}
// If the count of indexes % k and
// array elements % K are not same
// then the task is not possible
// therefore return -1
for (i = 0; i < K; i++) {
if (mat[i][0] != mat[i][1])
return -1;
}
// Count the swaps
for (i = 0; i < N; i++) {
// If condition is already true
// move to the next index
if (i % K == arr[i] % K)
continue ;
// Current index remainder
let ind = i % K;
// Current element remainder
let ele = arr[i] % K;
// Boolean variable to indicate
// if the swap was made with the
// element such that both the swapped
// elements would be at correct place
let swapped = false ;
// Search for the element from
// i + 1 till end of the array
for (j = i + 1; j < N; j++) {
// Expected index remainder
let ind_exp = j % K;
// Expected element remainder
let ele_exp = arr[j] % K;
if (ind == ele_exp
&& ele == ind_exp) {
// Swap the element if found
swapping(arr, i, j);
// Update the boolean
// variable swap to true
swapped = true ;
// Increment count of swaps
count++;
break ;
}
}
// If the swap didnt take place
if (swapped == false ) {
// Iterate from i+1 till end and
// find the accurate element for
// the current index
for (j = i + 1; j < N; j++) {
// Expected element remainder
let ele_exp = arr[j] % K;
if (ind == ele_exp) {
// Swap after finding
// the element
swapping(arr, i, j);
// Increment the count
count++;
break ;
}
}
}
}
// Return the result
return count;
}
// Driver code
let arr = [0, 1, 2, 3, 4, 5];
let K = 1;
let N = arr.length;
// Call the function
let swaps = CountMinSwaps(arr, N, K);
// Print the answer
document.write(swaps + '<br>' );
// This code is contributed by Potta Lokesh
</script>
|
0
Time Complexity: O(N2)
Auxiliary Space: O(2 * K)