• + 0 comments

    Typescript / Javascript solution:

    function workbook(n: number, k: number, arr: number[]): number {
        let usedPages = 0;
        let specialCount = 0;
        for (let i = 0; i < arr.length; i++) {
            const numOfQuestions = arr[i];
            const pagesNeeded = Math.ceil(numOfQuestions / k);
            for (let j = 0; j < pagesNeeded; j++) {
                 const pageNum = usedPages + j + 1;
                 const startIndex = j * k + 1;
                 const endIndex = Math.min(startIndex + k - 1, numOfQuestions);
                 if (pageNum >= startIndex && pageNum <= endIndex) {
                    specialCount++;
                 }
            } 
            usedPages += pagesNeeded;
        }
        return specialCount;
    }