Minimum swaps to minimize the sum of first K elements of the Permutation
Last Updated :
02 Dec, 2022
Given a permutation A[] of first N integers (i.e. array contains integers from 1 to N exactly once) and an integer K, the task is to find the minimum number of swaps needed to minimize the sum of the first K elements of the array.
Examples:
Input: N = 4, K = 2, A[] = {3, 4, 1, 2}
Output: 2
Explanation: The swaps performed are as follows:
{3, 4, 1, 2} -> {1, 4, 3, 2}, {1, 4, 3, 2} -> {1, 2, 3, 4}
The sum of first K(2) elements becomes 3,
which is minimum possible for the given permutation.
Input: N = 3, K = 1, A[] = {3, 2, 1}
Output: 1
Approach: The problem can be solved easily by a greedy approach.
The minimum possible sum of K elements of a permutation would be the sum of integers from 1 to K. i.e. A[1] + A[2] + . . . + A[K] can’t be less than 1 + 2 + …. + K. Therefore, apply the swap operation whenever A[i] > K (1 = i ? K).
Based on the above observation, the following approach can be followed to arrive at the answer:
- Declare a hash-set and initialize a variable (say count = 0) to store the total number of swap operations.
- Store first K elements of the given permutation in the set.
- Traverse through the permutation from i = 0 to K-1:
- At each iteration, check if A[i] is present in the set. If not, increment the count by 1.
- At the end of the iteration, return the count.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumSwaps( int A[], int N, int K)
{
set< int > S;
int count = 0;
for ( int i = 1; i <= K; i++) {
S.insert(i);
}
for ( int i = 0; i < K; i++) {
if (S.find(A[i]) == S.end()) {
count++;
}
}
return count;
}
int main()
{
int N = 4;
int K = 2;
int A[] = { 3, 4, 1, 2 };
int answer = minimumSwaps(A, N, K);
cout << answer;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int minimumSwaps( int A[], int N, int K)
{
Set<Integer> S = new HashSet<>();
int count = 0 ;
for ( int i = 1 ; i <= K; i++) {
S.add(i);
}
for ( int i = 0 ; i < K; i++) {
if (! S.contains(A[i])) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int N = 4 ;
int K = 2 ;
int A[] = { 3 , 4 , 1 , 2 };
int answer = minimumSwaps(A, N, K);
System.out.println( answer);
}
}
|
Python3
def minimumSwaps(A, N, K) :
S = set ()
count = 0
for i in range ( 1 , K + 1 ):
S.add(i)
for i in range (K):
if ((A[i]) not in S) :
count + = 1
return count
if __name__ = = "__main__" :
N = 4
K = 2
A = [ 3 , 4 , 1 , 2 ]
answer = minimumSwaps(A, N, K)
print (answer)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int minimumSwaps( int [] A, int N, int K)
{
HashSet< int > S = new HashSet< int >();
int count = 0;
for ( int i = 1; i <= K; i++) {
S.Add(i);
}
for ( int i = 0; i < K; i++) {
if (!S.Contains(A[i])) {
count++;
}
}
return count;
}
static public void Main()
{
int N = 4;
int K = 2;
int [] A = { 3, 4, 1, 2 };
int answer = minimumSwaps(A, N, K);
Console.WriteLine(answer);
}
}
|
Javascript
function minimumSwaps(A, N, K)
{
let S = new Set();
let count = 0;
for (let i = 1; i <= K; i++) {
S.add(i);
}
for (let i = 0; i < K; i++) {
if (!S.has(A[i])) {
count++;
}
}
return count;
}
let N = 4;
let K = 2;
let A = [3, 4, 1, 2];
let answer = minimumSwaps(A, N, K);
console.log(answer);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...