10 September 2016

Lisa's Workbook

Standard

In this post i will explain the solution of the following problem at hackerrank.com.

Here is the problem statement.


This problem looks difficult at the first sight but it's not too difficult if you go step by step.
  1. Create a loop which runs through the chapters.
  2. Inside that loop create another loop which runs through all the problems in  that particular chapter and checks whether the problem number is equal to chapter number or not.
AN ADVICE : Try writing a code before reading further.Keep in mind the above 2 steps. If you don't succeed than come back here and read further.
First of all initialize two int variables to keep track of the page number and the number of special problem,i have initialized pagenum and spb as these two variables.Initialize another three variables for the number of chapters, maximum number of problems per page and the number of problems per chapter.
Take the inputs i.e. the number of chapters and the maximum number of problems per chapter. Now create a loop which runs through the chapters.
Now take another input which will be the number of problems per chapter. After that create another loop which will run through the problems in a particular chapter and check whether the problem number is equal to the page number or not. If equal than increase the variable  spb by 1, if not than continue. Take a look at my solution.
Hope it helped you :)
#include <iostream>
using namespace std;
int main() {
                int n,k,pagenum=1,spb=0, numprob;
                 cin>>n>>k;
 for(int i=1;i<=n;i++){
               cin>>numprob;          

               for(int j=1; j&lt =numprob; j++){   /* looping through the problems. */
                      if( pagenum==j&nbsp)spb++; /*if problem number is equal to page number than increase spb.*/
                    if( j==numprob || j%k==0 ) pagenum++; /*if j reaches the problem number or j%k=0 then increase the page number.*/
               }
  }
cout<<spb; /*print the number of special problems.*/
return 0;
}

*****
IF YOU WANT ME TO EXPLAIN A PERTICULAR PROBLEM THAN PLEASE LEAVE A LINK TO THE PROBLEM IN THE COMMENT SECTION
*****

27 comments:

  1. Super Bowl LI will be the 51st Super Bowl and the 47th modern-era National Football League .... The Touchdown Tour is a way to excite the Houston community about the 2016-2017 football season culminating in Super Bowl LI.
    Super Bowl 51
    Super Bowl 51 Live
    Super Bowl 51 Live Stream
    Watch Super Bowl 51
    Watch Super Bowl 51 Live
    Super Bowl 51 Live Online

    ReplyDelete
  2. Thanks for sharing your view on this, it is really very useful.

    ReplyDelete
  3. Hi admin
    Its acutely impressive, that is the account you adore I agreeable it.
    I accept additionally this accompanying site.
    This commodity covers the recommended best practices for website design. What is a charge and what to abstain to body a acceptable looking, user affable website.
    Web design brisbane
    Best Regards
    Lori Donna

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Thanks To Watch The Masters 2017 Live Stream, one of the most prestigious tournaments in golf, is set to take place this week at Augusta National Golf Club from April 6-9.

    Masters 2017

    The Masters 2017

    Masters 2017

    The Masters 2017

    Masters Golf

    UFC 210

    UFC 210

    We already know Tiger Woods won’t be Masters 2017 Live participating in the 2017 Masters, but there are still intriguing questions entering this year’s tournament.

    ReplyDelete
  6. A week after Chavez vs Canelo went toe-to-toe in an instant classic, boxing fans get another star-studded fight when Saul “Canelo” Alvarez (48-1-1, 34 KOs) and Julio Cesar Chavez Jr. (50-2-1, 32 KOs) meet at the T-Mobile Arena in Las Vegas.
    Canelo vs Chavez Jr

    Chavez Jr vs Canelo

    Chavez Jr vs Canelo

    ReplyDelete
  7. Little mistake:
    It should be
    int main() {
    int n,k,t,page=1,spb=0;
    cin>>n>>k;
    for(int c=1;c<=n;c++){
    cin>>t;
    int p;
    for(p=1;p<=t;p++){
    if(p==page)
    spb++;
    if(p==k || p%k==0)
    page++;
    }
    if((p-1)%k!=0)//if last problem ends in the middle of the page...next chapter
    page++; //should start from the next page...(p-1) is the last problem
    }
    cout<<spb;
    return 0;
    }

    ReplyDelete
  8. The SummerSlam 2017 is an upcoming professional wrestling pay-per-view (PPV) event and WWE Network event produced by WWE for the Raw and WWE SummerSlam brands. It will take place on August 20, 2017, at the Barclays Center in Brooklyn, New York. It will be the thirtieth event under the SummerSlam chronology.
    WWE SummerSlam 2017

    SummerSlam Live

    WWE SummerSlam Live

    Watch SummerSlam

    Watch WWE SummerSlam

    SummerSlam live Stream

    WWE SummerSlam Live Stream

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. href="https://istanbulolala.biz/">https://istanbulolala.biz/
    CUV

    ReplyDelete