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.

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 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.

Leave a Comment

Your email address will not be published. Required fields are marked *