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 ≤ 10^{6}, 1 ≤ B ≤ 10^{12}, 1 ≤ N ≤ 10^{12}. All values in input are integers.

Input:A = 5, B = 7, N = 4

Output:2

Explanation:

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

Output: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:

## C++

`// C++ Program to find the maximum ` `// possible value for the given function ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum ` `// value of f(x) ` `int` `floorMax(` `int` `A, ` `int` `B, ` `int` `N) ` `{ ` ` ` `int` `x = min(B - 1, N); ` ` ` ` ` `return` `(A * x) / B; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `A = 11, B = 10, N = 9; ` ` ` ` ` `cout << floorMax(A, B, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

9

**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.

## Recommended Posts:

- Find Maximum and Minimum of two numbers using Absolute function
- Find maximum value of x such that n! % (k^x) = 0
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Find M such that GCD of M and given number N is maximum
- Find the maximum value of Y for a given X from given set of lines
- Find the Maximum possible Sum for the given conditions
- Find two numbers with given sum and maximum possible LCM
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Find a positive number M such that gcd(N^M, N&M) is maximum
- Find the maximum element in the array other than A
_{i} - Find the node whose sum with X has maximum set bits
- Find the maximum number of handshakes
- Find a pair from the given array with maximum nCr value
- Find maximum xor of k elements in an array
- Find the Substring with maximum product
- Find three integers less than or equal to N such that their LCM is maximum
- Given count of digits 1, 2, 3, 4, find the maximum sum possible
- Find a number M < N such that difference between their XOR and AND is maximum
- Find the maximum length of the prefix
- Find pair with maximum GCD in an array

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.