Challenge - 5 Problems
Binary Search Allocation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Minimum Pages Allocation Function
What is the output of the following C# code that uses binary search to allocate minimum pages to students?
DSA C#
using System; class Program { static bool IsPossible(int[] arr, int n, int m, int mid) { int studentCount = 1; int pageSum = 0; for (int i = 0; i < n; i++) { if (arr[i] > mid) return false; if (pageSum + arr[i] > mid) { studentCount++; pageSum = arr[i]; if (studentCount > m) return false; } else { pageSum += arr[i]; } } return true; } static int AllocateMinimumPages(int[] arr, int n, int m) { int start = 0; int end = 0; int result = -1; for (int i = 0; i < n; i++) end += arr[i]; while (start <= end) { int mid = (start + end) / 2; if (IsPossible(arr, n, m, mid)) { result = mid; end = mid - 1; } else { start = mid + 1; } } return result; } static void Main() { int[] pages = {12, 34, 67, 90}; int students = 2; Console.WriteLine(AllocateMinimumPages(pages, pages.Length, students)); } }
Attempts:
2 left
💡 Hint
Think about how binary search narrows down the maximum pages allocated to a student.
✗ Incorrect
The function tries to minimize the maximum number of pages assigned to a student. The minimum feasible maximum is 113 for the given input.
🧠 Conceptual
intermediateUnderstanding the Role of 'IsPossible' Function
In the Allocate Minimum Pages problem, what is the main purpose of the 'IsPossible' function?
Attempts:
2 left
💡 Hint
Consider what the function returns true or false for.
✗ Incorrect
'IsPossible' checks if it is feasible to allocate pages such that no student gets more than 'mid' pages and all students are assigned.
🔧 Debug
advancedIdentify the Bug in Allocation Code
What error will occur when running this code snippet for Allocate Minimum Pages problem?
DSA C#
static bool IsPossible(int[] arr, int n, int m, int mid) { int studentCount = 1; int pageSum = 0; for (int i = 0; i < n; i++) { if (arr[i] > mid) return false; if (pageSum + arr[i] > mid) { studentCount++; pageSum = arr[i]; if (studentCount > m) return false; } else { pageSum += arr[i]; } } return true; }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop runs from i = 0 to i <= n, which causes an index out of range when i equals n.
🚀 Application
advancedMinimum Pages Allocation for Custom Input
Given the array of pages [10, 20, 30, 40, 50] and 3 students, what is the minimum maximum number of pages allocated to a student?
Attempts:
2 left
💡 Hint
Try to split the array into 3 parts minimizing the maximum sum.
✗ Incorrect
The optimal allocation is [10,20,30], [40], [50] with maximum pages 60.
❓ Predict Output
expertOutput of Modified Allocation with Single Student
What will be the output of the AllocateMinimumPages function when given pages = [5, 10, 15, 20] and students = 1?
DSA C#
static int AllocateMinimumPages(int[] arr, int n, int m) { int start = 0; int end = 0; int result = -1; for (int i = 0; i < n; i++) end += arr[i]; while (start <= end) { int mid = (start + end) / 2; if (IsPossible(arr, n, m, mid)) { result = mid; end = mid - 1; } else { start = mid + 1; } } return result; } static bool IsPossible(int[] arr, int n, int m, int mid) { int studentCount = 1; int pageSum = 0; for (int i = 0; i < n; i++) { if (arr[i] > mid) return false; if (pageSum + arr[i] > mid) { studentCount++; pageSum = arr[i]; if (studentCount > m) return false; } else { pageSum += arr[i]; } } return true; } static void Main() { int[] pages = {5, 10, 15, 20}; int students = 1; Console.WriteLine(AllocateMinimumPages(pages, pages.Length, students)); }
Attempts:
2 left
💡 Hint
With only one student, all pages must be allocated to that student.
✗ Incorrect
Since there is only one student, the maximum pages allocated is the sum of all pages: 5+10+15+20=50.
