# Lisa’s Workbook – HackerRank Solution

In this post, we will solve Lisa’s Workbook HackerRank Solution. This problem (Lisa’s Workbook) is a part of HackerRank Problem Solving series.

Lisa just got a new math workbook. A workbook contains exercise problems, grouped into chapters. Lisa believes a problem to be special if its index (within a chapter) is the same as the page number where it’s located. The format of Lisa’s book is as follows:

• There areÂ nÂ chapters in Lisa’s workbook, numbered fromÂ 1Â toÂ n.
• TheÂ ithÂ chapter hasÂ arr[i]Â problems, numbered fromÂ 1Â toÂ arr[i].
• Each page can holdÂ up toÂ kÂ problems. Only a chapter’s last page of exercises may contain fewer thanÂ kÂ problems.
• Each new chapter starts on a new page, so a pageÂ will neverÂ contain problems from more than one chapter.
• The page number indexing starts atÂ 1.

Given the details for Lisa’s workbook, can you count its number ofÂ specialÂ problems?

Example

arr = [4, 2]
k = 3

Lisa’s workbook containsÂ arr[1] = 4Â problems for chapterÂ 1, andÂ arr[2] = 2Â problems for chapterÂ 2. Each page can holdÂ k = 3Â problems.

The first page will holdÂ 3Â problems for chapterÂ 1. ProblemÂ 1Â is on pageÂ 1, so it isÂ special. PageÂ 2Â contains only ChapterÂ 1, ProblemÂ 4, so noÂ specialÂ problem is on pageÂ 2. ChapterÂ 2Â problems start on pageÂ 3Â and there areÂ 2Â problems. Since there is no problemÂ 3Â on pageÂ 3, there is noÂ specialÂ problem on that page either. There isÂ 1Â specialÂ problem in her workbook.

Note: See the diagram in the Explanation section for more details.

Function Description

Complete the workbook function in the editor below.

workbook has the following parameter(s):

• int n: the number of chapters
• int k: the maximum number of problems per page
• int arr[n]: the number of problems in each chapter

Returns
–Â int:Â the number of special problems in the workbook

## Input Format

The first line contains two integersÂ nÂ andÂ k, the number of chapters and the maximum number of problems per page.
The second line containsÂ nÂ space-separated integersÂ arr[i]Â whereÂ arr[i]Â denotes the number of problems in theÂ ithÂ chapter.

## Constraints

• 1 <= n, k, arr[i] <= 100

Sample Input

``````STDIN       Function
-----       --------
5 3         n = 5, k = 3
4 2 6 1 10  arr = [4, 2, 6, 1, 10]``````

Sample Output

``4``

Explanation

The diagram below depicts Lisa’s workbook withÂ n = 5Â chapters and a maximum ofÂ k = 3Â problems per page. Special problems are outlined in red, and page numbers are in yellow squares.

There areÂ 4Â special problems and thus we print the numberÂ 4Â on a new line.

## Solution – Lisa’s Workshop – HackerRank Solution

### C++

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n, k; // n = # of chapters, k = maximum number of problems per page
cin>>n>>k;
int numProblems, specialProblems = 0, pageNumber = 0;
for(int i = 0; i < n; i++) {
cin>>numProblems;
pageNumber++; // increase for new chapter
int problem = 1;
while(numProblems > 0) {
numProblems--;
if(problem == pageNumber) {
specialProblems++;
}
if(problem%k == 0 && numProblems != 0) {
pageNumber++; // increase for full page
}
problem++;
}
}
cout<<specialProblems;
return 0;
}```

### Python

```import sys
from math import ceil

def workbook(n, k, arr):
res = 0
page = 1

for el in arr:
pr_cur = 1
for pr in range(pr_cur, pr_cur + el):
if pr == page + pr//k - (1 if pr%k == 0 else 0):
res += 1
#print("problem = {} page = {} page% = {} res = {}".format(pr, page + pr//k - (1 if pr%k == 0 else 0), pr%k, res))

#print()

if el%k == 0:
page += el//k
else:
page += 1 + el//k
#print("page at end: {}".format(page))

return res

if __name__ == "__main__":
n, k = input().strip().split(' ')
n, k = [int(n), int(k)]
arr = list(map(int, input().strip().split(' ')))
result = workbook(n, k, arr)
print(result)
```

### Java

```import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int max = in.nextInt();
int[] arr = new int[num];
int numPages = 0;
int curPage = 1;
int count = 0;
for(int i =0;i<num;i++){
arr[i] = in.nextInt();
for(int prob= 1;prob<=arr[i];prob++){
int whatPage = prob/max;
if(prob == curPage) {
count++;
//System.out.println("HI chap"+(i+1)+" prob "+prob);
}
if(prob%max==0 && prob!=arr[i]){
curPage++;
}
}
//System.out.println("END CHA"+curPage);
curPage++;
}
System.out.println(count);
//System.out.println(Arrays.toString(arr)+count);
}
}
```

Note: This problem (Lisa’s Workshop) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.