We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.
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();
}
}
(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();
}
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.
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.
Java Loops II
You are viewing a single comment's thread. Return to all 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.
From my HackerRank Java solutions.
Can you please explain what does this line do? a += b * (int) Math.pow(2, j);
is shorthand for
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
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.
Thank you very much for explaining me.
what's wrong with this? can you please tell me
I think the values for "a" and "c" are incorrect inside the inner for loop.
where the value of t is used ?what is neccesary of it in this code....
t is number of test cases
Great explanation. Thank you so much.
thank u mam
Nicely explained, thanks
you are the bad ass who actually understand
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.
Yes, only exception that code will terminate is ; first input 0 Great code @rshaghoulian
System.out.print(a + " ");......whats the meaning of this line?
(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();
}
I have written the same code but it's not working. Can you help
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.
can u explain this code
Hi. We want to print each term in the formula provided in the problem description. This is the code that does so:
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:
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.
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.
Why is a System.out.println(); added in the last. If I am remove it the code is not working. please help.
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
Thank you very much. I got it. You're awesome.
what will hapeen if we write System.out.println(a+" "); instead of System.out.print();
System.out.println(); What is the use of this?
This was answered in the post above you by Apporvsh. It makes the next print start on the next line.
HackerRank solutions.
Can you please explain me the logic of for (int j) loop.
someone explain me this logic
why a+b*(int)Math.pow(2,j) is assigned to 'a' can u tell me
for(i=0;i
can you please explain about int t and 1st for loop ..
** int t = scan.nextInt(); for (int i = 0; i < t; i++)**
Its the 6th time i am searching for your solutions. THanks for that.
Thanks Rodney, you never disappoint!
int t = scan.nextInt(); for (int i = 0; i < t; i++) { can you explain what this means
int t = scan.nextInt(); for (int i = 0; i < t; i++) Why this line had been taken?