The program must accept N positive
integers as the input and sort them based on the factor count (lowest to
highest factor count). If two numbers have the same factor count, order based
on the value of the numbers in the ascending order.
Input
Format:
The first line will contain N. The second
line will contain N positive integers separated by a space.
Output
Format:
The first line will contain the N positive
integers (separated by a space) ordered by their factor count.
Boundary
Conditions:
2 <= N <= 10000
Example
Input/Output:
Input:
5
18 23 100 1200 45
Output:
23 18 45 100 1200
Solution
1:
import math
n,x,y=int(input()),[int(i) for i in
input().split()],[]
for i in x:
s=math.ceil(i**0.5)
c=1 if s*s==i else 0
for j in range(1,s):
if i%j==0:
c+=2
y.append(c)
f=sorted(list(zip(y,x)))
print(*[j for i,j in f])
Solution
2:
#include <bits/stdc++.h>
using namespace std;
int cnt(int a)
{
int c,i,s=sqrt(a);
s*s==a?c=1:c=0;
for(i=1;i<sqrt(a);i++)
if(a%i==0)
c+=2;
return c;
}
bool fc(int x,int y)
{
int a=cnt(x),b=cnt(y);
return a==b?x<y:a<b;
}
int main()
{
int
i,n;
cin>>n;
int
a[n];
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n,fc);
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}