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 firstname.lastname@example.org. 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.
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.
- Program for dot product and cross product of two vectors
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Find an index such that difference between product of elements before and after it is minimum
- Maximum element in an array such that its previous and next element product is maximum
- Sum and Product of all even digit sum Nodes of a Singly Linked List
- Sum of series formed by difference between product and sum of N natural numbers
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Smallest number k such that the product of digits of k is equal to n
- Python | Number of elements to be removed such that product of adjacent elements is always even
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Print N numbers such that their product is a Perfect Cube
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Rearrange an array such that product of every two consecutive elements is a multiple of 4
- Sum and product of k smallest and k largest prime numbers in the array
- Sum and product of k smallest and k largest composite numbers in the array
- Derive a MultiSet from given Array such that sum is > P and removing any element makes sum < P
- Sum and Product of Prime Frequencies of Characters in a String
- Find N integers with given difference between product and sum
- Maximum of sum and product of digits until number is reduced to a single digit