Skip to content

Instantly share code, notes, and snippets.

@Shaddyjr
Created November 29, 2021 07:22
Show Gist options
  • Save Shaddyjr/b759ea36701323f1e1cf6a864e5fb33a to your computer and use it in GitHub Desktop.
Save Shaddyjr/b759ea36701323f1e1cf6a864e5fb33a to your computer and use it in GitHub Desktop.
# source: https://www.hackerrank.com/challenges/lisa-workbook/problem
# video: https://youtu.be/G2_o-FqdCTs
def workbook(n, k, arr):
count = 0
current_page = 1 # NOTE: we're indexing starting at 1
for question_count in arr: # O(n)
pages_added, remaining_questions = divmod(question_count, k)
for new_chapter_page in range(1, pages_added + 1): # O(pmodk) => O(100mod1) => O(100) => O(1)
first_question_on_page = (new_chapter_page - 1) * k + 1
last_question_on_page = first_question_on_page + (k - 1)
# check for "special" question => page index will be within question range
count += first_question_on_page <= current_page <= last_question_on_page
current_page += 1
# handle remainder questions
if remaining_questions: # will add another page
first_question_on_page = pages_added * k + 1
last_question_on_page = first_question_on_page + (remaining_questions - 1)
# check for "special" question => page index will be within question range
count += first_question_on_page <= current_page <= last_question_on_page
current_page += 1
return count # Total Time Complexity: O(n) * O(pmodk) => O(n)
@Yash08official
Copy link

Hii sir , it does not give the output

@Shaddyjr
Copy link
Author

Hii sir , it does not give the output

Hmm, I just tried it now and it's still working on my end.
image

Did you select "Python 3" for the language? Perhaps something wasn't copied over completely?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment