# HackerRank Lisa Workbook Solution

#### ByBrokenprogrammers

Dec 13, 2022

Hello Programmers, In this post, you will learn how to solve HackerRank Lisa Workbook Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## HackerRank Lisa Workbook Solution

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 spaceseparated integers arr[i] where arr[i] denotes the number of problems in the ith chapter.

Constraints

• 1 <= nkarr[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 Lisas 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.

## HackerRank Lisa Workbook Solution

### Lisa Workbook Solution in C

```#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int chapters, probs; scanf("%i %i", &chapters, &probs);
int *ar = malloc(sizeof(int)*chapters);
for (int i=0; i<chapters; i++) scanf("%i", &ar[i]);

int curChapter = 1, curPage = 1, count = 0;
while (curChapter <= chapters) {

for (int i=1; i<=ar[curChapter-1]; i++) {
if (i == curPage)
count++;

if (i+1 <= ar[curChapter-1] && i % probs == 0)
curPage++;
}

curPage++;
curChapter++;
}

printf("%i", count);
return 0;
}```

### Lisa Workbook Solution in Cpp

```#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int N = 200005;
int n , K , t[N];
int main() {
scanf("%d%d" , &n , &K);
int res = 0;
int now = 1;
for (int i = 1 ; i <= n ; ++ i) {
int x = 0;
scanf("%d" , &t[i]);
for (int j = 1 ; j <= t[i] ; ++ j) {
if (j == now)
++ res;
++ x;
if (x == K) {
x = 0;
now ++;
}
}
if (x) now++;
}
cout << res << endl;
return 0;
}```

### Lisa Workbook Solution in Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int special = 0;
int p = 1;
for(int i=0;i<n;i++){
int t = sc.nextInt();
int c = 0;
for(int h=1;h<=t;h++){
c++;
if(p == h)
special++;
if(c == k) {
c = 0;
p++;
}
}
if(c!=0){
p++;
}
}
System.out.println(special);
}
}```

### Lisa Workbook Solution in Python

```def readInts():
return map(int, raw_input().strip().split(' '))
n, k = readInts()
# n = 5
# k = 3
# ts = [4, 2, 6, 1, 10]
ans = 0
pageNum = 1
for chapter, problems in enumerate(ts):
pageLeft = k
for problemId in xrange(1, problems+1):
if pageNum == problemId:
ans += 1
pageLeft -= 1
if pageLeft == 0:
pageLeft = k
pageNum += 1
if pageLeft < k:
pageNum += 1
print ans```

### Lisa Workbook Solution using JavaScript

```function processData(input) {
//Enter your code here
var data = input.split('\n');
var ch = data[0].split(' ')[0];
var pageLimit = data[0].split(' ')[1];
var problems = data[1].split(' ');
var currPage = 1;
var specials = 0;

problems.forEach(function(chProblem) {
var i;
var pageProblem = 0;
for (i = 1; i <= chProblem; i++) {
pageProblem++;
if (i === currPage)
{
specials++;
}
if (pageProblem >= pageLimit) {
pageProblem = 0;
currPage++;
}
}
if (pageProblem > 0) {
currPage++;
}
});

console.log(specials);

}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});```

### Lisa Workbook Solution in Scala

```object Solution {
def main(args: Array[String]) {
val sc = new java.util.Scanner (System.in);
val Array(n,k) = sc.nextLine().split(" ").map(_.toInt)
val caps = sc.nextLine().split(" ").map(_.toInt)
println(solve(n, k, caps))
}
def solve(n:Int, k:Int, caps:Array[Int]): Int = {
var pagina = 1
var total = 0
for(c <- caps) {
for(p <- 1 to c) {
if(pagina == p) {
total += 1
}
if(p % k == 0) {
pagina += 1
}
}
if(c % k != 0) {
pagina += 1
}
}
total
}
}```

### Lisa Workbook Solution in Pascal

```(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
uses math;
var        n,k,x,tr,j,ans,i,sum:longint;
begin
tr:=0;
for i:=1 to n do
begin
if (k=1) and (i=1) then sum:=x;
for j:=1 to x do
begin
if (j mod k=1) then inc(tr);
if j=tr then  inc(ans);
end;
end;
if k=1 then writeln(sum) else
writeln(ans);
end.```

Disclaimer: This problem (Lisa Workbook) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.