0
0
DSA Javascriptprogramming~20 mins

Binary Search on Answer Technique in DSA Javascript - 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 JavaScript code that uses binary search on answer to find the minimum largest sum among subarrays?
DSA Javascript
function canSplit(nums, maxSum, m) {
  let currentSum = 0, count = 1;
  for (const num of nums) {
    if (currentSum + num > maxSum) {
      count++;
      currentSum = num;
      if (count > m) return false;
    } else {
      currentSum += num;
    }
  }
  return true;
}

function splitArray(nums, m) {
  let left = Math.max(...nums);
  let right = nums.reduce((a, b) => a + b, 0);
  while (left < right) {
    const mid = Math.floor((left + right) / 2);
    if (canSplit(nums, mid, m)) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
}

const nums = [7,2,5,10,8];
const m = 2;
console.log(splitArray(nums, m));
A18
B15
C20
D23
Attempts:
2 left
💡 Hint
Think about splitting the array into 2 parts such that the largest sum among them is minimized.
🧠 Conceptual
intermediate
1:30remaining
Understanding Binary Search on Answer Technique
Which of the following best describes the main idea behind the binary search on answer technique?
APerform binary search on the input array elements directly to find a target value.
BUse binary search on a range of possible answers to find the minimum or maximum feasible value satisfying a condition.
CSort the array and then apply binary search to find the median element.
DUse binary search to find the first occurrence of a value in a sorted array.
Attempts:
2 left
💡 Hint
Think about searching over a range of values, not array indices.
🔧 Debug
advanced
2:00remaining
Identify the Error in Binary Search on Answer Implementation
What error does the following code produce when trying to find the minimum maximum subarray sum using binary search on answer?
DSA Javascript
function canSplit(nums, maxSum, m) {
  let currentSum = 0, count = 1;
  for (const num of nums) {
    if (currentSum + num >= maxSum) {
      count++;
      currentSum = num;
      if (count > m) return false;
    } else {
      currentSum += num;
    }
  }
  return true;
}

function splitArray(nums, m) {
  let left = Math.max(...nums);
  let right = nums.reduce((a, b) => a + b, 0);
  while (left < right) {
    const mid = Math.floor((left + right) / 2);
    if (canSplit(nums, mid, m)) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
}

const nums = [7,2,5,10,8];
const m = 2;
console.log(splitArray(nums, m));
AThe function does not handle empty arrays, causing runtime error.
BThe initial left boundary should be 0 instead of max element.
CThe condition 'currentSum + num >= maxSum' causes incorrect splitting, leading to wrong output.
DThe binary search loop condition should be 'left <= right' instead of 'left < right'.
Attempts:
2 left
💡 Hint
Check the condition that triggers a new subarray split.
🚀 Application
advanced
2:00remaining
Applying Binary Search on Answer to Allocate Books Problem
Given an array representing pages in books and number of students, which output is correct for minimum maximum pages allocated using binary search on answer?
DSA Javascript
function isPossible(books, maxPages, students) {
  let requiredStudents = 1, currentSum = 0;
  for (const pages of books) {
    if (pages > maxPages) return false;
    if (currentSum + pages > maxPages) {
      requiredStudents++;
      currentSum = pages;
      if (requiredStudents > students) return false;
    } else {
      currentSum += pages;
    }
  }
  return true;
}

function allocateBooks(books, students) {
  let left = Math.max(...books);
  let right = books.reduce((a, b) => a + b, 0);
  while (left < right) {
    const mid = Math.floor((left + right) / 2);
    if (isPossible(books, mid, students)) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
}

const books = [12, 34, 67, 90];
const students = 2;
console.log(allocateBooks(books, students));
A113
B157
C67
D90
Attempts:
2 left
💡 Hint
Try splitting books into 2 students with minimum maximum pages.
🧠 Conceptual
expert
1:30remaining
Why is Binary Search on Answer Efficient for Certain Problems?
Why does binary search on answer technique provide an efficient solution for problems like splitting arrays or allocating resources?
ABecause it uses dynamic programming to store intermediate results.
BBecause it sorts the input data first, reducing complexity.
CBecause it uses recursion to explore all possible partitions quickly.
DBecause it converts a complex problem into a series of feasibility checks over a numeric range, reducing search space logarithmically.
Attempts:
2 left
💡 Hint
Think about how the search space is reduced step by step.