Hackerrank java Introduction loops II


Here we have been given 3 things - a, b , n. so now try to learn the series pattern.

you are given q queries means you have to loop through or process the series q times. if you will concentrate on the series structure you will get the series pattern.

we are having a geometric progression of 2 multiplied by b and we are adding this with a.

Example:


first term = a + pow(2,0)*b = 5 + 1*3 = 8
second term = a + pow(2,0)*b + pow (2,1)*b = 5 + 1*3 + 2*3 = 14
third term =  a + pow(2,0)*b + pow (2,1)*b + pow (2,2)*b = 5 + 1*3 + 2*3 + 4*3 = 26

similarly
nth term =  a + pow(2,0)*b + pow (2,1)*b + pow (2,2)*b + ------- + pow (2,n-1)*b

so what we will do is that.. we will make a loop and iterate it to n-1 times starting from 0.

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

for more clarification watch - 







Comments

Related Posts Plugin for WordPress, Blogger...