0
0
DSA C++programming~20 mins

Binary Search on Answer Technique in DSA C++ - 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 C++ code that uses binary search on answer to find the minimum largest sum of subarrays?
DSA C++
#include <iostream>
#include <vector>
using namespace std;

bool canSplit(const vector<int>& nums, int m, int maxSum) {
    int currentSum = 0, count = 1;
    for (int num : nums) {
        if (num > maxSum) return false;
        if (currentSum + num > maxSum) {
            count++;
            currentSum = num;
            if (count > m) return false;
        } else {
            currentSum += num;
        }
    }
    return true;
}

int splitArray(vector<int>& nums, int m) {
    int left = 0, right = 0;
    for (int num : nums) right += num;
    int result = right;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (canSplit(nums, m, mid)) {
            result = mid;
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return result;
}

int main() {
    vector<int> nums = {7, 2, 5, 10, 8};
    int m = 2;
    cout << splitArray(nums, m) << endl;
    return 0;
}
A18
B15
C20
D25
Attempts:
2 left
💡 Hint
Think about how the array can be split into 2 subarrays with minimum largest sum.
🧠 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 the input array elements directly to find a target value.
BIt applies binary search on a range of possible answers to find the optimal solution.
CIt sorts the array first and then applies binary search to find an element.
DIt uses binary search to split the array into equal halves repeatedly.
Attempts:
2 left
💡 Hint
Think about searching over a range of values, not array indices.
Predict Output
advanced
2:00remaining
Output of Binary Search on Answer for Minimum Time to Complete Jobs
What is the output of this C++ code that uses binary search on answer to find the minimum time to finish jobs with given workers?
DSA C++
#include <iostream>
#include <vector>
using namespace std;

bool canFinish(const vector<int>& jobs, int workers, int maxTime) {
    int count = 1, currentTime = 0;
    for (int job : jobs) {
        if (job > maxTime) return false;
        if (currentTime + job > maxTime) {
            count++;
            currentTime = job;
            if (count > workers) return false;
        } else {
            currentTime += job;
        }
    }
    return true;
}

int minTime(vector<int>& jobs, int workers) {
    int left = 0, right = 0;
    for (int job : jobs) right += job;
    int result = right;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (canFinish(jobs, workers, mid)) {
            result = mid;
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return result;
}

int main() {
    vector<int> jobs = {3, 2, 3};
    int workers = 3;
    cout << minTime(jobs, workers) << endl;
    return 0;
}
A3
B4
C5
D6
Attempts:
2 left
💡 Hint
Each worker can do one job, so minimum time is the longest single job.
🔧 Debug
advanced
2:00remaining
Identify the error in binary search on answer implementation
What error does the following code produce when run?
DSA C++
#include <iostream>
#include <vector>
using namespace std;

bool canSplit(const vector<int>& nums, int m, int maxSum) {
    int currentSum = 0, count = 1;
    for (int num : nums) {
        if (num > maxSum) return false;
        if (currentSum + num >= maxSum) {
            count++;
            currentSum = num;
            if (count > m) return false;
        } else {
            currentSum += num;
        }
    }
    return true;
}

int splitArray(vector<int>& nums, int m) {
    int left = 0, right = 0;
    for (int num : nums) right += num;
    int result = right;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (canSplit(nums, m, mid)) {
            result = mid;
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return result;
}

int main() {
    vector<int> nums = {7, 2, 5, 10, 8};
    int m = 2;
    cout << splitArray(nums, m) << endl;
    return 0;
}
AInfinite loop
BOutputs 18
COutputs 17
DOutputs 20
Attempts:
2 left
💡 Hint
Check the condition that decides when to split the subarray.
🚀 Application
expert
2:30remaining
Minimum Largest Distance to Gas Station
Given an array of gas station positions and an integer k representing additional gas stations to add, what is the minimum possible maximum distance between adjacent gas stations after adding k stations? Consider the code below. What is the output?
DSA C++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

bool canPlace(const vector<int>& stations, int k, double maxDist) {
    int count = 0;
    for (int i = 1; i < stations.size(); i++) {
        double dist = stations[i] - stations[i - 1];
        count += (int)(dist / maxDist);
        if (count > k) return false;
    }
    return true;
}

double minmaxGasDist(vector<int>& stations, int k) {
    double left = 0, right = stations.back() - stations.front();
    for (int i = 0; i < 100; i++) {
        double mid = (left + right) / 2;
        if (canPlace(stations, k, mid)) {
            right = mid;
        } else {
            left = mid;
        }
    }
    return right;
}

int main() {
    vector<int> stations = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int k = 9;
    cout.precision(6);
    cout << fixed << minmaxGasDist(stations, k) << endl;
    return 0;
}
A1.000000
B0.750000
C0.900000
D0.500000
Attempts:
2 left
💡 Hint
Adding 9 stations between 10 stations can reduce max distance to 0.5.