0
0
DSA Goprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
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
What is the output of the following Go code that uses binary search to allocate minimum pages to 2 students for 4 books?
DSA Go
package main
import "fmt"
func isPossible(books []int, students int, mid int) bool {
    count := 1
    sum := 0
    for _, pages := range books {
        if pages > mid {
            return false
        }
        if sum+pages > mid {
            count++
            sum = pages
            if count > students {
                return false
            }
        } else {
            sum += pages
        }
    }
    return true
}
func allocateMinimumPages(books []int, students int) int {
    start, end := 0, 0
    for _, pages := range books {
        if pages > start {
            start = pages
        }
        end += pages
    }
    result := end
    for start <= end {
        mid := (start + end) / 2
        if isPossible(books, students, mid) {
            result = mid
            end = mid - 1
        } else {
            start = mid + 1
        }
    }
    return result
}
func main() {
    books := []int{10, 20, 30, 40}
    students := 2
    fmt.Println(allocateMinimumPages(books, students))
}
A70
B50
C60
D40
Attempts:
2 left
💡 Hint
Think about dividing books so that the maximum pages assigned to any student is minimized.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Role of Binary Search in Minimum Pages Allocation
Why do we use binary search on the answer space instead of the input array when solving the Allocate Minimum Pages problem?
ABecause the answer lies between the maximum single book pages and the sum of all pages, which forms a sorted range to binary search.
BBecause binary search is faster on arrays than on numbers.
CBecause the books array is always sorted, so binary search applies directly on it.
DBecause binary search helps to sort the books before allocation.
Attempts:
2 left
💡 Hint
Think about the range of possible answers and how binary search can narrow it down.
🔧 Debug
advanced
2:00remaining
Identify the Bug in the Allocation Feasibility Check
What error will occur when running this feasibility check function for allocating pages?
DSA Go
func isPossible(books []int, students int, mid int) bool {
    count := 1
    sum := 0
    for _, pages := range books {
        if pages >= mid {
            return false
        }
        if sum+pages > mid {
            count++
            sum = pages
            if count > students {
                return false
            }
        } else {
            sum += pages
        }
    }
    return true
}
AIt always returns true regardless of input.
BIt returns false incorrectly when a book has pages equal to mid, causing wrong allocation.
CIt causes an infinite loop.
DIt causes a runtime panic due to uninitialized variables.
Attempts:
2 left
💡 Hint
Check the condition that compares pages with mid carefully.
Predict Output
advanced
2:00remaining
Output of Allocation with 3 Students and 5 Books
What is the output of the following Go code that allocates minimum pages to 3 students for 5 books?
DSA Go
package main
import "fmt"
func isPossible(books []int, students int, mid int) bool {
    count := 1
    sum := 0
    for _, pages := range books {
        if pages > mid {
            return false
        }
        if sum+pages > mid {
            count++
            sum = pages
            if count > students {
                return false
            }
        } else {
            sum += pages
        }
    }
    return true
}
func allocateMinimumPages(books []int, students int) int {
    start, end := 0, 0
    for _, pages := range books {
        if pages > start {
            start = pages
        }
        end += pages
    }
    result := end
    for start <= end {
        mid := (start + end) / 2
        if isPossible(books, students, mid) {
            result = mid
            end = mid - 1
        } else {
            start = mid + 1
        }
    }
    return result
}
func main() {
    books := []int{12, 34, 67, 90, 45}
    students := 3
    fmt.Println(allocateMinimumPages(books, students))
}
A113
B101
C90
D67
Attempts:
2 left
💡 Hint
Try dividing books into 3 parts minimizing the maximum pages assigned.
🚀 Application
expert
3:00remaining
Minimum Pages Allocation for Large Input with Constraints
Given 7 books with pages [15, 17, 20, 25, 30, 35, 40] and 4 students, what is the minimum maximum pages allocated to any student?
A70
B60
C65
D55
Attempts:
2 left
💡 Hint
Use binary search between max single book pages and sum of all pages to find minimal max allocation.