Kth Root [codingblocks]

You are given two integers n and k. Find the greatest integer x, such that, x^k <= n.


Input Format
First line contains number of test cases, T. Next T lines contains integers, n and k.

Constraints
1<=T<=10
1<=N<=10^15
1<=K<=10^4

Output Format
Output the integer x

Sample Input
2
10000 1
1000000000000000 10

Sample Output
10000
31

Explanation
For the first test case, for x=10000, 10000^1=10000=n



Java Code:



/*
Amit Kumar
06-12-2020
IDE Code: https://ide.geeksforgeeks.org/VwbOuvQdfV
*/

package array;
import java.util.Scanner;

public class Array_KthRoot {
    public static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        int testCase = scanner.nextInt();
        while (testCase-- >0) {
            long n = scanner.nextLong();
            int k = scanner.nextInt();

            long head = 0;
            long tail = 1000000000000000L;
            long mid;
            long possibleAns=-1;
            while (head < tail) {
                mid = (head + tail)/2;
                if (Math.pow(mid, k) == n) {
                    System.out.println("LOL");
                    possibleAns = mid;
                    break;
                }

                if (Math.pow(mid, k) > n) {
                    tail = mid-1;
                } else  {
                    possibleAns = mid;
                    head = mid+1;
                }
            }
            System.out.println(possibleAns);
        }
    }
}

Comments

Post a Comment

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )