Java Loops II

  • + 18 comments

    Java solution - passes 100% of test cases

    If using Math.pow(), make sure you cast it to an integer since it returns a double.

    import java.util.Scanner;
    
    class Solution{
        public static void main(String [] args) {
            Scanner scan = new Scanner(System.in);
            int t = scan.nextInt();
            for (int i = 0; i < t; i++) {
                int a = scan.nextInt();
                int b = scan.nextInt();
                int n = scan.nextInt();
                
                for (int j = 0; j < n; j++) {
                    a += b * (int) Math.pow(2, j);
                    System.out.print(a + " ");
                }
                System.out.println();
            }
            scan.close();
        }
    }
    

    From my HackerRank Java solutions.

    • + 2 comments

      Can you please explain what does this line do? a += b * (int) Math.pow(2, j);

      • + 5 comments
        a += b * (int) Math.pow(2, j);
        

        is shorthand for

        a = a + b * (int) Math.pow(2, j);
        

        Math.pow() is a function that is provided to us by Java. Here, it takes 2 parameters. When I pass in 2 and j, it does exponentiation (known as "power") as 2 to the j-th power. For example, Math.pow(3,5) = 3 * 3 * 3 * 3 * 3.

        Also, Math.pow() returns a double instead of an int. We "cast" it to an integer by putting

        (int)
        

        in front of it. We cast our result since a is an integer type and we can only assign an int type to it.

        Hope this helps.

        HackerRank solutions.

        • + 1 comment

          Thank you very much for explaining me.

          • + 2 comments

            what's wrong with this? can you please tell me

            class Solution{
                public static void main(String []argh){
                    Scanner in = new Scanner(System.in);
                    int t=in.nextInt();
                    for(int i=0;i<t;i++){
                        int a = in.nextInt();
                        int b = in.nextInt();
                        int n = in.nextInt();
            
                        for (int j = 0; j < n; j++) {
                            int c = a + b * (int) Math.pow(2, j);
                            System.out.print(a + " ");
                            a = c;
                        }
                        System.out.println();
                    }
                    in.close();
                }
            }
            
            • + 0 comments

              I think the values for "a" and "c" are incorrect inside the inner for loop.

            • + 1 comment

              where the value of t is used ?what is neccesary of it in this code....

              • + 0 comments

                t is number of test cases

        • + 0 comments

          Great explanation. Thank you so much.

        • + 0 comments

          thank u mam

        • + 0 comments

          Nicely explained, thanks

        • + 0 comments

          you are the bad ass who actually understand

      • + 0 comments

        The value of 'b x 2^j' gets added to 'a' in that expression. It is like 'a = a + (b x 2^j)'. Hope this helps.

    • + 0 comments

      Yes, only exception that code will terminate is ; first input 0 Great code @rshaghoulian

    • + 1 comment

      System.out.print(a + " ");......whats the meaning of this line?

      • + 0 comments

        (answer for ur question is if consider a =10 , that line prints a blank space for the value i.e., 10) import java.util.; import java.io.;

        class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int t=in.nextInt();

            for(int i=0;i<t;i++){
                int k =0;
                int r=0;
                int a = in.nextInt();
                int b = in.nextInt();
                int n = in.nextInt();
               // System.out.println(a+" "+b+" "+n );
                for (double j =0;j<n;j++){
                r = r+(int)(Math.pow(2.0,j)*b);
                k=r+a;
                System.out.print(k+" ");
                }
                System.out.print("\n");
            }
        
            in.close();
        }
        

        }

    • + 1 comment

      I have written the same code but it's not working. Can you help

      • + 0 comments

        Sure. Try to compare your code to the code I have posted above to see what's different. Change 1 line at a time from your code to mine to see when it starts working.

        HackerRank solutions.

    • + 1 comment

      can u explain this code

      • + 0 comments

        Hi. We want to print each term in the formula provided in the problem description. This is the code that does so:

        for (int j = 0; j < n; j++) {
             a += b * (int) Math.pow(2, j);
             System.out.print(a + " ");
        }
        

        Try to compare the value of a in my code above to each term in the equation in the problem statement. First, for j= 0, we will get:

        a + (2^0)*b
        

        which matches the first term in the formula in the problem statement.

        Try to see what we get for j=1 and j=2.

        HackerRank solutions.

    • + 0 comments

      While I agree this works, not sure if this is what was intended from the prompt. In the last section it describes: "Once we hit n=10, we print the first ten terms as a single line of space-separated integers.", which would imply that they want you to do the entire series calculation first, then print the result.

      Seems a bit weird that this is where it is in the challenge list, as I would think a recursive solution is called for in that case.

    • + 1 comment

      Why is a System.out.println(); added in the last. If I am remove it the code is not working. please help.

      • + 2 comments

        Because you need the other test case on a different line

        you want this!!

        2 6 14 30 62 126 254 510 1022 2046

        8 14 26 50 98

        you don't want this

        2 6 14 30 62 126 254 510 1022 2046 8 14 26 50 98

        • + 0 comments

          Thank you very much. I got it. You're awesome.

        • + 0 comments

          what will hapeen if we write System.out.println(a+" "); instead of System.out.print();

    • + 1 comment

      System.out.println(); What is the use of this?

      • + 0 comments

        This was answered in the post above you by Apporvsh. It makes the next print start on the next line.

        HackerRank solutions.

    • + 0 comments

      Can you please explain me the logic of for (int j) loop.

    • + 0 comments

      someone explain me this logic

    • + 0 comments

      why a+b*(int)Math.pow(2,j) is assigned to 'a' can u tell me

    • + 0 comments

      for(i=0;i

    • + 0 comments

      can you please explain about int t and 1st for loop ..

      ** int t = scan.nextInt(); for (int i = 0; i < t; i++)**

    • + 0 comments

      Its the 6th time i am searching for your solutions. THanks for that.

    • + 0 comments

      Thanks Rodney, you never disappoint!

    • + 0 comments

      int t = scan.nextInt(); for (int i = 0; i < t; i++) { can you explain what this means

    • + 0 comments

      int t = scan.nextInt(); for (int i = 0; i < t; i++) Why this line had been taken?