Open in App
Not now

# 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++

```// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function to find minimum swaps needed
// to minimize the sum of first K elements
// of the array
int minimumSwaps(int A[], int N, int K)
{
// Declaring the Hash-set
set<int> S;

// Variable to store the count of
// swaps required
int count = 0;

// Inserting initial K elements of
// the permutation into the set
for (int i = 1; i <= K; i++) {
S.insert(i);
}

// Checking which among 1 to K is not
// present in the initial K elements
for (int i = 0; i < K; i++) {
if (S.find(A[i]) == S.end()) {
count++;
}
}

// Returning the count of swaps
// required
return count;
}

// Driver code
int main()
{
int N = 4;
int K = 2;
int A[] = { 3, 4, 1, 2 };

// Function Call
int answer = minimumSwaps(A, N, K);

return 0;
}```

## Java

```// Java code for the above approach
import java.io.*;
import java.util.*;

class GFG
{

// Function to find minimum swaps needed
// to minimize the sum of first K elements
// of the array
static int minimumSwaps(int A[], int N, int K)
{
// Declaring the Hash-set
Set<Integer> S = new HashSet<>();

// Variable to store the count of
// swaps required
int count = 0;

// Inserting initial K elements of
// the permutation into the set
for (int i = 1; i <= K; i++) {
}

// Checking which among 1 to K is not
// present in the initial K elements
for (int i = 0; i < K; i++) {
if (! S.contains(A[i])) {
count++;
}
}

// Returning the count of swaps
// required
return count;
}

// Driver code
public static void main(String[] args)
{
int N = 4;
int K = 2;
int A[] = { 3, 4, 1, 2 };

// Function Call
int answer = minimumSwaps(A, N, K);
}
}

// This code is contributed by sanjoy_62.```

## Python3

```# Function to check if it is possible to

# Function to find minimum swaps needed
# to minimize the sum of first K elements
# of the array
def minimumSwaps(A, N, K) :

# Declaring the Hash-set
S = set()

# Variable to store the count of
# swaps required
count = 0

# Inserting initial K elements of
# the permutation into the set
for i in range(1, K+1):

# Checking which among 1 to K is not
# present in the initial K elements
for i in range(K):
if ((A[i]) not in S) :
count += 1

# Returning the count of swaps
# required
return count

# Driver code
if __name__ == "__main__":

N = 4
K = 2
A = [ 3, 4, 1, 2 ]

# Function Call

# This code is contributed by code_hunt.```

## C#

```// C# code for the above approach
using System;
using System.Collections.Generic;

public class GFG {

// Function to find minimum swaps needed
// to minimize the sum of first K elements
// of the array
static int minimumSwaps(int[] A, int N, int K)
{
// Declaring the Hash-set
HashSet<int> S = new HashSet<int>();

// Variable to store the count of
// swaps required
int count = 0;

// Inserting initial K elements of
// the permutation into the set
for (int i = 1; i <= K; i++) {
}

// Checking which among 1 to K is not
// present in the initial K elements
for (int i = 0; i < K; i++) {
if (!S.Contains(A[i])) {
count++;
}
}

// Returning the count of swaps
// required
return count;
}

static public void Main()
{

// Code
int N = 4;
int K = 2;
int[] A = { 3, 4, 1, 2 };

// Function Call
int answer = minimumSwaps(A, N, K);
}
}

// This code is contributed by lokeshmvs21.```

## Javascript

```// Javascript code to implement the approach
function minimumSwaps(A, N, K)
{

// Declaring the Hash-set
let S = new Set();

// Variable to store the count of
// swaps required
let count = 0;

// Inserting initial K elements of
// the permutation into the set
for (let i = 1; i <= K; i++) {
}

// Checking which among 1 to K is not
// present in the initial K elements
for (let i = 0; i < K; i++) {
if (!S.has(A[i])) {
count++;
}
}

// Returning the count of swaps
// required
return count;
}

let N = 4;
let K = 2;
let A = [3, 4, 1, 2];

// Function Call
let answer = minimumSwaps(A, N, K);
`2`