Given two integers N and M, the task is to print all elements in the range [N, M] having at least one odd divisor.
Examples:
Input: N = 2, M = 10
Output: 3 5 6 7 9 10
Explanation:
3, 6 have an odd divisor 3
5, 10 have an odd divisor 5
7 have an odd divisor 7
9 have two odd divisors 3, 9Input: N = 15, M = 20
Output: 15 17 18 19 20
Naive Approach:
The simplest approach is to loop through all numbers in the range [1, N], and for every element, check if it has an odd divisor or not.
Time Complexity: O(N^{3/2})
Efficient Approach:
To optimize the above approach, we need to observe the following details:
- Any number which is a power of 2, does not have any odd divisors.
- All other elements will have at least one odd divisors
Illustration:
In the range [3, 10], the elements which have at least one odd divisors are {3, 5, 6, 7, 9, 10} and {4, 8} does not contain any odd divisors.
Follow the steps below to solve the problem:
- Traverse the range [N, M] and check for each element if any of its set bit is set in the previous number or not, that is check whether i & (i – 1) is equal to 0 or not.
- If so, then the number is a power of 2 and is not considered. Otherwise, print the number as it is not a power of 2 and has at least one odd divisor.
Below is the implementation of the above approach.
C++
// C++ program to print all numbers // with least one odd factor in the // given range #include <bits/stdc++.h> using namespace std; // Function to prints all numbers // with at least one odd divisor int printOddFactorNumber( int n, int m) { for ( int i = n; i <= m; i++) { // Check if the number is // not a power of two if ((i > 0) && ((i & (i - 1)) != 0)) cout << i << " " ; } } // Driver Code int main() { int N = 2, M = 10; printOddFactorNumber(N, M); return 0; } |
Java
// Java program to print all numbers // with least one odd factor in the // given range class GFG{ // Function to prints all numbers // with at least one odd divisor static int printOddFactorNumber( int n, int m) { for ( int i = n; i <= m; i++) { // Check if the number is // not a power of two if ((i > 0 ) && ((i & (i - 1 )) != 0 )) System.out.print(i + " " ); } return 0 ; } // Driver Code public static void main(String[] args) { int N = 2 , M = 10 ; printOddFactorNumber(N, M); } } // This code is contributed // by shivanisinghss2110 |
Python3
# Python3 program to print all numbers # with least one odd factor in the # given range # Function to prints all numbers # with at least one odd divisor def printOddFactorNumber(n, m): for i in range (n, m + 1 ): # Check if the number is # not a power of two if ((i > 0 ) and ((i & (i - 1 )) ! = 0 )): print (i, end = " " ) # Driver Code N = 2 M = 10 printOddFactorNumber(N, M) # This code is contributed by Vishal Maurya |
C#
// C# program to print all numbers // with least one odd factor in the // given range using System; class GFG{ // Function to prints all numbers // with at least one odd divisor static int printOddFactorNumber( int n, int m) { for ( int i = n; i <= m; i++) { // Check if the number is // not a power of two if ((i > 0) && ((i & (i - 1)) != 0)) Console.Write(i + " " ); } return 0; } // Driver Code public static void Main() { int N = 2, M = 10; printOddFactorNumber(N, M); } } // This code is contributed // by Code_Mech |
3 5 6 7 9 10
Time Complexity: O(N)
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.
Recommended Posts:
- Sum of greatest odd divisor of numbers in given range
- Queries on XOR of greatest odd divisor of the range
- Greatest divisor which divides all natural number in range [L, R]
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Find even occurring elements in an array of limited range
- Array range queries to find the number of perfect square elements with updates
- Find the k-th smallest divisor of a natural number N
- Find an integer X which is divisor of all except exactly one element in an array
- Program to find Greatest Common Divisor (GCD) of N strings
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Minimum elements to be added in a range so that count of elements is divisible by K
- GCD of elements in a given range
- XOR of all the elements in the given range [L, R]
- Sum of elements of an AP in the given range
- XOR of a subarray (range of elements) | Set 2
- Sum of elements of a Geometric Progression (GP) in a given range
- Sum of multiples of Array elements within a given range [L, R]
- Number of elements with odd factors in given range
- Number of elements with even factors in the given range
- Queries for number of array elements in a range with Kth Bit Set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.