Lisa’s Workbook HackerRank Solution in C, C++, Java, Python

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?

For example, 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 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. It should return an integer that represents the number of special problems in the workbook.

workbook has the following parameter(s):

n: an integer that denotes the number of chapters
k: an integer that denotes the maximum number of problems per page
arr: an array of integers that denote the number of problems in each chapter

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.

Output Format

Print the number of special problems in Lisa’s workbook.

Sample Input

5 3 
4 2 6 1 10

Sample Output

4

Lisa’s Workbook HackerRank 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’s Workbook HackerRank Solution in C++

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

int n,k,j,x,ans,page,i;

int main() {
    page = 1;
    
    scanf("%d%d",&n,&k);
    for (i=1 ; i<=n ; i++) {
        scanf("%d",&x);
        for (j=1 ; j<=x ; j++) {
            if (page == j) ans++;
            if (j % k == 0) page++;
        }
        if (x % k != 0) page++;
        //printf("%d\n",page);
    }
    printf("%d\n",ans);
    return 0;
}

 

Lisa’s Workbook HackerRank 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’s Workbook HackerRank 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’s Workbook HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        string[] line = Console.ReadLine().Split(' ');
        int N = Int32.Parse(line[0]);
        int K = Int32.Parse(line[1]);
        line = Console.ReadLine().Split(' ');
        int page = 1;
        int cnt = 0;
        for(int i=0; i<N; i++) {
            int Ps= Int32.Parse(line[i]);
            for(int j=1; j<=Ps; j+=K) {
                if(page >= j && page <= Math.Min(Ps,j+K-1)) {
                    cnt++;
                }
                page++;
            }
        }
        Console.WriteLine(cnt);
    }
}

 

Attempt Lisa’s Workbook HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/lisa-workbook/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/flatland-space-stations-hackerrank-solution/

 

Leave a Comment