0
0
DSA Goprogramming~20 mins

Binary Search on Answer Technique in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Search on Answer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Binary Search on Answer for Minimum Maximum Subarray Sum
What is the output of the following Go code that uses binary search on answer to find the minimum largest sum when splitting an array into 2 subarrays?
DSA Go
package main
import "fmt"
func canSplit(nums []int, maxSum int, m int) bool {
    count, currentSum := 1, 0
    for _, num := range nums {
        if currentSum+num > maxSum {
            count++
            currentSum = num
            if count > m {
                return false
            }
        } else {
            currentSum += num
        }
    }
    return true
}
func splitArray(nums []int, m int) int {
    left, right := 0, 0
    for _, num := range nums {
        if num > left {
            left = num
        }
        right += num
    }
    for left < right {
        mid := (left + right) / 2
        if canSplit(nums, mid, m) {
            right = mid
        } else {
            left = mid + 1
        }
    }
    return left
}
func main() {
    nums := []int{7, 2, 5, 10, 8}
    m := 2
    fmt.Println(splitArray(nums, m))
}
A13
B15
C20
D18
Attempts:
2 left
💡 Hint
Think about how binary search narrows down the maximum allowed subarray sum to split into m parts.
🧠 Conceptual
intermediate
1:30remaining
Understanding Binary Search on Answer Technique
Which statement best describes the binary search on answer technique?
AIt uses binary search on a range of possible answers to find the optimal solution.
BIt sorts the array first and then applies binary search to find an element.
CIt applies binary search on the input array elements directly to find a target value.
DIt uses binary search to find the median of the array.
Attempts:
2 left
💡 Hint
Think about searching over possible answers, not array elements.
🔧 Debug
advanced
2:00remaining
Identify the error in binary search on answer implementation
What error does the following Go code produce when run? It tries to find the minimum maximum subarray sum for splitting into m parts.
DSA Go
package main
import "fmt"
func canSplit(nums []int, maxSum int, m int) bool {
    count, currentSum := 1, 0
    for _, num := range nums {
        if currentSum+num >= maxSum {
            count++
            currentSum = num
            if count > m {
                return false
            }
        } else {
            currentSum += num
        }
    }
    return true
}
func splitArray(nums []int, m int) int {
    left, right := 0, 0
    for _, num := range nums {
        if num > left {
            left = num
        }
        right += num
    }
    for left < right {
        mid := (left + right) / 2
        if canSplit(nums, mid, m) {
            right = mid
        } else {
            left = mid + 1
        }
    }
    return left
}
func main() {
    nums := []int{7, 2, 5, 10, 8}
    m := 2
    fmt.Println(splitArray(nums, m))
}
AThe code causes a runtime panic due to index out of range.
BThe code outputs 18, which is the correct answer.
CThe code outputs 19, which is incorrect for the problem.
DThe code causes an infinite loop due to incorrect condition in canSplit.
Attempts:
2 left
💡 Hint
Check the condition that decides when to split the subarray.
🚀 Application
advanced
2:30remaining
Using Binary Search on Answer to Minimize Maximum Distance
You have positions of gas stations on a highway as [1, 2, 8, 12, 17]. You want to add 2 more stations to minimize the maximum distance between adjacent stations. What is the minimized maximum distance after adding 2 stations?
A5
B4
C3
D6
Attempts:
2 left
💡 Hint
Use binary search on the distance and check feasibility by counting required stations.
Predict Output
expert
3:00remaining
Output of Binary Search on Answer for Painter Partition Problem
What is the output of this Go code that uses binary search on answer to find the minimum time to paint all boards by 3 painters?
DSA Go
package main
import "fmt"
func isPossible(boards []int, maxTime int, painters int) bool {
    total, count := 0, 1
    for _, time := range boards {
        if time > maxTime {
            return false
        }
        if total+time > maxTime {
            count++
            total = time
            if count > painters {
                return false
            }
        } else {
            total += time
        }
    }
    return true
}
func minTime(boards []int, painters int) int {
    left, right := 0, 0
    for _, time := range boards {
        if time > left {
            left = time
        }
        right += time
    }
    for left < right {
        mid := (left + right) / 2
        if isPossible(boards, mid, painters) {
            right = mid
        } else {
            left = mid + 1
        }
    }
    return left
}
func main() {
    boards := []int{10, 20, 30, 40}
    painters := 3
    fmt.Println(minTime(boards, painters))
}
A40
B50
C60
D30
Attempts:
2 left
💡 Hint
Think about how painters can split boards to minimize maximum painting time.