# 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 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++ program to sort array according to the ` `// count of ditinct prime factors ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// array to store the count of distict 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 seive ` ` ` `SieveOfEratosthenes(); ` ` ` ` ` `int` `arr[] = { 30, 2, 1024, 210, 3, 6 }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `sortArr(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2 1024 3 6 30 210

## Recommended Posts:

- Distinct Prime Factors of Array Product
- Sort only non-prime numbers of an array in increasing order
- Number of distinct prime factors of first n natural numbers
- Count numbers from range whose prime factors are only 2 and 3
- Number which has the maximum number of distinct prime factors in the range M to N
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count numbers in a range having GCD of powers of prime factors equal to 1
- 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 distinct elements in an array
- Sort prime numbers of an array in descending order
- Absolute distinct count in a sorted array
- Count of distinct substrings of a string using Suffix Array
- Count number of distinct pairs whose sum exists in the given array
- Count subarrays having total distinct elements same as original array

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.