Given three numbers A, B, and N, the task is to find the maximum possible value of floor(A * x / B) – A * floor(x / b) where x is a non-negative integer less than or equal to N. Here floor(T) = denotes the greatest integer not greater than the real number T (G.I.F function).
Constraints: 1 ≤ A ≤ 106, 1 ≤ B ≤ 1012, 1 ≤ N ≤ 1012. All values in input are integers.
Input: A = 5, B = 7, N = 4
The maximum value is obtained for the value x = 3. On substituting this value in the equation:
floor((5 * 3)/7) – (5 * floor(3 / 7)) = floor(2.1) – 0 = 2.
Input: A = 11, B = 10, N = 9
Naive Approach: The naive approach for this problem is to consider all the possible numbers from 1 to N and compute the maximum possible value.
Time Complexity: O(N).
Efficient Approach: The idea is to make an observation on the function f(x) = floor(A * x / B) – A * floor(x / B).
- We can observe that the given function is a periodic function. This can be proved by:
f(x + B) = floor(A * (x + B)/B) – A * floor((x + B)/B)
=> f(x + B) = floor((A * x / B) + A) – A * floor((x /B) + 1)
By floor-function property, floor(x + Integer) = Integer + floor(x).
=> f(x + B) = floor(A * x / B) – A * floor(x / B) = f(x)
- Hence, we can conclude that 0 ≤ x ≤ B. However, if x = B, f(x) = 0. So, we exclude it and get 0 ≤ x ≤ B-1.
- However, we must also consider the condition x ≤ N. Since floor(x) is a monotonically non-decreasing function, we must incorporate the best of both the ranges.
- Hence, the maximum value of f(x) is obtained when x = min(B – 1, N).
Below is the implementation of the above approach:
Time Complexity: 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.
- Count distinct regular bracket sequences which are not N periodic
- Find the maximum possible value of the minimum value of modified array
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Minimum value possible of a given function from the given set
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Find the maximum value of Y for a given X from given set of lines
- Rearrange array to obtain maximum possible value of concatenation of prefix GCDs
- Given count of digits 1, 2, 3, 4, find the maximum sum possible
- Find the maximum possible distance from origin using given points
- Find the Maximum possible Sum for the given conditions
- Find two numbers with given sum and maximum possible LCM
- Maximum sum possible from given Matrix by performing given operations
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Find the largest possible value of K such that K modulo X is Y
- Possible pairs forming a Pythagorean Triple with a given value
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Find a pair from the given array with maximum nCr value
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Find maximum value of the last element after reducing the array with given operations
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.