Minimize swaps required to make all prime-indexed elements as prime
Last Updated :
08 Oct, 2021
Given an array arr[] of size N. The task is to find the minimum number of swaps required to re-arrange the array such that all prime-indexed elements are prime, If the task can’t be achieved, print “-1“
Examples:
Input: N = 5, arr[] = {1, 2, 3, 4, 5}
Output: 0
Explanation: All the prime indices {2, 3, 5} (one-based indexing) have prime elements present on them. Therefore, minimum swaps required is 0.
Input: N = 5, arr[] = {2, 7, 8, 5, 13}
Output: 1
Explanation: swap 8 and 5 once, to get all the prime numbers at prime indices.
Approach: The task can be solved using the Sieve of Eratosthenes.
- Iterate over the array and check whether the current index is prime or not, if it is a prime, check the element present on this index.
- On observation, it can be seen that It is possible to achieve the required configuration, only if the total number of primes in the array is greater than or equal to the total number of prime indices in the array.
- If this condition holds, then the minimum number of swaps is equal to the number of prime indices that do not have a prime number on them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e4 + 1;
bool prime[mxn + 1];
void SieveOfEratosthenes()
{
memset (prime, true , sizeof (prime));
for ( int p = 2; p * p <= mxn; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i <= mxn; i += p)
prime[i] = false ;
}
}
}
int countMin( int arr[], int n)
{
int cMinSwaps = 0;
int cPrimeIndices = 0;
int cPrimeNos = 0;
for ( int i = 0; i < n; i++) {
if (prime[i + 1]) {
cPrimeIndices++;
if (!prime[arr[i]])
cMinSwaps++;
else
cPrimeNos++;
}
else if (prime[arr[i]]) {
cPrimeNos++;
}
}
if (cPrimeNos >= cPrimeIndices)
return cMinSwaps;
else
return -1;
}
int main()
{
SieveOfEratosthenes();
int n = 5;
int arr[5] = { 2, 7, 8, 5, 13 };
cout << countMin(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean prime[] = new boolean [( int )1e4 + 2 ];
static void SieveOfEratosthenes()
{
for ( int i = 0 ; i < ( int )1e4 + 2 ; i++)
prime[i] = true ;
for ( int p = 2 ; p * p < ( int )1e4 + 2 ; p++)
{
if (prime[p] == true )
{
for ( int i = p * p; i < ( int )1e4 + 2 ;
i += p)
prime[i] = false ;
}
}
}
static int countMin( int [] arr, int n)
{
int cMinSwaps = 0 ;
int cPrimeIndices = 0 ;
int cPrimeNos = 0 ;
for ( int i = 0 ; i < n; i++) {
if (prime[i + 1 ]) {
cPrimeIndices++;
if (prime[arr[i]] == false )
cMinSwaps++;
else
cPrimeNos++;
}
else if (prime[arr[i]]) {
cPrimeNos++;
}
}
if (cPrimeNos >= cPrimeIndices)
return cMinSwaps;
else
return - 1 ;
}
public static void main(String[] args)
{
SieveOfEratosthenes();
int n = 5 ;
int arr[] = { 2 , 7 , 8 , 5 , 13 };
System.out.println(countMin(arr, n));
}
}
|
Python3
import math
mxn = 10000 + 1
prime = [ True for _ in range (mxn + 1 )]
def SieveOfEratosthenes():
global prime
for p in range ( 2 , int (math.sqrt(mxn)) + 1 ):
if (prime[p] = = True ):
for i in range (p * p, mxn + 1 , p):
prime[i] = False
def countMin(arr, n):
cMinSwaps = 0
cPrimeIndices = 0
cPrimeNos = 0
for i in range ( 0 , n):
if (prime[i + 1 ]):
cPrimeIndices + = 1
if ( not prime[arr[i]]):
cMinSwaps + = 1
else :
cPrimeNos + = 1
elif (prime[arr[i]]):
cPrimeNos + = 1
if (cPrimeNos > = cPrimeIndices):
return cMinSwaps
else :
return - 1
if __name__ = = "__main__" :
SieveOfEratosthenes()
n = 5
arr = [ 2 , 7 , 8 , 5 , 13 ]
print (countMin(arr, n))
|
C#
using System;
class GFG
{
static bool [] prime = new bool [( int )1e4 + 2];
static void SieveOfEratosthenes()
{
for ( int i = 0; i < ( int )1e4 + 2; i++)
prime[i] = true ;
for ( int p = 2; p * p < ( int )1e4 + 2; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i < ( int )1e4 + 2;
i += p)
prime[i] = false ;
}
}
}
static int countMin( int [] arr, int n)
{
int cMinSwaps = 0;
int cPrimeIndices = 0;
int cPrimeNos = 0;
for ( int i = 0; i < n; i++) {
if (prime[i + 1]) {
cPrimeIndices++;
if (prime[arr[i]] == false )
cMinSwaps++;
else
cPrimeNos++;
}
else if (prime[arr[i]]) {
cPrimeNos++;
}
}
if (cPrimeNos >= cPrimeIndices)
return cMinSwaps;
else
return -1;
}
public static void Main()
{
SieveOfEratosthenes();
int n = 5;
int [] arr = { 2, 7, 8, 5, 13 };
Console.Write(countMin(arr, n));
}
}
|
Javascript
<script>
const mxn = 1e4 + 1;
let prime = new Array(mxn + 1);
function SieveOfEratosthenes() {
prime.fill( true );
for (let p = 2; p * p <= mxn; p++)
{
if (prime[p] == true )
{
for (let i = p * p; i <= mxn; i += p) prime[i] = false ;
}
}
}
function countMin(arr, n)
{
let cMinSwaps = 0;
let cPrimeIndices = 0;
let cPrimeNos = 0;
for (let i = 0; i < n; i++) {
if (prime[i + 1]) {
cPrimeIndices++;
if (!prime[arr[i]]) cMinSwaps++;
else cPrimeNos++;
} else if (prime[arr[i]]) {
cPrimeNos++;
}
}
if (cPrimeNos >= cPrimeIndices) return cMinSwaps;
else return -1;
}
SieveOfEratosthenes();
let n = 5;
let arr = [2, 7, 8, 5, 13];
document.write(countMin(arr, n));
</script>
|
Time Complexity: O(mxn(log(log(mxn)))
Auxiliary Space: O(mxn)
Share your thoughts in the comments
Please Login to comment...