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.
t is No. queries and n is power of 2. With t we can execute the nested For-loop. As shown below..............
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++){
a = a + (int)Math.pow(2,j) * b;
System.out.print(a + " ");
}
System.out.println("");
}
(1<<h) Starts with 2 to the zeroth power, a binary 1, Shifts the number to the left by h binary bits, e.g 00000001 in binary left shift 1 becomes 00000010, left shift 2 becomes 00000100.
Left-shifting a binary number by 1 bit position multiplies that number by 2. Left-shifting by 2 multiplies by 2^2. Left-shifting by 3 multiplies by 2^3, etc.
well doing so increases the looping hence it may cause time complexity issues (takes longer time ) hence to reduce that its easy to define another variable and assign it a value hence makes the process easy as looping tend to take much time to compile hence this shortens the time .
Could you please explain me about below three lines:
1. StringBuilder sb = new StringBuilder();
2. sb.setLength(0);
3. sb.append((int) (a + b*(Math.pow(2, j+1) - 1))).append(" ");
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
Yes.This is correct.
But I have one question...
In the series we can see evey value is multiply by b.(either first a also).But in your code I can't see that.
But your one gives correct answer.
Can you please explain that for me?
I used this and this gives wrong answerI can't realize why it is wrong???
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int num1, num2=0;
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 k=0;k < n; k++){
num1 =a;
num2 += (Math.pow(2,k)*b);
System.out.print(num1 + num2 + " ");
}
}
in.close();
}
I have two question first why is this not working and why did he use (int) beofre Math.pow().
Thank You
Math.pow returns a double.
To store it in an int variable you have to typecast it to int {putting (int) before the value}
typecasting a double to an int removes all the digits after the decimal.
12.25 becomes 12
13.99 becomes 13
14.00 becomes 14
bro...why are u giving int datatype near to math.pow,is it for a printing purpose like, the output will be in integer form so for that ur giving the int datatype or what?
class Java_Loops_II {
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int a=0;
int b=0;
int n=0;
int t = in.nextInt();
for(int i=0;i<t;i++ ) {
a = in.nextInt();
b = in.nextInt();
n = in.nextInt();
for(int x=0;x<n;x++) {
int s = a;
for(int j=0;j<=x;j++) {
s =s+ (int) Math.pow(2,j)*b;
}
System.out.printf(s +" ");
}
}
}
Here are the Easiest way Without Using any in-build function
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int time = sc.nextInt();
int e = 1;
for (int i = 0; i < time; i++) {
int a = sc.nextInt(), b = sc.nextInt(), n = sc.nextInt();
for (int j = 0; j < n; j++) {
a = a + b * e;
e *= 2;
System.out.print(a + " ");
}
e = 1;
System.out.println();
}
sc.close();
}
Java Loops II
You are viewing a single comment's thread. Return to all comments →
Guys, done without creating another object please checkout
That's good. You can simplify even further if you realize that 2^0 + 2^1 + ... + 2^j = 2^(j+1) - 1
What does variable 't' hold in this program?
The number of queries, which is the integer on the first line of the input.
then what's about n? I'm confuse in between these two. Can you please explain.
2 3 3 4 5 6 5 7 n = 8
t is No. queries and n is power of 2. With t we can execute the nested For-loop. As shown below..............
to get q
I didn't need to call the Math.pow() function.
I used
what does this line(result += (1<< h) * b) do? what is the role of << operator?
a += b; is the same as a = a + b;
(1<<h) Starts with 2 to the zeroth power, a binary 1, Shifts the number to the left by h binary bits, e.g 00000001 in binary left shift 1 becomes 00000010, left shift 2 becomes 00000100.
Left-shifting a binary number by 1 bit position multiplies that number by 2. Left-shifting by 2 multiplies by 2^2. Left-shifting by 3 multiplies by 2^3, etc.
thankyou..
nice idea with the binary shift!
<< is the bitwise left shift operator.
1 << h takes the bits in 1 and shifts them with h positions to the left.
1 << 0 == 1 1 << 1 == 2 1 << 2 == 4
for(int i=0;i
Should be h<=x+1
best Answer Yet!
Why did you represent another variable result? Is it impossible if a is directly call in the for loop of shifting?
What does result do?
well doing so increases the looping hence it may cause time complexity issues (takes longer time ) hence to reduce that its easy to define another variable and assign it a value hence makes the process easy as looping tend to take much time to compile hence this shortens the time .
whats that String builder means?
It is one of the represents sequence of the character. But here you can change string value. because String Builder and String Buffer is mutable.
Could you please explain me about below three lines: 1. StringBuilder sb = new StringBuilder(); 2. sb.setLength(0); 3. sb.append((int) (a + b*(Math.pow(2, j+1) - 1))).append(" ");
ye math.pow kya hai
its predefine function.with the help of pow we can get power of any int value
Is Math.pow() an inbuilt function in JAVA?
Yes
what is the function of math.pow
math.pow(2, 3) is 2^3 (3rd power of two) and will result in 8. Basic math.
yes
to the power of
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)
exponentiation
excellent
Sir, It dosen't Work.The line"System.out.println("");"does not change the line for 2nd input.
make sure, it is after the block.
I also do this,but it is wrong!Why?
here a = a + (int)Math.pow(2,j)*b;
why we used a= a + __ : can we use another integer like int c=0; c= a + (int)Math.pow(2,j)*b;
if not please explain
why u use t integer variable here?
Clear and Perfect...!! Thanks
Yes.This is correct. But I have one question... In the series we can see evey value is multiply by b.(either first a also).But in your code I can't see that. But your one gives correct answer. Can you please explain that for me?
I used this and this gives wrong answerI can't realize why it is wrong???
This line:
Should be:
Since you're loop from 0 you code is calculating a + 2^0*b twice and you will get a wrong result.
And this line:
The '^' it xor (exclusive or) operator not pow operator. You have to use
Math.pow();
or a shift operation.Thank You. I have used the shift operation as you said but two test cases failed out of 5. Could you please help me with this code?
Thanks #sharmatushar124 for ur Soln.
but what i the need of 't' here
silly me, i was calculating each term individually (3 nested for loops instead of 2) which would increase time complexity alot. Thanks xD
I have two question first why is this not working and why did he use (int) beofre Math.pow(). Thank You
Math.pow returns a double. To store it in an int variable you have to typecast it to int {putting (int) before the value} typecasting a double to an int removes all the digits after the decimal. 12.25 becomes 12 13.99 becomes 13 14.00 becomes 14
System.out.print(a + " "); System.out.println(""); can you please explain this and this
Thanks .It's working without any error.
why are we assigning the value to a?
Hi,
What is the relevance of the below statement in the Program.Please explain.
System.out.println("");
hey can you tell me what is this type of assignment that you used (int)Math.pow(2,j), thanks
bro...why are u giving int datatype near to math.pow,is it for a printing purpose like, the output will be in integer form so for that ur giving the int datatype or what?
import java.util.; import java.io.;
class Java_Loops_II { public static void main(String []argh){ Scanner in = new Scanner(System.in);
}
i Cannot understand whats the use of ' t' over there.
if we dont put System.out.println(""); line why the ouput is not comming correct.?
What is the System.out.println(""); line for?
Here are the Easiest way Without Using any in-build function
import java.util.Scanner;
class Solution {
}
e *= 2;
what is was missing, supherb answer friend.
thanks
i understood everything but whats the use of "time" variable in here could you please tell me about this
its just the number of queries. in default code 't' is given. bro changed it to time.
Thank you. i understood the logic very well. you are awesome. keep going.
Can U plz Explain why u use System.out.println("");
hmm this command outputs "space" or "whitespace" for our output
what is j and (int)Math.pow(2,j) for
this is the correct code
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
}