Sort an array according to the increasing count of distinct Prime Factors
Given an array of integers. The task is to sort the given array on the basis of increasing count of distinct prime factors.
Examples:
Input : arr[] = {30, 2, 1024, 210, 3, 6}
Output : 2 1024 3 6 30 210
Input : arr[] = {12, 16, 27, 6}
Output : 16 27 6 12
A naive approach is to find all the prime factors of each elements of the array and pair the count of the prime factors with the element in a vector and sort the array with respect to the count of prime factors.
An Efficient approach is to use a sieve to find the count of distinct prime factors and store them in a vector. Now, traverse through the array and pair the count of distinct prime factors with the element in a vector and sort the array with respect to the count of prime factors using a comparator function.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int prime[100001];
void SieveOfEratosthenes()
{
memset (prime, 0, sizeof (prime));
prime[0] = 0;
prime[1] = 0;
for ( int p = 2; p * p <= 100001; p++) {
if (prime[p] == 0) {
prime[p]++;
for ( int i = p * p; i <= 100001; i += p)
prime[i]++;
}
}
}
bool Compare(pair< int , int > p1, pair< int , int > p2)
{
return (p1.second < p2.second);
}
void sortArr( int arr[], int n)
{
vector<pair< int , int > > v;
for ( int i = 0; i < n; i++) {
v.push_back(make_pair(arr[i], prime[arr[i]]));
}
sort(v.begin(), v.end(), Compare);
for ( int i = 0; i < n; i++)
cout << v[i].first << " " ;
cout << endl;
}
int main()
{
SieveOfEratosthenes();
int arr[] = { 30, 2, 1024, 210, 3, 6 };
int n = sizeof (arr) / sizeof ( int );
sortArr(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static class Pair implements Comparable<Pair>
{
int first, second;
Pair( int f, int s)
{
first = f;
second = s;
}
@Override
public int compareTo(Pair o)
{
if ( this .second > o.second)
return 1 ;
else if ( this .second == o.second)
return 0 ;
return - 1 ;
}
}
static int prime[] = new int [ 100002 ];
static void SieveOfEratosthenes()
{
Arrays.fill(prime, 0 );
prime[ 0 ] = 0 ;
prime[ 1 ] = 0 ;
for ( int p = 2 ; p * p <= 100001 ; p++)
{
if (prime[p] == 0 )
{
prime[p]++;
for ( int i = p * p; i <= 100001 ; i += p)
prime[i]++;
}
}
}
static void sortArr( int arr[], int n)
{
Pair v[] = new Pair[n];
for ( int i = 0 ; i < n; i++)
{
v[i] = new Pair(arr[i], prime[arr[i]]);
}
Arrays.sort(v);
for ( int i = 0 ; i < n; i++)
System.out.print(v[i].first + " " );
System.out.println();
}
public static void main(String args[])
{
SieveOfEratosthenes();
int arr[] = { 30 , 2 , 1024 , 210 , 3 , 6 };
int n = arr.length;
sortArr(arr, n);
}
}
|
Python3
import functools as ft
prime = [ 0 for i in range ( 100001 )]
def SieveOfEratosthenes():
prime[ 0 ] = 0
prime[ 1 ] = 0
for p in range ( 2 , 100002 ):
if p * p > 100001 :
break
if (prime[p] = = 0 ):
prime[p] + = 1
for i in range ( 2 * p, 100001 , p):
prime[i] + = 1
def sortArr(arr, n):
v = []
for i in range (n):
v.append([arr[i], prime[arr[i]]])
v.sort(key = lambda x:x[ 1 ])
for i in range (n):
print (v[i][ 0 ], end = " " )
print ()
SieveOfEratosthenes()
arr = [ 30 , 2 , 1024 , 210 , 3 , 6 ]
n = len (arr)
sortArr(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class Pair : IComparable<Pair>
{
public int first, second;
public Pair( int f, int s)
{
first = f;
second = s;
}
public int CompareTo(Pair o)
{
if ( this .second > o.second)
return 1;
else if ( this .second == o.second)
return 0;
return -1;
}
}
static int [] prime = new int [100002];
static void SieveOfEratosthenes()
{
Array.Fill(prime, 0);
prime[0] = 0;
prime[1] = 0;
for ( int p = 2; p * p <= 100001; p++)
{
if (prime[p] == 0)
{
prime[p]++;
for ( int i = p * p; i <= 100001; i += p)
prime[i]++;
}
}
}
static void sortArr( int [] arr, int n)
{
Pair[] v = new Pair[n];
for ( int i = 0; i < n; i++)
{
v[i] = new Pair(arr[i],
prime[arr[i]]);
}
Array.Sort(v);
for ( int i = 0; i < n; i++)
Console.Write(v[i].first + " " );
Console.WriteLine();
}
public static void Main( string [] args)
{
SieveOfEratosthenes();
int [] arr = { 30, 2, 1024, 210, 3, 6 };
int n = arr.Length;
sortArr(arr, n);
}
}
|
Javascript
<script>
let prime = new Array(100002);
function SieveOfEratosthenes()
{
for (let i=0;i<prime.length;i++)
{
prime[i]=0;
}
prime[0] = 0;
prime[1] = 0;
for (let p = 2; p * p <= 100001; p++)
{
if (prime[p] == 0)
{
prime[p]++;
for (let i = p * p; i <= 100001; i += p)
prime[i]++;
}
}
}
function sortArr(arr,n)
{
let v=[];
for (let i = 0; i < n; i++)
{
v.push([arr[i], prime[arr[i]]]);
}
v.sort( function (a,b){ return a[1]-b[1];});
for (let i = 0; i < n; i++)
document.write(v[i][0] + " " );
document.write( "<br>" );
}
SieveOfEratosthenes();
let arr=[30, 2, 1024, 210, 3, 6];
let n = arr.length;
sortArr(arr, n);
</script>
|
Time complexity: O(nlog(log(n))).
Auxiliary Space complexity: O(n).
Last Updated :
06 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...