In this HackerRank Lisa's Workbook problem You will be given the details for Lisa's workbook, can you count its number of special problems?

HackerRank Lisa's Workbook problem solution


Problem solution in Python programming.

if __name__ == '__main__':
    import sys
    n, k = map(int, sys.stdin.readline().strip().split())
    t = list(map(int, sys.stdin.readline().strip().split()))

    page = 0
    n_special = 0
    # Iterate over chapter
    for i in range(n):
        #print("chapter{}".format(i + 1))
        page += 1
        #print("page{}".format(page))
        # Iterate over problems in chapter
        for j in range(1, t[i] + 1):
            #print("\tproblem{}".format(j))
            if j == page:
                n_special += 1
            if j != 0 and j % k == 0 and j < t[i]:
                page += 1
                #print("page{}".format(page))

    #print("---")
    print(n_special)


Problem solution in Java Programming.

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);
    }
}


Problem solution in C++ programming.

#define _USE_MATH_DEFINES
#include <algorithm>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int v, w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
	int N, K; cin >> N >> K;
	vector<int> a(N);
	for (int i = 0; i < N; i++)
		cin >> a[i];
	int x = 1, cnt = 0;
	for (int i = 0; i < N; i++) {
		int y = 0;
		for (int j = 1; j <= a[i]; j++) {
			if (j == x) cnt++;
			y++;
			if (y == K) {
				y = 0;
				x++;
			}
		}
		if (y) x++;
	}
	cout << cnt << endl;
}


Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

struct prob{
    int problem_no;
    int page_no;
};

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n, k, page_number=1, counter=0;
    scanf("%d %d", &n, &k);
    int chapter[n];
    for(int i=1;i<=n;i++){
        scanf("%d", &chapter[i]);
        struct prob problem[chapter[i]];
        for(int j=1;j<=chapter[i];j++){
            problem[j].problem_no=j;
            problem[j].page_no=page_number;
            if((j%k==0) && (j!=chapter[i]))
                page_number++;
        }
        page_number++;
        for(int j=1;j<=chapter[i];j++){
            if(problem[j].problem_no==problem[j].page_no)
                counter++;
        }
    }
    printf("%d", counter);
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    
    var top = input.split('\n')[0];
    var bot = input.split('\n')[1];
    
    var chapcount = top.split(' ')[0];
    var maxq = top.split(' ')[1];
    var chaps = bot.split(' ');
    var pageoffset = 1;
    var speccount = 0;
    var specmem = [];
    for(var a = 0; a < chaps.length; a++){
        var chapqs = chaps[a];
        var pagecount = Math.ceil(chapqs/maxq);
        var qs = [];
        for(var q = 1; q <= chapqs; q++){qs.unshift(q); }
        //for(var q = 1; q <= chapqs; q++){qs.push(q); }
        
        for(var p = pageoffset; p < pagecount + pageoffset; p++){
           specmem.push([]);
           //specmem[p-1].push(qs.slice());
            
            
           if(qs.length >=maxq){
               for(var s = 1; s <= maxq; s++){
                    specmem[p-1].push(qs.pop());
               }
           }
           else{
               //console.log(p+"LENGTH"+qs.length);
               var qslength = qs.length;
                for(var s = 0; s < qslength; s++){
                    //console.log(s+":"+p+":"+qs.pop())
                    specmem[p-1].push(qs.pop());
               }
            }
        }
     
        pageoffset = pageoffset+ pagecount;
    }
    
    for(var a = 0; a < specmem.length; a++){
        var mem = specmem[a];
        //console.log((a+1)+":"+mem);
        for(var b = 0; b < mem.length; b++){
            
            if(a+1 == mem[b])speccount++;
        }
    }
    console.log(speccount);
} 

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

process.stdin.on("end", function () {
   processData(_input);
});