Related Articles
Compute the maximum power with a given condition
• Last Updated : 27 Apr, 2021

Given 3 integers N, M, and K. The task is to find maximum P such that N * KP ≤ M.

Examples:

Input :N = 1, K = 2, M = 5
Output :2
Input: N = 5, K = 25, M = 100
Output: 0

Approach:
In this algorithm, simply multiply N with K and update the current value of N with the result and increase variable power (initially 0) by 1
To achieve this, a recursive function is defined which has 2 base cases.

1. Initially, N is greater than required, therefore, return 0.
2. Otherwise, return power – 1.
• If the current value of N is equal to the M then return power.
• Recursive condition if current N < M
Update N as (N * k) and power as current power + 1

Below is the implementation of the above approach:

## C++

 `// Compute maximum power to which K can be raised so``// that given condition remains true``#include ``using` `namespace` `std;` `#define ll long long` `// Function to return the largest``// power``int` `calculate(ll ``int` `n, ll ``int` `k,``              ``ll ``int` `m, ll ``int` `power)``{` `    ``// If n is greater than given M``    ``if` `(n > m) {``        ``if` `(power == 0)``            ``return` `0;``        ``else``            ``return` `power - 1;``    ``}` `    ``// If n == m``    ``else` `if` `(n == m)``        ``return` `power;` `    ``else``        ``// Checking for the next power``        ``return` `calculate(n * k, k, m, power + 1);``}` `// Driver Code``int` `main()``{``    ``ll N = 1, K = 2, M = 5;` `    ``cout << calculate(N, K, M, 0);` `    ``return` `0;``}`

## Java

 `// Java program for Compute maximum power``// to which K can be raised so that``// given condition remains true``class` `GFG``{` `// Function to return the largest``// power``static` `int` `calculate(``int` `n, ``int` `k,``                     ``int` `m, ``int` `power)``{` `    ``// If n is greater than given M``    ``if` `(n > m)``    ``{``        ``if` `(power == ``0``)``            ``return` `0``;``        ``else``            ``return` `power - ``1``;``    ``}` `    ``// If n == m``    ``else` `if` `(n == m)``        ``return` `power;` `    ``else``        ``// Checking for the next power``        ``return` `calculate(n * k, k, m,``                          ``power + ``1``);``}` `// Driver Code``public` `static` `void` `main (String[] args)``{``    ``int` `N = ``1``, K = ``2``, M = ``5``;` `    ``System.out.println(calculate(N, K, M, ``0``));``}``}` `// This code is contributed by AnkitRai01`

## Python

 `# Compute maximum power to``# which K can be raised so``# that given condition``# remains true`  `# Function to return the largest``# power``def` `calculate(n, k, m, power):    ``    ` `    ``# If n is greater than given M                        ``    ``if` `n > m:``        ``if` `power ``=``=` `0``:``            ``return` `0``        ``else``:``            ``return` `power``-``1``    ` `    ``# If n == m``    ``elif` `n ``=``=` `m:``        ``return` `power``    ``else``:``        ``# Checking for the next power``        ``return` `calculate(n ``*` `k, k, m, power ``+` `1``)` `# Driver's code    ``if` `__name__``=``=``"__main__"``:``    ` `    ``N ``=` `1``    ``K ``=` `2``    ``M ``=` `5``    ` `    ``print``(calculate(N, K, M, ``0``))`

## C#

 `// C# program for Compute maximum power``// to which K can be raised so that``// given condition remains true``using` `System;``class` `GFG``{` `// Function to return the largest``// power``static` `int` `calculate(``int` `n, ``int` `k,``                     ``int` `m, ``int` `power)``{` `    ``// If n is greater than given M``    ``if` `(n > m)``    ``{``        ``if` `(power == 0)``            ``return` `0;``        ``else``            ``return` `power - 1;``    ``}` `    ``// If n == m``    ``else` `if` `(n == m)``        ``return` `power;` `    ``else``        ``// Checking for the next power``        ``return` `calculate(n * k, k, m,``                         ``power + 1);``}` `// Driver Code``public` `static` `void` `Main (String[] args)``{``    ``int` `N = 1, K = 2, M = 5;` `    ``Console.WriteLine(calculate(N, K, M, 0));``}``}` `// This code is contributed by PrinciRaj1992`

## Javascript

 ``
Output:
`2`

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up