Given an array of positive integers. Sort the given array in decreasing order of number of factors of each element, i.e., element having the highest number of factors should be the first to be displayed and the number having least number of factors should be the last one. Two elements with equal number of factors should be in the same order as in the original array.

Examples:

Input : {5, 11, 10, 20, 9, 16, 23} Output : 20 16 10 9 5 11 23 Number of distinct factors: For20= 6,16= 5,10= 4,9= 3 and for5, 11, 23= 2 (same number of factors therefore sorted in increasing order of index) Input : {104, 210, 315, 166, 441, 180} Output : 180 210 315 441 104 166

We have already discussed a structure based solution to sort according to number of factors.

The following steps sort numbers in decreasing order of count of factors.

- Count distinct number of factors of each element. Refer this.
- Create a vector of pairs which stores elements and their factor counts.
- Sort this array on the basis of the problem statement using any sorting algorithm.

`// Sort an array of numbers according ` `// to number of factors. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that helps to sort elements ` `// in descending order ` `bool` `compare(` `const` `pair<` `int` `, ` `int` `> &a, ` ` ` `const` `pair<` `int` `, ` `int` `> &b) { ` ` ` `return` `(a.first > b.first); ` `} ` ` ` `// Prints array elements sorted in descending ` `// order of number of factors. ` `void` `printSorted(` `int` `arr[], ` `int` `n) { ` ` ` ` ` `vector<pair<` `int` `, ` `int` `>> v; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Count factors of arr[i]. ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `j = 1; j * j <= arr[i]; j++) { ` ` ` ` ` `// To check Given Number is Exactly ` ` ` `// divisible ` ` ` `if` `(arr[i] % j == 0) { ` ` ` `count++; ` ` ` ` ` `// To check Given number is perfect ` ` ` `// square ` ` ` `if` `(arr[i] / j != j) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Insert factor count and array element ` ` ` `v.push_back(make_pair(count, arr[i])); ` ` ` `} ` ` ` ` ` `// Sort the vector ` ` ` `sort(v.begin(), v.end(), compare); ` ` ` ` ` `// Print the vector ` ` ` `for` `(` `int` `i = 0; i < v.size(); i++) ` ` ` `cout << v[i].second << ` `" "` `; ` `} ` ` ` `// Driver's Function ` `int` `main() { ` ` ` `int` `arr[] = {5, 11, 10, 20, 9, 16, 23}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `printSorted(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

20 16 10 9 5 11 23

Time Complexity: O(n*sqrt(n))

Auxiliary Complexity: O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sort elements on the basis of number of factors
- Sort the Array of Strings on the basis of given substring range
- Maximum number of prime factors a number can have with exactly x factors
- Check if a number exists having exactly N factors and K prime factors
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- sort() vs. partial_sort() vs. nth_element() + sort() in C++ STL
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Sort an array according to the increasing count of distinct Prime Factors
- Number of factors of very large number N modulo M where M is any prime number
- Insertion sort using C++ STL
- Sort a nearly sorted array using STL
- How to sort an Array in descending order using STL in C++?
- How to sort a Vector in descending order using STL in C++?
- How to sort an Array using STL in C++?
- Sum of Factors of a Number using Prime Factorization
- Program to sort an array of strings using Selection Sort
- Odd Even Transposition Sort / Brick Sort using pthreads
- Sort an Array which contain 1 to N values in O(N) using Cycle Sort
- Super Ugly Number (Number whose prime factors are in given set)

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.