0
0
DSA C++programming~20 mins

Allocate Minimum Pages Binary Search on Answer in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Allocate Minimum Pages Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Minimum Pages Allocation for Given Books and Students
Given the array of book pages and number of students, what is the minimum maximum number of pages allocated to a student?
DSA C++
int 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;
}

int findPages(int arr[], int n, int m) {
    int sum = 0;
    for (int i = 0; i < n; i++) sum += arr[i];
    int start = 0, end = sum, result = -1;
    while (start <= end) {
        int mid = start + (end - start) / 2;
        if (isPossible(arr, n, m, mid)) {
            result = mid;
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
    return result;
}

#include <iostream>
int main() {
    int arr[] = {12, 34, 67, 90};
    int n = 4;
    int m = 2;
    std::cout << findPages(arr, n, m) << std::endl;
    return 0;
}
A113
B157
C90
D67
Attempts:
2 left
💡 Hint
Think about dividing books so that the maximum pages assigned to any student is minimized.
Predict Output
intermediate
1:30remaining
Output for Single Student Allocation
What is the output when there is only one student for the given books?
DSA C++
int 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;
}

int findPages(int arr[], int n, int m) {
    int sum = 0;
    for (int i = 0; i < n; i++) sum += arr[i];
    int start = 0, end = sum, result = -1;
    while (start <= end) {
        int mid = start + (end - start) / 2;
        if (isPossible(arr, n, m, mid)) {
            result = mid;
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
    return result;
}

#include <iostream>
int main() {
    int arr[] = {10, 20, 30, 40};
    int n = 4;
    int m = 1;
    std::cout << findPages(arr, n, m) << std::endl;
    return 0;
}
A40
B30
C10
D100
Attempts:
2 left
💡 Hint
If only one student is there, he must read all books.
🔧 Debug
advanced
2:00remaining
Identify the Error in Allocation Check Function
What error will this code produce when checking if allocation is possible?
DSA C++
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;
}
AReturns false incorrectly when arr[i] == mid
BReturns true incorrectly when arr[i] > mid
CCauses runtime error due to uninitialized variable
DNo error, works correctly
Attempts:
2 left
💡 Hint
Check the condition that compares arr[i] and mid carefully.
🧠 Conceptual
advanced
1:30remaining
Why Use Binary Search in Allocate Minimum Pages Problem?
Why is binary search used on the answer space instead of the input array in the Allocate Minimum Pages problem?
ABecause the input array is unsorted and cannot be searched directly
BBecause binary search is faster on arrays than on numbers
CBecause the answer lies between the max single book pages and sum of all pages, which forms a sorted search space
DBecause binary search can only be applied to the input array
Attempts:
2 left
💡 Hint
Think about the range of possible answers and how binary search helps narrow it down.
Predict Output
expert
2:30remaining
Output for Complex Allocation Scenario
What is the output of the minimum maximum pages allocated for the following input?
DSA C++
int 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;
}

int findPages(int arr[], int n, int m) {
    int sum = 0;
    for (int i = 0; i < n; i++) sum += arr[i];
    int start = 0, end = sum, result = -1;
    while (start <= end) {
        int mid = start + (end - start) / 2;
        if (isPossible(arr, n, m, mid)) {
            result = mid;
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
    return result;
}

#include <iostream>
int main() {
    int arr[] = {10, 20, 30, 40, 50, 60, 70, 80};
    int n = 8;
    int m = 3;
    std::cout << findPages(arr, n, m) << std::endl;
    return 0;
}
A210
B150
C170
D120
Attempts:
2 left
💡 Hint
Try to split books into 3 students so that the maximum pages assigned is minimized.