Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Friday, April 7, 2017

Express No. as sum of power of natural no.


100 = 10^2
             OR 6^2 + 8^2
             OR 1^2 + 3^2 + 4^2 + 5^2 + 7^2

Code with explanation:



import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
   int count=0,i=1,j=1;
   int a=ab.nextInt();
   int b=ab.nextInt();
//   System.out.println("");
   System.out.println(check(a,b,1,0));
}
}
public static int check(int a,int b,double curr_no,double cur_sum)
{
    int result=0;
    double p=Math.pow(curr_no,b);
    while(p+cur_sum<a)
    {
        result+=check(a,b,curr_no+1,p+cur_sum); // because every time result is 0
        curr_no++;//check with new no as the current scennario has been exeuted
        p=Math.pow(curr_no,b);
       
    }
    if(p+cur_sum==a)
    result++;
    return result;
}
}

0 Comments:

Post a Comment

Stats