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.
create a variable constant which will hold the value of 2^n-1
craete a variable counter that will go to n the number of iterations to do
finally create sum which will hold the final value of the previous iteration.
importjava.util.*;importjava.io.*;classSolution{publicstaticvoidmain(String[]argh){Scannerin=newScanner(System.in);intt=in.nextInt();for(inti=0;i<t;i++){inta=in.nextInt();intb=in.nextInt();intn=in.nextInt();intcount=0;//counterintconstant=0;// 2^(n-1) * bintsum=0;//answhile(count<n){if(count==0){constant=1;//2^0= 1sum=a+(constant*b)+sum;// sum=2}else{constant=constant*2;//power of 2sum=(constant*b)+sum;//power of 2 times the value of b; add the answer from previous iteration sum}System.out.print(sum+" ");count+=1;}//end whileSystem.out.println();}in.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Loops II
You are viewing a single comment's thread. Return to all comments →
This how I solved it
create a variable constant which will hold the value of 2^n-1 craete a variable counter that will go to n the number of iterations to do finally create sum which will hold the final value of the previous iteration.