Given an interval of integers [A, B]. For each number in this interval compute its greatest odd divisor. Output the sum of these divisors.
Input : A = 1, B = 3 Output : 5 1 + 1 + 3 = 5 Input : A = 3, B = 9 Output : 29 3 + 1 + 5 + 3 + 7 + 1 + 9 = 29
Naive Approach :
A simple approach is to iterate through all numbers in the range find their greatest odd divisor but this algorithm has time complexity of O(n).
Efficient Approach :
We need to find the answer in range [ A, B ] if we can find answer in range [ 1, B ] and subtract it from [ 1, A -1 ] then we will get our required answer.
Here we can see that –
- The answer for an odd number X is X itself.
- The answer for an even number X is equal to the answer for X/2. This is true because X and X/2 have the same odd divisors.( if X = 4 then 4 and 2 both have 1 as greatest odd divisor).
If we want to find answer in range [1, N], then first we need to determine the sum of all the odd numbers
( 1, 3, 5, …) using a simple formula: the sum of the first K odd numbers is equal to K2. Then we need to add the answers for the even numbers (2, 4, 6, …). But these are actually equal to the answers for 1, 2, 3, …, floor(N/2), so we can call our function recursively for floor(N/2).
The complexity of this algorithm is O(log( N)).
Below is the implementation of the above idea :
Time Complexity : O(log(N))
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.
- Greatest divisor which divides all natural number in range [L, R]
- Program to find Greatest Common Divisor (GCD) of N strings
- Find elements in a given range having at least one odd divisor
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Largest odd divisor Game to check which player wins
- Greatest odd factor of an even number
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Queries to find Kth greatest character in a range [L, R] from a string with updates
- Generating numbers that are divisor of their right-rotations
- Maximum sum after repeatedly dividing N by a divisor
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Greatest number that can be formed from a pair in a given Array
- Average of odd numbers till a given odd number
- Sum of range in a series of first odd then even natural numbers
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Sum of all odd length palindromic numbers within the range [L, R]
- Sum of all odd natural numbers in range L and R
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Sum of all odd factors of numbers in the range [l, r]
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.