bytelandian gold


import java.io.BufferedReader;
import java.io.InputStreamReader;
class test {
public static long coin(long n)
{
if((coin(n/4)+coin(n/3)+coin(n/2))>n)
{
return coin(n/4)+coin(n/3)+coin(n/2);
}
else
{
return n;
}
}
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int lin = 10;
while(lin–>0)
{
long n = Integer.parseInt(br.readLine());
System.out.println(coin(n));
}
}
}

its giving runtime error !!!

One obvious mistake i see is the way you have taken the input.

Read the statement carefully, The input will contain several test cases (not more than 10).

You have directly assumed that number of input will be = 10. But, here in thus problem we need to use EOF(End Of File).EOF is basically a character which indictes end of the file(obviously) and is used when we don’t know exact number of test cases. 10 is the upper limit and not the number of cases, there can be files with 1,2,3…10 cases.

So, to achieve end of file:

Replace in your code:


int lin = 10;
while(lin–>0)
{
long n = Integer.parseInt(br.readLine());
System.out.println(coin(n));
}

With:

   String input;
   while((input=br.readLine())!=null)
    {
        long n = Integer.parseInt(input);
        System.out.println(coin(n));
    }

Even after this, I’m not sure taht it will get AC(obviously do try once, good if it does :smiley: ). It might give you RE again. Try to work/optimize your recursive function. :slight_smile:

Hope this helps :slight_smile:

2 Likes

yaa needed to use hash table instead got an ac finally :slight_smile: thnx

1 Like