Question :
Given two weights of a
and b
units, in how many different ways you can achieve a weight of d
units using only the given weights? Any of the given weights can be used any number of times (including 0 number of times).
Example: 7 can only be achieved by using 2 two times and 3 one time. INPUT 2 3 7 (as a,b,d SEPERATED BY SPACES) OUTPUT:1
My program works fine on bluej but not in hackerearth online compiler. it says Runtime ERROR NZEC my code didn't print anything. Heres my program:
import java.io.*;
public class WAYS
{
public static int process(String s)
{
s=s+" ";
int l=s.length();
String s1="";
int n[]=new int[3];
int j=0;
for(int i=0;i<l;i++)
{
char c=s.charAt(i);
if(c==' ')
{
n[j]=Integer.parseInt(s1);
j++;
s1="";
}
else
{
s1=s1+c;
}
}
int a=n[0];
int b=n[1];
int d=n[2];
int q=0;
for(int i=0;i<=d;i++)
{
int f=a*i;
for(int k=0;k<=d;k++)
{
int f1=b*k;
if(f+f1==d)
{
q++;
}
}
}
return q;
}
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Number of Test Cases");
int T=Integer.parseInt(br.readLine());
String s[]=new String[T];
int a[]=new int[T];
for(int i=0;i<T;i++)
{
System.out.print("("+(i+1)+")");
s[i]=br.readLine();
a[i]=process(s[i]);
}
System.out.println();
for(int i=0;i<T;i++)
{
System.out.println(a[i]);
}
}
}
Can u tell where am i wrong??