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(" ");
Java Loops II
You are viewing a single comment's thread. Return to all comments →
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