Given an array of integers. The task is to sort the given array on the basis increasing count of distinct prime factors.

**Examples:**

Input :arr[] = {30, 2, 1024, 210, 3, 6}Output :2 1024 3 6 30 210Input :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++

`// C++ program to sort array according to the ` `// count of distinct prime factors ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// array to store the count of distinct prime ` `int` `prime[100001]; ` ` ` `void` `SieveOfEratosthenes() ` `{ ` ` ` `// Create a int array "prime[0..n]" and initialize ` ` ` `// all entries it as 0. A value in prime[i] will ` ` ` `// count of distinct prime factors. ` ` ` ` ` `memset` `(prime, 0, ` `sizeof` `(prime)); ` ` ` ` ` `// 0 and 1 does not have any prime factors ` ` ` `prime[0] = 0; ` ` ` `prime[1] = 0; ` ` ` ` ` `for` `(` `int` `p = 2; p * p <= 100001; p++) { ` ` ` `// If prime[p] is not changed, ` ` ` `// then it is a prime ` ` ` `if` `(prime[p] == 0) { ` ` ` `prime[p]++; ` ` ` ` ` `// Update all multiples of p greater than or ` ` ` `// equal to the square of it ` ` ` `// numbers which are multiple of p and are ` ` ` `// less than p^2 are already been marked. ` ` ` `for` `(` `int` `i = p * p; i <= 100001; i += p) ` ` ` `prime[i]++; ` ` ` `} ` ` ` `} ` `} ` ` ` `// comparator function to sort the vector in ` `// ascending order of second element of the pair ` `bool` `Compare(pair<` `int` `, ` `int` `> p1, pair<` `int` `, ` `int` `> p2) ` `{ ` ` ` `return` `(p1.second < p2.second); ` `} ` ` ` `// Function to sort the array on the ` `// basis increasing count of distinct ` `// prime factors ` `void` `sortArr(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// vector to store the number and ` ` ` `// count of prime factor ` ` ` `vector<pair<` `int` `, ` `int` `> > v; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `// push_back the element and ` ` ` `// count of distinct ` ` ` `// prime factors ` ` ` `v.push_back(make_pair(arr[i], prime[arr[i]])); ` ` ` `} ` ` ` ` ` `// sort the array on the ` ` ` `// basis increasing count of ` ` ` `// distinct prime factors ` ` ` `sort(v.begin(), v.end(), Compare); ` ` ` ` ` `// display the sorted array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `cout << v[i].first << ` `" "` `; ` ` ` ` ` `cout << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// create the sieve ` ` ` `SieveOfEratosthenes(); ` ` ` ` ` `int` `arr[] = { 30, 2, 1024, 210, 3, 6 }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `sortArr(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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` `; ` ` ` `} ` ` ` `} ` ` ` `// array to store the count of distinct prime ` ` ` `static` `int` `prime[] = ` `new` `int` `[` `100002` `]; ` ` ` ` ` `static` `void` `SieveOfEratosthenes() ` ` ` `{ ` ` ` `// Create a int array "prime[0..n]" and initialize ` ` ` `// all entries it as 0. A value in prime[i] will ` ` ` `// count of distinct prime factors. ` ` ` `Arrays.fill(prime, ` `0` `); ` ` ` ` ` `// 0 and 1 does not have any prime factors ` ` ` `prime[` `0` `] = ` `0` `; ` ` ` `prime[` `1` `] = ` `0` `; ` ` ` ` ` `for` `(` `int` `p = ` `2` `; p * p <= ` `100001` `; p++) ` ` ` `{ ` ` ` `// If prime[p] is not changed, ` ` ` `// then it is a prime ` ` ` `if` `(prime[p] == ` `0` `) ` ` ` `{ ` ` ` `prime[p]++; ` ` ` ` ` `// Update all multiples of p greater than or ` ` ` `// equal to the square of it ` ` ` `// numbers which are multiple of p and are ` ` ` `// less than p^2 are already been marked. ` ` ` `for` `(` `int` `i = p * p; i <= ` `100001` `; i += p) ` ` ` `prime[i]++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Function to sort the array on the ` ` ` `// basis increasing count of distinct ` ` ` `// prime factors ` ` ` `static` `void` `sortArr(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `// Array to store the number and ` ` ` `// count of prime factor ` ` ` `Pair v[] = ` `new` `Pair[n]; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `// update the element and ` ` ` `// count of distinct ` ` ` `// prime factors ` ` ` `v[i] = ` `new` `Pair(arr[i], prime[arr[i]]); ` ` ` `} ` ` ` ` ` `// sort the array on the ` ` ` `// basis increasing count of ` ` ` `// distinct prime factors ` ` ` `Arrays.sort(v); ` ` ` ` ` `// display the sorted array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `System.out.print(v[i].first + ` `" "` `); ` ` ` ` ` `System.out.println(); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `// create the sieve ` ` ` `SieveOfEratosthenes(); ` ` ` ` ` `int` `arr[] = { ` `30` `, ` `2` `, ` `1024` `, ` `210` `, ` `3` `, ` `6` `}; ` ` ` ` ` `int` `n = arr.length; ` ` ` ` ` `sortArr(arr, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by ghanshyampandey ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to sort array according to the ` `# count of distinct prime factors ` `import` `functools as ft ` ` ` `# array to store the count of distinct prime ` `prime ` `=` `[` `0` `for` `i ` `in` `range` `(` `100001` `)] ` ` ` `def` `SieveOfEratosthenes(): ` ` ` ` ` `# Create a array "prime[0..n]" and initialize ` ` ` `# all entries it as 0. A value in prime[i] ` ` ` `# will count of distinct prime factors. ` ` ` ` ` `# memset(prime, 0, sizeof(prime)) ` ` ` ` ` `# 0 and 1 does not have any prime factors ` ` ` `prime[` `0` `] ` `=` `0` ` ` `prime[` `1` `] ` `=` `0` ` ` ` ` `for` `p ` `in` `range` `(` `2` `, ` `100002` `): ` ` ` ` ` `if` `p ` `*` `p > ` `100001` `: ` ` ` `break` ` ` ` ` `# If prime[p] is not changed, ` ` ` `# then it is a prime ` ` ` `if` `(prime[p] ` `=` `=` `0` `): ` ` ` `prime[p] ` `+` `=` `1` ` ` ` ` `# Update all multiples of p greater than ` ` ` `# or equal to the square of it ` ` ` `# numbers which are multiple of p and are ` ` ` `# less than p^2 are already been marked. ` ` ` `for` `i ` `in` `range` `(` `2` `*` `p, ` `100001` `, p): ` ` ` `prime[i] ` `+` `=` `1` ` ` `# Function to sort the array on the ` `# basis increasing count of distinct ` `# prime factors ` `def` `sortArr(arr, n): ` ` ` ` ` `# vector to store the number and ` ` ` `# count of prime factor ` ` ` `v ` `=` `[] ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# append the element and ` ` ` `# count of distinct ` ` ` `# prime factors ` ` ` `v.append([arr[i], prime[arr[i]]]) ` ` ` ` ` `# sort the array on the ` ` ` `# basis increasing count of ` ` ` `# distinct prime factors ` ` ` `v.sort(key` `=` `lambda` `x:x[` `1` `]) ` ` ` ` ` `# display the sorted array ` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `print` `(v[i][` `0` `], end ` `=` `" "` `) ` ` ` ` ` `print` `() ` ` ` `# Driver code ` ` ` `# create the sieve ` `SieveOfEratosthenes() ` ` ` `arr ` `=` `[` `30` `, ` `2` `, ` `1024` `, ` `210` `, ` `3` `, ` `6` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `sortArr(arr, n) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

2 1024 3 6 30 210

## Recommended Posts:

- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Maximum number of prime factors a number can have with exactly x factors
- Distinct Prime Factors of Array Product
- Number of distinct prime factors of first n natural numbers
- Number which has the maximum number of distinct prime factors in the range M to N
- Sort only non-prime numbers of an array in increasing order
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Count of prime factors of N to be added at each step to convert N to M
- Sort the array of strings according to alphabetical order defined by another string
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Find prime factors of Array elements whose sum of exponents is divisible by K
- Check whether a number has exactly three distinct factors or not
- Length of longest increasing prime subsequence from a given array
- Sort an Increasing-Decreasing Array
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Check whether count of distinct characters in a string is Prime or not
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Count of subsequences of length atmost K containing distinct prime elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.