Java Loops II

  • [deleted]
    + 4 comments

    You need to take the value of a, b and n and create a series of ans using this (a + 2^0 * b),..., (a + 2^0 * b + 2^n-1 * b)

    q is just tells the number of quires you'll be performing, if q is 3 then you'll have to create 3 sereies of ans using the series that they have given you.

    q = 3;

    a = 0 b = 2 n = 10

    a = 5 b = 3 n = 5

    a = 1 b = 8 n = 3

    [2, 6,..., 2046]

    [8, 14,...,98]

    [9, 25, 57]

    n will tell you how long will series of ans will be. a = 0; b = 2 ; n = 10; (0 + 2^0 * b), [(0 + 2^0 * b) + (2^1 * b)] ... (0 + 2^0 * b + 2^1 * b + a + 2^0 * b ) Your series of ans be [2 , 6 ... ans]

    The catch in the series is you only need to add the new 2^n-1 to the previous calculation you did.

    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 a = in.nextInt();
                int b = in.nextInt();
                int n = in.nextInt();
            
                int count = 0;
                int constant = 0;
                int sum = 0;
                
                while(count < n){
                    if(count == 0){
                        constant = 1;
                        sum = a + (constant*b) + sum;
                        }else{
                        constant = constant * 2;
                        sum = (constant * b) + sum;
                    }
                    System.out.print (sum + " ");
                    count += 1;
                }//end while
            System.out.println();
            
            
            
            
            }
            in.close();
        }
    }