
Java Loops II gives you three numbers a, b and n, and asks you to print the first n terms of a series that grows by doubling. It is the first problem in HackerRank's Java track where a loop has to remember something from the previous round. That idea, keeping a running total instead of recalculating everything from scratch, is behind a huge number of algorithms you will meet later, such as prefix sums.
It also brings in nested loops (one loop for queries, one for terms) and a small output detail that catches many students: numbers separated by spaces, with no extra space at the end of the line.
Problem
For a query with values a, b and n, the series is defined term by term:
- term 0 = a + 2^0·b
- term 1 = a + 2^0·b + 2^1·b
- term 2 = a + 2^0·b + 2^1·b + 2^2·b
- and so on, so term k adds the powers of two from 2^0 up to 2^k, each multiplied by
b.
You will be given q queries. For each, print the first n terms on one line, separated by single spaces.
Input format: the first line has q. Each of the next q lines has three integers a b n.
Output format: one line per query containing n space-separated integers.
Constraints: 0 ≤ q ≤ 500, 0 ≤ a, b ≤ 50, 1 ≤ n ≤ 15.
Input:
2
0 2 10
5 3 5
Output:
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
Approach
The slow way is to compute every term from the formula, summing all the powers each time. That works, but notice that each term is just the previous term plus one more piece. Term 1 is term 0 plus 2^1·b. Term 2 is term 1 plus 2^2·b. So we can keep two running values:
term, starting ata.power, starting at 1 (that is 2^0).
In each round: add power * b to term, print term, then double power. Repeat n times. An outer loop runs this once per query.
Dry run on the second query (a = 5, b = 3, n = 5):
- Start: term = 5, power = 1.
- Round 1: term = 5 + 1·3 = 8. Print 8. power = 2.
- Round 2: term = 8 + 2·3 = 14. Print 14. power = 4.
- Round 3: term = 14 + 4·3 = 26. Print 26. power = 8.
- Round 4: term = 26 + 8·3 = 50. Print 50. power = 16.
- Round 5: term = 50 + 16·3 = 98. Print 98.
That gives 8 14 26 50 98, matching the sample.
Solution in Java
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
StringBuilder out = new StringBuilder();
for (int query = 0; query < q; query++) {
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int term = a; // running total, starts at a
int power = 1; // 2^0, then 2^1, 2^2, ...
for (int j = 0; j < n; j++) {
term += power * b; // add the next piece of the series
if (j > 0) out.append(' '); // space only between numbers
out.append(term);
power *= 2;
}
out.append('\n');
}
in.close();
System.out.print(out);
}
}
Notes on the Java used:
Scanner.nextInt()reads numbers separated by any whitespace, so it does not matter that three numbers share a line.StringBuildercollects all the output in memory and prints it once at the end. With 500 queries this is faster than hundreds of separate print calls, and it is a habit worth building for bigger problems.term += xis shorthand forterm = term + x, andpower *= 2doublespower.
The old solution on this page reused a and b as the running values (a += b; b = b * 2). That is correct and gives the same output, it just overwrites the inputs, which makes the code harder to follow. Here the running values have their own names.
Is int big enough? The largest case is a = b = 50, n = 15. The last term is 50 + 50·(2^15 − 1) = 1,638,400, far below the int limit of about 2.1 billion. I ran that case, plus 0 0 1 (prints 0) and q = 0 (prints nothing), along with the sample.
Output
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
Complexity
O(q · n) time, since each term costs a constant amount of work. Extra space is O(q · n) for the output buffer, or O(1) if you print directly.
Common mistakes
- Printing
aas the first term. The first term already includes2^0·b, so for the first sample it is 2, not 0. - Calling
Math.pow(2, j). It returns adouble, so you either get output like8.0or need a cast. Doubling anintis simpler and exact. - A trailing space after the last number, or forgetting the newline between queries.
- Resetting
termandpoweroutside the outer loop, so the second query continues from where the first left off.
Previous in the Java track: Java Loops I. Next: Java Substring Comparisons.
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
More HackerRank solutions All coding problems MCQ practice
See everything in one place: Coding Problems and Solutions
Comments
Post a Comment