0

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??

ArK
  • 20,698
  • 67
  • 109
  • 136

3 Answers3

0

Your error is most likely because you are creating too many in memory variables. You must create variables outside the for loop and manipulate it inside the loop.

EG : char c=s.charAt(i); , int f=a*i; and int f1=b*k;

StackFlowed
  • 6,664
  • 1
  • 29
  • 45
0

It's probably because the statement -

n[j]=Integer.parseInt(s1); // since s1 here is ""

could be leading to a NumberFormatException in your code.

Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.


Smallest piece of code to reproduce that is --

public static void main(String[] args) {
        String str = "";
        int n = Integer.parseInt(str);
}

And although I am not sure about the algorithm you are following, would suggest assigning some numeric value as string to s1 if that conversion is a must. Also, do read - How to resolve java.lang.NumberFormatException: For input string: "N/A"?

Community
  • 1
  • 1
Naman
  • 27,789
  • 26
  • 218
  • 353
0

Finally fixed the problem. WHEN USING JAVA, ALWAYS USE 'String args[]' and' throws Exception' in main method. Example: public static void main(String args[])throws Exception{} to avoid this silly NZEC error. If that's not working, make return type of your main method to 'int' and return 0 before closing your main method. Also check for infinite loops.

  • Just make your main method `public static void main(String[] args)`. Anything else is *not* a main method! For example, if it doesn't take a String array parameter, then it is not going to be called as a main method, but will be considered just an ordinary method. – Klitos Kyriacou Dec 12 '16 at 14:28