Sort on the basis of number of factors using STL
Last Updated :
16 Mar, 2023
Given an array of positive integers. Sort the given array in decreasing order of a number of factors of each element, i.e., an element having the highest number of factors should be the first to be displayed and the number having the least number of factors should be the last one. Two elements with an 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
We have already discussed a structure-based solution to sort according to a number of factors. The following steps sort numbers in decreasing order of count of factors.
- Count a distinct number of factors of each element. Refer this.
- Create a vector of pairs that stores elements and their factor counts.
- Sort this array based on the problem statement using any sorting algorithm.
Implementation:
CPP
#include <bits/stdc++.h>
using namespace std;
bool compare( const pair< int , int > &a,
const pair< int , int > &b) {
return (a.first > b.first);
}
void printSorted( int arr[], int n) {
vector<pair< int , int >> v;
for ( int i = 0; i < n; i++) {
int count = 0;
for ( int j = 1; j * j <= arr[i]; j++) {
if (arr[i] % j == 0) {
count++;
if (arr[i] / j != j)
count++;
}
}
v.push_back(make_pair(count, arr[i]));
}
sort(v.begin(), v.end(), compare);
for ( int i = 0; i < v.size(); i++)
cout << v[i].second << " " ;
}
int main() {
int arr[] = {5, 11, 10, 20, 9, 16, 23};
int n = sizeof (arr) / sizeof (arr[0]);
printSorted(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Pair
{
int a;
int b;
Pair( int a, int b)
{
this .a=a;
this .b=b;
}
}
class SComparator implements Comparator<Pair> {
public int compare(Pair o1, Pair o2)
{
if (o1.a == o2.a) {
return 0 ;
}
else if (o1.a < o2.a) {
return 1 ;
}
else {
return - 1 ;
}
}
}
class GFG {
static void printSorted( int arr[], int n)
{
ArrayList<Pair> v = new ArrayList<Pair>();
for ( int i = 0 ; i < n; i++) {
int count = 0 ;
for ( int j = 1 ; j * j <= arr[i]; j++) {
if (arr[i] % j == 0 ) {
count++;
if (arr[i] / j != j)
count++;
}
}
v.add( new Pair (count, arr[i]));
}
Collections.sort(v, new SComparator());
for (Pair i : v)
System.out.print(i.b+ " " );
}
public static void main(String[] args)
{
int arr[] = { 5 , 11 , 10 , 20 , 9 , 16 , 23 };
int n = arr.length;
printSorted(arr, n);
}
}
|
Python3
from functools import cmp_to_key
def compare(a, b):
return b[ 0 ] - a[ 0 ]
def printSorted(arr,n):
v = []
for i in range (n):
count = 0
j = 1
while (j * j< = arr[i]):
if (arr[i] % j = = 0 ):
count + = 1
if (arr[i] / j! = j):
count + = 1
j + = 1
v.append((count,arr[i]))
v = sorted (v, key = cmp_to_key(compare))
for a,b in v:
print (b,end = " " )
arr = [ 5 , 11 , 10 , 20 , 9 , 16 , 23 ]
n = len (arr)
printSorted(arr, n)
|
C#
using System;
using System.Collections.Generic;
class Pair
{
public int a;
public int b;
public Pair( int a, int b)
{
this .a = a;
this .b = b;
}
}
class SComparator : IComparer<Pair>
{
public int Compare(Pair o1, Pair o2)
{
if (o1.a == o2.a)
{
return 0;
}
else if (o1.a < o2.a)
{
return 1;
}
else
{
return -1;
}
}
}
public class GFG{
static void printSorted( int [] arr, int n)
{
List<Pair> v = new List<Pair>();
for ( int i = 0; i < n; i++)
{
int count = 0;
for ( int j = 1; j * j <= arr[i]; j++)
{
if (arr[i] % j == 0)
{
count++;
if (arr[i] / j != j)
count++;
}
}
v.Add( new Pair (count, arr[i]));
}
v.Sort( new SComparator());
foreach (Pair i in v)
Console.Write(i.b + " " );
}
static public void Main (){
int [] arr = { 5, 11, 10, 20, 9, 16, 23 };
int n = arr.Length;
printSorted(arr, n);
}
}
|
Javascript
function compare(a, b) {
return b[0] - a[0];
}
function printSorted(arr) {
const n = arr.length;
const v = [];
for (let i = 0; i < n; i++) {
let count = 0;
let j = 1;
while (j * j <= arr[i])
{
if (arr[i] % j == 0) {
count++;
if (arr[i] / j != j) count++;
}
j++;
}
v.push([count, arr[i]]);
}
v.sort(compare);
v.forEach((i) => console.log(i[1]));
}
const arr = [5, 11, 10, 20, 9, 16, 23];
printSorted(arr);
|
Output:
20 16 10 9 5 11 23
Time Complexity: O(n*log(n))
Auxiliary Complexity: O(n)
Share your thoughts in the comments
Please Login to comment...