We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Instead of iterating over each problem, we can check if current_page falls within the range of problem_number to last_problem_on_page.
This approach reduces the time complexity for each page from O(k) to O(1) , where k is the maximum number of problems per page. Because it performs a single comparison per page rather than multiple comparisons for each problem on the page.
defwork_book(n,k,arr):special_problems=0current_page=1forchapterinrange(n):total_problems=arr[chapter]problem_number=1whileproblem_number<=total_problems:# Calculate the last problem on the current pagelast_problem_on_page=min(problem_number+k-1,total_problems)# Check if the current page contains a special problemifproblem_number<=current_page<=last_problem_on_page:special_problems+=1# Move to the next page and update the problem numbercurrent_page+=1problem_number=last_problem_on_page+1returnspecial_problems
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lisa's Workbook
You are viewing a single comment's thread. Return to all comments →
Instead of iterating over each problem, we can check if current_page falls within the range of problem_number to last_problem_on_page.
This approach reduces the time complexity for each page from O(k) to O(1) , where k is the maximum number of problems per page. Because it performs a single comparison per page rather than multiple comparisons for each problem on the page.