Jason arora
Jason arora

Reputation: 550

Output for finding kth digit from right of a^b does not come as Expected?

Given two numbers a and b, find kth digit from right of a^b?

Link for the problem:

http://www.practice.geeksforgeeks.org/problem-page.php?pid=302

MyApproach:

I took 4 numbers as Input.First was the number of test cases.Then,Input was numbers a b and k respectively(Seperated by space).I calculated a^b and then from right searched each number till the time kth digit is not equal to the count.If I get them equal I returned the remainder expected.

Below is the Code:

public static void main (String[] args) 
{
       Scanner sc=new Scanner(System.in);
          int T=sc.nextInt();

          for(int i=1;i<=T;i++)
          { 
              int count=1;
              int a=sc.nextInt();
              System.out.print(" ");
              int b=sc.nextInt();
               System.out.print(" ");
              int k=sc.nextInt();
              long result=(long) Math.pow(a,b);
              if(k!=count)
              {
                  while(k!=count)
                  {
                      count++;
                      int remainder=(int) (result%10);
                      result=result/10;

                  }
              }
              result=result%10;
              System.out.println(result);
          }
    }


GeeksId Output:                  

Wrong !! Here your code Failed 

Input: 

7 6 3 

And its Correct output is: 

6

Eclipse ID:

Input:

7 6 3

Output

6

Wny I am getting Failed on geeksId?Is my solution do not produce correct output?

Upvotes: 0

Views: 1130

Answers (2)

Vasanth Jegathesan
Vasanth Jegathesan

Reputation: 86

Can also be done this way without much String operations and not expecting the intermediate output a^b to store in long data type,

private void handle() {
    Scanner scanner = new Scanner(System.in);
    int T = scanner.nextInt();
    for(int i = 0; i < T; i++) {
        findKthDigit(scanner);
    }
    scanner.close();
}

private void findKthDigit(Scanner scanner) {
    int a = scanner.nextInt();
    int b = scanner.nextInt();
    int k = scanner.nextInt();
    System.out.println((int)((Math.pow(a, b) % Math.pow(10, k))
            / Math.pow(10, k-1)));
}

As requested, your program is modified to make it work in GFG system,

    public static void main (String[] args) 
    {
      Scanner sc=new Scanner(System.in);
      int T=sc.nextInt();

      for(int i=1;i<=T;i++)
      { 
          int count=1;
          int a=sc.nextInt();
          //System.out.print(" ");
          int b=sc.nextInt();
          // System.out.print(" ");
          int k=sc.nextInt();
          long result=(long) Math.pow(a,b);
          if(k!=count)
          {
              while(k!=count)
              {
                  count++;
                  int remainder=(int) (result%10);
                  result=result/10;

              }
          }
          result=result%10;
          System.out.println(result);
      }
}

Hope this helps to understand. (however, you need to check other related exceptions for a good programming practice)

Upvotes: 1

deltashade
deltashade

Reputation: 386

It seems like you did not follow the direction of the problem. The problem gives you a few constraints. you do not do a check for those. You should add the code to check that, so it will make sure you do not run into any exceptions.

I wanted to point out that you can just convert the Math.pow(a,b) result to string, and print the length - k char using the charAt function. This will make it very easy. and gets rid of the loops.

Code for that part is:

String tempString = String.valueOf(result);
System.out.println(tempString.charAt(tempString.length() - k));

Hope this puts you in the right direction.

Upvotes: 2

Related Questions