Given two integers n and k, find how many integers x satisfy the condition that the integral part (floor value) of the kth root of x is n.
Examples:
Input: n = 3, k = 2
Output: 7
Explanation: 9, 10, 11, 12, 13, 14, 15 have 3 as integral part of there square root.Input: n = 2, k = 3
Output: 19
Explanation: 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 have 2 as integral part of there cube root.
Table of Content
[Naive Approach] Directly Count Valid Integers - O(k + ((n + 1) ^ k - n ^ k)) Time and O(1) Space
The idea is to find the range of values of x whose kth root has integral part n, and then count every value in that range one by one. The valid range is from n^k to (n + 1)^k - 1.
Working of Approach:
- Calculate n^k as the starting value.
- Calculate (n + 1)^k as the upper boundary.
- Iterate from n^k to (n + 1)^k - 1.
- Count every integer in this range.
- Return the total count.
#include <iostream>
using namespace std;
int integralRoot(int n, int k)
{
// Calculate n^k.
int left = 1;
for (int i = 0; i < k; i++)
left *= n;
// Calculate (n + 1)^k.
int right = 1;
for (int i = 0; i < k; i++)
right *= (n + 1);
// Count every integer in the valid range.
int cnt = 0;
for (int x = left; x < right; x++)
cnt++;
return cnt;
}
int main()
{
int n = 2, k = 3;
cout << integralRoot(n, k) << endl;
return 0;
}
import java.util.Scanner;
public class GFG {
// Calculate n^k.
public static int integralRoot(int n, int k)
{
int left = 1;
for (int i = 0; i < k; i++)
left *= n;
// Calculate (n + 1)^k.
int right = 1;
for (int i = 0; i < k; i++)
right *= (n + 1);
// Count every integer in the valid range.
int cnt = 0;
for (int x = left; x < right; x++)
cnt++;
return cnt;
}
public static void main(String[] args)
{
int n = 2, k = 3;
System.out.println(integralRoot(n, k));
}
}
def integralRoot(n, k):
# Calculate n^k.
left = 1
for i in range(k):
left *= n
# Calculate (n + 1)^k.
right = 1
for i in range(k):
right *= (n + 1)
# Count every integer in the valid range.
cnt = 0
for x in range(left, right):
cnt += 1
return cnt
if __name__ == '__main__':
n = 2
k = 3
print(integralRoot(n, k))
using System;
public class GFG {
// Calculate n^k.
public static int integralRoot(int n, int k)
{
int left = 1;
for (int i = 0; i < k; i++)
left *= n;
// Calculate (n + 1)^k.
int right = 1;
for (int i = 0; i < k; i++)
right *= (n + 1);
// Count every integer in the valid range.
int cnt = 0;
for (int x = left; x < right; x++)
cnt++;
return cnt;
}
public static void Main()
{
int n = 2, k = 3;
Console.WriteLine(integralRoot(n, k));
}
}
function integralRoot(n, k)
{
// Calculate n^k.
let left = 1;
for (let i = 0; i < k; i++)
left *= n;
// Calculate (n + 1)^k.
let right = 1;
for (let i = 0; i < k; i++)
right *= (n + 1);
// Count every integer in the valid range.
let cnt = 0;
for (let x = left; x < right; x++)
cnt++;
return cnt;
}
// Driver Code
let n = 2, k = 3;
console.log(integralRoot(n, k));
Output
19
[Expected Approach] Using Binary Exponentiation - O(log k) Time and O(1) Space
The idea is to observe that floor(kth root(x)) = n when n^k <= x < (n + 1)^k. Therefore, the number of valid integers is simply (n + 1)^k - n^k. We calculate both powers efficiently using binary exponentiation.
Working of Approach:
- Calculate n^k using binary exponentiation.
- Calculate (n + 1)^k using binary exponentiation.
- All valid values of x lie between these two values.
- The number of integers in this range is right - left.
- Return this difference as the answer.
Let us understand with an example:
Input: n = 2, k = 3
- Calculate left = power(2, 3) = 8 and right = power(3, 3) = 27.
- power(2, 3) uses binary exponentiation and gives 8.
- power(3, 3) uses binary exponentiation and gives 27.
- The valid values lie from 8 to 26, so the count is 27 - 8 = 19.
- Therefore, the output is 19.
#include <iostream>
using namespace std;
int power(int base, int exp)
{
int res = 1;
// Compute base raised to the given exponent.
while (exp > 0)
{
if (exp & 1)
res *= base;
base *= base;
exp >>= 1;
}
return res;
}
int integralRoot(int n, int k)
{
// Compute n^k and (n + 1)^k.
int left = power(n, k);
int right = power(n + 1, k);
// Return the number of integers having n as the integral kth root.
return right - left;
}
int main()
{
int n = 2, k = 3;
cout << integralRoot(n, k) << endl;
return 0;
}
public class GFG {
public static int power(int base, int exp)
{
int res = 1;
// Compute base raised to the given exponent.
while (exp > 0) {
if ((exp & 1) != 0) {
res *= base;
}
base *= base;
exp >>= 1;
}
return res;
}
public static int integralRoot(int n, int k)
{
// Compute n^k and (n + 1)^k.
int left = power(n, k);
int right = power(n + 1, k);
// Return the number of integers having n as the
// integral kth root.
return right - left;
}
public static void main(String[] args)
{
int n = 2, k = 3;
System.out.println(integralRoot(n, k));
}
}
def power(base, exp):
res = 1
# Compute base raised to the given exponent.
while exp > 0:
if exp & 1:
res *= base
base *= base
exp >>= 1
return res
def integralRoot(n, k):
# Compute n^k and (n + 1)^k.
left = power(n, k)
right = power(n + 1, k)
# Return the number of integers having n as the integral kth root.
return right - left
if __name__ == '__main__':
n = 2
k = 3
print(integralRoot(n, k))
using System;
public class GFG {
public static int power(int baseNum, int exp)
{
int res = 1;
// Compute base raised to the given exponent.
while (exp > 0) {
if ((exp & 1) != 0)
res *= baseNum;
baseNum *= baseNum;
exp >>= 1;
}
return res;
}
public static int integralRoot(int n, int k)
{
// Compute n^k and (n + 1)^k.
int left = power(n, k);
int right = power(n + 1, k);
// Return the number of integers having n as the
// integral kth root.
return right - left;
}
public static void Main()
{
int n = 2, k = 3;
Console.WriteLine(integralRoot(n, k));
}
}
function power(base, exp)
{
let res = 1;
// Compute base raised to the given exponent.
while (exp > 0) {
if (exp & 1) {
res *= base;
}
base *= base;
exp >>= 1;
}
return res;
}
function integralRoot(n, k)
{
// Compute n^k and (n + 1)^k.
let left = power(n, k);
let right = power(n + 1, k);
// Return the number of integers having n as the
// integral kth root.
return right - left;
}
// Driver Code
let n = 2, k = 3;
console.log(integralRoot(n, k));
Output
19