Hackerrank java Introduction loops II

In this post, we will solve Hackerrank Java Introduction loops II Challenge.

Problem statement :

You are given queries in the form of a, b, and n. For each query, print the series corresponding to the given a, b, and n values as a single line of space-separated integers.


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.


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

If you have any confusion please let me know in the comment box. I would be happy to help you. you can subscribe to the channel if you want to learn more about hackerrank java challenges. 

Leave a Reply

Your email address will not be published. Required fields are marked *