HackerRank Lisa Workbook Solution

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 straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Lisa Workbook Solution
HackerRank Lisa Workbook Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Lisa Workbook

Task

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()
ts = 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
  read(n,k);
  tr:=0;

  for i:=1 to n do
   begin
    read(x);
    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 Chase2learn. This tutorial is only for Educational and Learning purposes.

FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi


Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment