Search code examples
javalogic

I tryed to do stairCase problem with n stairs and 1or2or3 steps at a time But my solution is working fine until 13 after that not, its giving less


public class StairCase {
    public static int Solution(int n) {
        int sum, count = 0;
//Funtion to calculate stair case and use factorial because a solution can have many ways.
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                for (int k = 0; k <= n; k++) {
                    sum = i + (j * 2) + (k * 3);
                    if (sum == n) {
                        count = count + (fact.fact1(i + j + k) / (fact.fact1(i) * fact.fact1(j) * fact.fact1(k)));
                    }
                }
            }

/*logic behind this funtion is that like we have to find no of arrangement abbbccde can we written so we write (8!/(3!*2!)) so after getting like ijk= 1,2,3 I rearranged them in that man ways (i+j+k)!/i!*j!k! !=factorial/

        }
        return count;
    }

    public static void doTestPass() {

        boolean result = true;
        result = result && (Solution(3) == 4);
        result = result && (Solution(4) == 7);
        result = result && (Solution(11) == 504);
        result = result && (Solution(12) == 927);
        result = result && (Solution(13) == 1705);
//working fine till 13 not after that
        System.out.println(Solution(14));
        System.out.println(Solution(20));
        System.out.println(Solution(21));

//14--3127 20--68603 21--94351(orignal ans-- 3136,121415,223317)

        if (result) {
            System.out.println("All Test Cases Passed");
        } else {
            System.out.println("Test Case Failed");
        }
    }

    public static void main(String[] Args) {
        doTestPass();
    }

public class fact

{

 static int fact1(int n)

{

//this funtion is to create factorial

    int res = 1;

    for (int i = 2; i <= n; i++)

{

        res = res * i;

}

   return res;

}

}

}

Solution

  • The problem has to do with using primitive integers for your factorial method and overflowing the integer limit of 2,147,483,647.

    Take a look at trial runs of your factorial code below:

    fact1(12) outputs 479001600 which is correct for 12!

    fact1(13) outputs 1932053504 which is not 13! which should be 6227020800

    This means when you execute fact.fact1(i) in your code, it will be outputting the wrong answer for any value greater than 12. This would require you to rework the data structure you use to hold these big numbers to BigInteger.

    The reworked fact method would look like this:

    BigInteger fact1(int n) {
        // this funtion is to create factorial
        BigInteger res = BigInteger.ONE;
        for (int i = 2; i <= n; i++) {
            res = res.multiply(BigInteger.valueOf(i));
        }
        return res;
    }
    

    With the output of fact1(13) being the correct value of 6227020800 now.

    You will need to rework the other aspects of your code now with BigInteger, but you should understand now what your issue was.