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:
For 20 = 6, 16 = 5, 10 = 4, 9 = 3
and for 5, 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
The following steps sort numbers in decreasing order of count of factors.
- Count distinct number of factors of each element. Refer this.
- You can use a structure for each element to store its original index and count of factors. Create an array of such structures to store such information for all the elements.
- Sort this array of structures on the basis of the problem statement using any sorting algorithm.
- Traverse this array of structures from the beginning and get the number from the original array with the help of the index stored in the structure of each element of the sorted array of structures.
C++
#include <bits/stdc++.h>
using namespace std;
struct element
{
int index, no_of_fact;
};
int countFactors( int n)
{
int count = 0;
int sq = sqrt (n);
if (sq * sq == n)
count++;
for ( int i=1; i< sqrt (n); i++)
{
if (n % i == 0)
count += 2;
}
return count;
}
bool compare( struct element e1, struct element e2)
{
if (e1.no_of_fact == e2.no_of_fact)
return e1.index < e2.index;
return e1.no_of_fact > e2.no_of_fact;
}
void printOnBasisOfFactors( int arr[], int n)
{
struct element num[n];
for ( int i=0; i<n; i++)
{
num[i].index = i;
num[i].no_of_fact = countFactors(arr[i]);
}
sort(num, num+n, compare);
for ( int i=0; i<n; i++)
cout << arr[num[i].index] << " " ;
}
int main()
{
int arr[] = {5, 11, 10, 20, 9, 16, 23};
int n = sizeof (arr) / sizeof (arr[0]);
printOnBasisOfFactors(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Comparator;
class Element
{
int index, no_of_fact;
public Element( int i, int countFactors)
{
index = i;
no_of_fact = countFactors;
}
static int countFactors( int n)
{
int count = 0 ;
int sq = ( int )Math.sqrt(n);
if (sq * sq == n)
count++;
for ( int i= 1 ; i<Math.sqrt(n); i++)
{
if (n % i == 0 )
count += 2 ;
}
return count;
}
static void printOnBasisOfFactors( int arr[], int n)
{
Element num[] = new Element[n];
for ( int i= 0 ; i<n; i++)
{
num[i] = new Element(i,countFactors(arr[i]));
}
Arrays.sort(num, new Comparator<Element>() {
@Override
public int compare(Element e1, Element e2) {
if (e1.no_of_fact == e2.no_of_fact)
return e1.index < e2.index ? - 1 : 1 ;
return e1.no_of_fact > e2.no_of_fact ? - 1 : 1 ;
}
});
for ( int i= 0 ; i<n; i++)
System.out.print(arr[num[i].index]+ " " );
}
public static void main(String[] args)
{
int arr[] = { 5 , 11 , 10 , 20 , 9 , 16 , 23 };
printOnBasisOfFactors(arr, arr.length);
}
}
|
Python3
import math
class Element:
def __init__( self , index, no_of_fact):
self .index = index
self .no_of_fact = no_of_fact
def countFactors(n):
count = 0
sq = int (math.sqrt(n))
if sq * sq = = n:
count + = 1
for i in range ( 1 , int (math.sqrt(n)) + 1 ):
if n % i = = 0 and n / i ! = i:
count + = 2
return count
def compare(e1, e2):
if e1.no_of_fact = = e2.no_of_fact:
return e1.index < e2.index
return e1.no_of_fact > e2.no_of_fact
def printOnBasisOfFactors(arr):
n = len (arr)
num = []
for i in range (n):
num.append(Element(i, countFactors(arr[i])))
num.sort(key = lambda x: (x.no_of_fact, - x.index), reverse = True )
for i in range (n):
print (arr[num[i].index], end = " " )
arr = [ 5 , 11 , 10 , 20 , 9 , 16 , 23 ]
printOnBasisOfFactors(arr)
|
C#
using System;
using System.Collections.Generic;
class Element
{
public int index, no_of_fact;
public Element( int i, int countFactors)
{
index = i;
no_of_fact = countFactors;
}
static int compare(Element e1, Element e2) {
if (e1.no_of_fact == e2.no_of_fact)
return e1.index < e2.index ? -1 : 1;
return e1.no_of_fact > e2.no_of_fact ? -1 : 1;
}
static int countFactors( int n)
{
int count = 0;
int sq = ( int )Math.Sqrt(n);
if (sq * sq == n)
count++;
for ( int i = 1; i < Math.Sqrt(n); i++)
{
if (n % i == 0)
count += 2;
}
return count;
}
static void printOnBasisOfFactors( int [] arr, int n)
{
Element[] num = new Element[n];
for ( int i = 0; i < n; i++)
{
num[i] = new Element(i,countFactors(arr[i]));
}
Array.Sort(num,(a,b)=>compare(a,b));
for ( int i = 0; i < n; i++)
Console.Write(arr[num[i].index] + " " );
}
public static void Main()
{
int [] arr = {5, 11, 10, 20, 9, 16, 23};
printOnBasisOfFactors(arr, arr.Length);
}
}
|
Javascript
<script>
class Element {
constructor(i, countFactors) {
this .index = i;
this .no_of_fact = countFactors;
}
}
function countFactors(n) {
let count = 0;
let sq = Math.floor(Math.sqrt(n));
if (sq * sq == n)
count++;
for (let i = 1; i < Math.sqrt(n); i++) {
if (n % i == 0)
count += 2;
}
return count;
}
function printOnBasisOfFactors(arr, n) {
let num = new Array(n);
for (let i = 0; i < n; i++) {
num[i] = new Element(i, countFactors(arr[i]));
}
num.sort((e1, e2) => {
if (e1.no_of_fact == e2.no_of_fact)
return e1.index < e2.index ? -1 : 1;
return e1.no_of_fact > e2.no_of_fact ? -1 : 1;
})
for (let i = 0; i < n; i++)
document.write(arr[num[i].index] + " " );
}
let arr = [5, 11, 10, 20, 9, 16, 23];
printOnBasisOfFactors(arr, arr.length);
</script>
|
Output:
20 16 10 9 5 11 23
Time Complexity: O(n ?n)
Auxiliary Space: O(n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
07 Mar, 2023
Like Article
Save Article