VOOZH about

URL: https://www.geeksforgeeks.org/dsa/time-crunch-challenge/

⇱ Time Crunch Challenge - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Time Crunch Challenge

Last Updated : 23 Jul, 2025

Geeks for Geeks is organizing a hackathon consisting of N sections, each containing K questions. The duration of the hackathon is H hours.

Each participant can determine their speed of solving questions, denoted as S (S = questions-per-hour). During each hour, a participant can choose a section and solve S problems. If a section has fewer than S questions, the participant will not solve any additional questions from that section during that hour.

The goal is for the participant to solve the questions at a slower pace while still completing all the questions before the hackathon ends, the task is to find the S such that ensures the participant will solve all the questions within H hours.

Examples:

Input: section = [ 2, 4, 2, 4, 5], H = 8
Output: 3
Explanation: Participate will solve 3 questions per hour to solve all the questions within 8 hours.

Input: section = [ 8, 11, 18, 20], H = 10
Output: 7
Explanation: Participate will solve 7 questions per hour to solve all the questions within 10 hours.

Naïve Approach: The basic way to solve the problem is as follows:

  • Check if H is equal to the number of sections.
  • If true, return the maximum time needed for any section.
  • Calculate the minimum possible solving speed (k_min) by dividing the total time required to solve all sections by H.
  • Calculate the maximum possible solving speed (k_max) by finding the maximum time needed for any section using max_element.
  • Iterate through possible solving speeds from k_min to k_max.
  • Calculate the total hours required to solve all sections at each speed.
  • If total hours are equal H, return the current speed k.
  • If no valid solution is found, return -1.

Below is the implementation of the above approach:


Output
Minimum eating speed required: 7

Time Complexity: O(n (max K −min K))
Auxiliary Space: O(1)

Efficient Approach: To solve the problem follow the below idea:

We can solve this problem using Binary Search

Follow the below steps to solve the problem:

  • Initialize the left pointer to 1 and the right pointer to the maximum value in the section vector.
  • While the left pointer is less than the right pointer, repeat steps 3-5.
  • Calculate the midpoint as the average of the left and right pointers.
  • Check if the participant can solve all the questions within h hours at the current midpoint speed using the canSolveAll function. If true, update the right pointer to the midpoint. Otherwise, update the left pointer to midpoint + 1.
  • Repeat steps 3-4 until the left pointer becomes equal to the right pointer.
  • Return the left pointer as the minimum solving speed required to solve all the questions within H hours.
  • we get then our final result.

Below is the implementation of the above approach:


Output
7

Time Complexity: O(n.logn)
Auxiliary Space: O(1)

Comment
Article Tags: