Given a positive integer N, the task is to find the absolute difference of the count of odd and even factors of N.
Examples:
Input: N = 12
Output: 2
Explanation: The even factors of 12 are {2, 4, 6, 12}. Therefore, the count is 4.
The odd factors of 12 are {1, 3}. Therefore, the count is 2.
Hence, the difference between their counts is (4 – 2) = 2.Input: N = 9
Output: 3
Naive Approach: The simplest approach to solve the given problem is to find all the divisors of the number N and then find the absolute difference of count of odd and even divisors of N.
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized which is based on the following observations:
- According to the Unique Factorization Theorem, any number can be expressed in terms of the product of the power of primes. Therefore, N can be expressed as:
N = P1A1 * P2A2 * P3A3 * ……… * PkAK
where, each Pi is a prime and each Ai is a positive integer.(1 ? i ? K)
- Therefore, the total number of factors = (A1 + 1)*(A2 + 1)*(A3 + 1)* ……… *(A4 + 1). Let this count be T.
- The total number of odd factors can be calculated by excluding the power of 2 in the above formula. Let this count be O.
- The total number of even factors is equal to the difference between the total number of factors and the total number of odd factors.
Therefore, the idea is to find the prime factors and their powers in the prime factorization of N by using the Sieve of Eratosthenes and print the value absolute value of the difference of the total number of factors and twice the total number of odd factors as the result i.e., abs(T – 2*O).
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the smallest prime // factor of all the numbers using // Sieve Of Eratosthenes void sieveOfEratosthenes( int N, int s[]) { // Stores whether any number // is prime or not vector< bool > prime(N + 1, false ); // Initialize smallest factor as // 2 for all the even numbers for ( int i = 2; i <= N; i += 2) s[i] = 2; // Iterate over the range [3, N] for ( int i = 3; i <= N; i += 2) { // If i is prime if (prime[i] == false ) { s[i] = i; // Iterate all multiples of i for ( int j = i; j * i <= N; j += 2) { // i is the smallest // prime factor of i * j if (!prime[i * j]) { prime[i * j] = true ; s[i * j] = i; } } } } } // Function to find the absolute // difference between the count // of odd and even factors of N void findDifference( int N) { // Stores the smallest // prime factor of i int s[N + 1]; // Fill values in s[] using // sieve of eratosthenes sieveOfEratosthenes(N, s); // Stores the total number of // factors and the total number // of odd and even factors int total = 1, odd = 1, even = 0; // Store the current prime // factor of the number N int curr = s[N]; // Store the power of // current prime factor int cnt = 1; // Loop while N is greater than 1 while (N > 1) { N /= s[N]; // If N also has smallest // prime factor as curr, then // increment cnt by 1 if (curr == s[N]) { cnt++; continue ; } // Update only total number // of factors if curr is 2 if (curr == 2) { total = total * (cnt + 1); } // Update total number of // factors and total number // of odd factors else { total = total * (cnt + 1); odd = odd * (cnt + 1); } // Update current prime // factor as s[N] and // count as 1 curr = s[N]; cnt = 1; } // Calculate the number // of even factors even = total - odd; // Print the difference cout << abs (even - odd); } // Driver Code int main() { int N = 12; findDifference(N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the smallest prime // factor of all the numbers using // Sieve Of Eratosthenes static void sieveOfEratosthenes( int N, int s[]) { // Stores whether any number // is prime or not boolean []prime = new boolean [N + 1 ]; // Initialize smallest factor as // 2 for all the even numbers for ( int i = 2 ; i <= N; i += 2 ) s[i] = 2 ; // Iterate over the range [3, N] for ( int i = 3 ; i <= N; i += 2 ) { // If i is prime if (prime[i] == false ) { s[i] = i; // Iterate all multiples of i for ( int j = i; j * i <= N; j += 2 ) { // i is the smallest // prime factor of i * j if (!prime[i * j]) { prime[i * j] = true ; s[i * j] = i; } } } } } // Function to find the absolute // difference between the count // of odd and even factors of N static void findDifference( int N) { // Stores the smallest // prime factor of i int []s = new int [N + 1 ]; // Fill values in s[] using // sieve of eratosthenes sieveOfEratosthenes(N, s); // Stores the total number of // factors and the total number // of odd and even factors int total = 1 , odd = 1 , even = 0 ; // Store the current prime // factor of the number N int curr = s[N]; // Store the power of // current prime factor int cnt = 1 ; // Loop while N is greater than 1 while (N > 1 ) { N /= s[N]; // If N also has smallest // prime factor as curr, then // increment cnt by 1 if (curr == s[N]) { cnt++; continue ; } // Update only total number // of factors if curr is 2 if (curr == 2 ) { total = total * (cnt + 1 ); } // Update total number of // factors and total number // of odd factors else { total = total * (cnt + 1 ); odd = odd * (cnt + 1 ); } // Update current prime // factor as s[N] and // count as 1 curr = s[N]; cnt = 1 ; } // Calculate the number // of even factors even = total - odd; // Print the difference System.out.print(Math.abs(even - odd)); } // Driver Code public static void main(String[] args) { int N = 12 ; findDifference(N); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program for the above approach # Function to find the smallest prime # factor of all the numbers using # Sieve Of Eratosthenes def sieveOfEratosthenes(N, s): # Stores whether any number # is prime or not prime = [ False ] * (N + 1 ) # Initialize smallest factor as # 2 for all the even numbers for i in range ( 2 , N + 1 , 2 ): s[i] = 2 # Iterate over the range [3, N] for i in range ( 3 , N, 2 ): # If i is prime if (prime[i] = = False ): s[i] = i # Iterate all multiples of i for j in range (i, N, 2 ): if j * i > N: break # i is the smallest # prime factor of i * j if ( not prime[i * j]): prime[i * j] = True s[i * j] = i # Function to find the absolute # difference between the count # of odd and even factors of N def findDifference(N): # Stores the smallest # prime factor of i s = [ 0 ] * (N + 1 ) # Fill values in s[] using # sieve of eratosthenes sieveOfEratosthenes(N, s) # Stores the total number of # factors and the total number # of odd and even factors total , odd , even = 1 , 1 , 0 # Store the current prime # factor of the number N curr = s[N] # Store the power of # current prime factor cnt = 1 # Loop while N is greater than 1 while (N > 1 ): N / / = s[N] # If N also has smallest # prime factor as curr, then # increment cnt by 1 if (curr = = s[N]): cnt + = 1 continue # Update only total number # of factors if curr is 2 if (curr = = 2 ): total = total * (cnt + 1 ) # Update total number of # factors and total number # of odd factors else : total = total * (cnt + 1 ) odd = odd * (cnt + 1 ) # Update current prime # factor as s[N] and # count as 1 curr = s[N] cnt = 1 # Calculate the number # of even factors even = total - odd # Print the difference print ( abs (even - odd)) # Driver Code if __name__ = = '__main__' : N = 12 findDifference(N) # This code is contributed by mohit kumar 29. |
C#
// C# program for the above approach using System; class GFG { // Function to find the smallest prime // factor of all the numbers using // Sieve Of Eratosthenes static void sieveOfEratosthenes( int N, int [] s) { // Stores whether any number // is prime or not bool [] prime = new bool [N + 1]; // Initialize smallest factor as // 2 for all the even numbers for ( int i = 2; i <= N; i += 2) s[i] = 2; // Iterate over the range [3, N] for ( int i = 3; i <= N; i += 2) { // If i is prime if (prime[i] == false ) { s[i] = i; // Iterate all multiples of i for ( int j = i; j * i <= N; j += 2) { // i is the smallest // prime factor of i * j if (!prime[i * j]) { prime[i * j] = true ; s[i * j] = i; } } } } } // Function to find the absolute // difference between the count // of odd and even factors of N static void findDifference( int N) { // Stores the smallest // prime factor of i int [] s = new int [N + 1]; // Fill values in s[] using // sieve of eratosthenes sieveOfEratosthenes(N, s); // Stores the total number of // factors and the total number // of odd and even factors int total = 1, odd = 1, even = 0; // Store the current prime // factor of the number N int curr = s[N]; // Store the power of // current prime factor int cnt = 1; // Loop while N is greater than 1 while (N > 1) { N /= s[N]; // If N also has smallest // prime factor as curr, then // increment cnt by 1 if (curr == s[N]) { cnt++; continue ; } // Update only total number // of factors if curr is 2 if (curr == 2) { total = total * (cnt + 1); } // Update total number of // factors and total number // of odd factors else { total = total * (cnt + 1); odd = odd * (cnt + 1); } // Update current prime // factor as s[N] and // count as 1 curr = s[N]; cnt = 1; } // Calculate the number // of even factors even = total - odd; // Print the difference Console.Write(Math.Abs(even - odd)); } // Driver Code public static void Main() { int N = 12; findDifference(N); } } // This code is contributed by ukasp. |
Javascript
<script> // Javascript implementation of the above approach // Function to find the smallest prime // factor of all the numbers using // Sieve Of Eratosthenes function sieveOfEratosthenes(N, s) { // Stores whether any number // is prime or not let prime = Array.from({length: N+1}, (_, i) => 0); // Initialize smallest factor as // 2 for all the even numbers for (let i = 2; i <= N; i += 2) s[i] = 2; // Iterate over the range [3, N] for (let i = 3; i <= N; i += 2) { // If i is prime if (prime[i] == false ) { s[i] = i; // Iterate all multiples of i for (let j = i; j * i <= N; j += 2) { // i is the smallest // prime factor of i * j if (!prime[i * j]) { prime[i * j] = true ; s[i * j] = i; } } } } } // Function to find the absolute // difference between the count // of odd and even factors of N function findDifference(N) { // Stores the smallest // prime factor of i let s = Array.from({length: N+1}, (_, i) => 0); // Fill values in s[] using // sieve of eratosthenes sieveOfEratosthenes(N, s); // Stores the total number of // factors and the total number // of odd and even factors let total = 1, odd = 1, even = 0; // Store the current prime // factor of the number N let curr = s[N]; // Store the power of // current prime factor let cnt = 1; // Loop while N is greater than 1 while (N > 1) { N /= s[N]; // If N also has smallest // prime factor as curr, then // increment cnt by 1 if (curr == s[N]) { cnt++; continue ; } // Update only total number // of factors if curr is 2 if (curr == 2) { total = total * (cnt + 1); } // Update total number of // factors and total number // of odd factors else { total = total * (cnt + 1); odd = odd * (cnt + 1); } // Update current prime // factor as s[N] and // count as 1 curr = s[N]; cnt = 1; } // Calculate the number // of even factors even = total - odd; // Print the difference document.write(Math.abs(even - odd)); } // Driver Code let N = 12; findDifference(N); </script> |
2
Time Complexity: O(N*(log log N))
Auxiliary Space: O(N), since n extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!