• + 0 comments

    In C#

        public static int workbook(int n, int k, List<int> arr)
        {
            int page = 1;
            int chProb = 0;
            int special = 0;
            
            foreach(int i in arr)
            {
                int problems = i; // cant change iterated values so assing to new var
                
                while(problems > 0)
                {
                    // get problems in page, the min of k and left over problems
                    int pgProbs = Math.Min(k,problems);
                    // Add problems from the page to the chapter problems
                    chProb += pgProbs;
                    // Check if the problems worked are special
                    if(page >= (chProb - pgProbs + 1) && page <= chProb)
                        special++; // Found a special problem
                    // Subtrack the problems worked from the problems in the chapter
                    problems-=pgProbs;
                    // Next page
                    page++;
                }
                
                // Reset chapter problems
                chProb = 0;
            }
            
            // return special count
            return special;
        }