Given a number N, the task is to find whether N has an equal number of odd and even factors.
Input: N = 10
Explanation: 10 has two odd factors (1 and 5) and two even factors (2 and 10)
Input: N = 24
Explanation: 24 has two odd factors (1 and 3) and six even factors (2, 4, 6, 8 12 and 24)
Input: N = 125
Naive Approach: Find all the divisors and check whether count of odd divisors is same as count of even divisors.
Below is the implementation of the above approach
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
The following observation must be made to optimize the above approach:
- According to Unique Factorisation Theorem any number can be expressed in terms of the product of the power of primes. So, 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)
- Using law of combinators any divisor of N would be of the form :
N = P1B1 * P2B2 * P3B3 * …….. * PKBK where Bi is an integer and 0 <= Bi <= Ai for 1 <= i <= K.
- A divisor would be odd if it dosen’t contain 2 in its prime factorisation. So, if P1 = 2 then B1 must be 0. It can be done in only 1 way.
- For even divisors, B1 can be replaced by 1 (or) 2 (or)….A1 to get a divisor. It can be done in B1 ways.
- Now for others each Bi can be replaced either with 0 (or) 1 (or) 2….(or) Ai for 1 <= i <= K. It can be done in (Ai+1) ways.
- By Fundamental principle :
- Number of odd divisors are: X = 1 * (A2+1) * (A3+1) * ….. * (AK+1).
- Similarly, Number of even divisors are: Y = A1 * (A2+1) * (A3+1) * …. * (AK+1).
- For no. of even divisors to be equal to no. of odd divisors X, Y should be equal. This is possible only when A1 = 1.
So, it can be concluded that number of even and odd divisors of a number are equal, if it has 1 (and only 1) power of 2 in its prime factorisation.
Follow the steps below to solve the problem:
- For a given number N, check if it is divisible by 2.
- If the number is divisible by 2, then check if it is divisible by 22. If yes, then the number won’t have an equal number of odd and even factors. If not, then the number will have an equal number of odd and even factors.
- If the number is not divisible by 2, then the number will never have any even factor and thus it won’t have an equal number of odd and even factors.
Below is the implementation of the above approach.
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Python | Count the array elements with factors less than or equal to the factors of given x
- Check if count of even divisors of N is equal to count of odd divisors
- Check if a number exists having exactly N factors and K prime factors
- Check whether a given number is even or odd
- Check whether given floating point number is even or odd
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Queries to check whether bitwise AND of a subarray is even or odd
- Check if product of digits of a number at even and odd places is equal
- Maximum number of prime factors a number can have with exactly x factors
- Generate an array of given size with equal count and sum of odd and even numbers
- Check whether product of 'n' numbers is even or odd
- Check whether bitwise OR of N numbers is Even or Odd
- Check whether XOR of all numbers in a given range is even or odd
- Check whether a number has exactly three distinct factors or not
- Count subarrays having sum of elements at even and odd positions equal
- Find number of factors of N when location of its two factors whose product is N is given
- Queries on sum of odd number digit sums of all the factors of a number
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.