Given a positive integer n. The task is to find the sum of product of x and y such that ⌊n/x⌋ = y (Integer Division).
Input : n = 5 Output : 21 Following are the possible pairs of (x, y): (1, 5), (2, 2), (3, 1), (4, 1), (5, 1). So, 1*5 + 2*2 + 3*1 + 4*1 + 5*1 = 5 + 4 + 3 + 4 + 5 = 21. Input : n = 10 Output : 87
Method 1 (Brute Force):
Iterate x from 1 to n to find y. Then add x*y to the answer over each iteration.
Below is the implementation of this approach :
Time Complexity : O(n)
Method 2 (Efficient Approach):
Let’s solve for n = 10, so
x = 1, y = 10
x = 2, y = 5
x = 3, y = 3
x = 4, y = 2
x = 5, y = 2
x = 6, y = 1
x = 7, y = 1
x = 8, y = 1
x = 9, y = 1
x = 10, y = 1
So, our answer would be 1*10 + 2*5 + 3*3 + 4*2 + 5*2 + 6*1 + 7*1 + 8*1 + 9*1 + 10*1.
Now, observe some value of y is repeating. Also, observe that they are repeating for some range of consecutive value of x like y = 1 is repeating for x = 6 to 10.
So, instead of finding the value of y for all the value of x (1 to n) as done in method 1, try to find the lower and higher value of x for which the value of possible value of y like for y = 1 try to find lower value of x = 6 and higher value of x = 10. Now, observe lower value will be (n/(y+1)) + 1 and higher value will be (n/y). Find the sum of range of x and multiply with y and add to the answer.
How to find the possible value of y?
Observe, y has all values from 1 to sqrt(n) when y is smaller than or equal to x. So for y = 1 to sqrt(n), find the lower and higher limits of x for each y. For n = 10,
y = 1, lo = 6 and hi = 10, ans += (6 + 7 + 8 + 9 + 10)*1
y = 2, lo = 4 and hi = 5, ans += (4 + 5)*2
y = 3, lo = 3 and hi = 3, ans += (3)*3
For other values to be added (for y = 10 and 5 in n = 10), observe they can be found in above steps, for each y, add y * (n/y) in the answer.
For n = 10,
y = 1, ans += 1 * (10/1)
y = 2, ans += 2 * (10/2).
Below is the implementation of this approach:
Time Complexity : O((√n)
This article is contributed by Anuj Chauhan(APC). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- A Product Array Puzzle
- Find a pair with maximum product in array of Integers
- Breaking an Integer to get Maximum Product
- Largest palindrome which is product of two n-digit numbers
- Find if n can be written as product of k numbers
- First digit in product of an array of numbers
- Find minimum value to assign all array elements so that array product becomes greater
- Trailing number of 0s in product of two factorials
- A product array puzzle | Set 2 (O(1) Space)
- Sum and Product of Prime Frequencies of Characters in a String
- Number of digits in the product of two numbers
- Check whether a number can be expressed as a product of single digit numbers
- Smallest number k such that the product of digits of k is equal to n
- Digit - Product - Sequence
- Product of factors of number