Sort on the basis of number of factors using STL


Sorting a vector using STL is a piece of cake. We can use the famous sort() function to perform the task. The real challenge is to count the number of factors for each number.

A factor is a number which divides another number completely, i.e. with zero remainder.

Traversing through all the numbers to count the factors might be an approach but we will try to optimize and reach efficient solutions in this article.

Problem Statement

Sort a given array based on the number of factors of each number in increasing order. Thus, the number having the lowest number of factors should be at the start and the number having the highest number of factors should be at last. The number having the same number of factors should be in the same order as the original array. STL can be used to sort the array.

Example

Input − Array a = [15,2,20,3,10,4]
Output − 3 2 4 10 15 20
pre class="just-code notranslate language-cpp" data-lang="cpp">
The number of factors of 15 − 4.
The number of factors of 2 −  2.
The number of factors of 20 − 6.
The number of factors of 3 −  2.
The number of factors of 10 − 4.
The number of factors of 4 −  3.

Hence, after sorting the number in increasing order according to their factors, we get the output: 3 2 4 10 15 20.

Input − Array a = [5,9,12,19,21]
Output − 19 5 9 21 12

Explanation

The number of factors of 5 −  3.
The number of factors of 9 −  3.
The number of factors of 12 − 4.
The number of factors of 19 − 2.
The number of factors of 21 − 4.

Hence, after sorting the number in increasing order according to their factors, we get the output: 19 5 9 21 12.

Approach

  • Find the number of factors of each number.

  • Create a vector of pairs that keep a record of the number and its factor count.

  • Sort the vector and return the result.

Finding the number of factors of a number

Brute Force

A naive approach would be to traverse through all the numbers from 1 to n and find if they divide n. This way, we can count the number of factors of each number.

Example

Below is a C++ program to count all divisors of a number by brute force

#include <bits/stdc++.h>
using namespace std;
// function to count the divisors
int countDivisors(int n){
   int count = 0;
	for (int i = 1; i <= n; i++){
	   if (n % i == 0)
		   count++;
	} 
   return count;
}

int main(){
   int n = 55;
   //Function call
   int ans = countDivisors(n);
	cout <<"The number of divisors of 55 is: "<<ans<<endl;
	return 0;
}

Output

The number of divisors of 55 is: 4

Efficient Method

The divisors of a number exist in pairs.

For example, the divisors of 12 are 1,2,3,4,6,12.

But, we can visualize them like this: (1,12), (2,6), (3,4).

Thus, if we find one divisor, we can find the other one too, and we don’t need to traverse till n.

Hence, the efficient method would be to only traverse to the square root of the number and then count the divisors in pairs.

Example

Below is a C++ Program to count the divisors of a number

#include <bits/stdc++.h>
using namespace std;
// Function to count the divisors of a number
int countDivisors(int n){
   int count = 0;
	for (int i=1; i<=sqrt(n); i++){
		if (n%i == 0){
			// If divisors are equal, count only one
			if (n/i == i)
				count++;
			else // Otherwise count both
				count += 2;
		}
	}
	return count;
}

int main(){
   int n = 55;
   int ans = countDivisors(n);
   cout <<"The number of divisors of 55 is: "<<ans<<endl;
   return 0;
}

Output

The number of divisors of 55 is: 4

Now, we can follow the 2nd and 3rd steps of the approach discussed above.

Example C++ Program to print the sorted vector based on the number of factors

#include <bits/stdc++.h>
using namespace std;
// Function to count the divisors of a number
int countDivisors(int n){
	int count = 0;
	for (int i=1; i<=sqrt(n); i++){
		if (n%i == 0){
			// If divisors are equal, count only one
			if (n/i == i)
				count++;
			else // Otherwise count both
				count += 2;
		}
	}
	return count;
}
int main(){
   int n = 5;
   vector<int>vec;
   //Inserting input
   vec.push_back(5);
   vec.push_back(14);
   vec.push_back(18);
   vec.push_back(9);
   vec.push_back(10);
   //Vector of pairs to store the number and its factor count
   vector<pair<int,int>>count_data(n);
   for(int i=0;i<n;i++){
      //Storing the data in the vector
      count_data[i] = {countDivisors(vec[i]), vec[i]};
   }
   //Sort the vector according to the number of factors
   sort(count_data.begin(),count_data.end());
   //Printing the result
   cout<<"The sorted vector based on the number of factors is: \n";
   for(int i=0;i<n;i++){
      cout<<count_data[i].second<<" ";
   }
   return 0;
}

Output

The sorted vector based on the number of factors is: 
5 9 10 14 18 

Conclusion

In this article, we sorted a vector of integers based on the number of their factors.

We discussed a few examples and then talked about the approach.

The core of this problem is to find the count of divisors of a number. There can be two ways of solving this problem: brute force, and an efficient method. We saw both approaches and then utilized the efficient one to write the final program.

Updated on: 16-Aug-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements