0
0
DSA Javascriptprogramming~20 mins

Aggressive Cows Maximum Minimum Distance in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Aggressive Cows Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of placing cows with minimum distance 3
Given the stalls at positions [1, 2, 8, 4, 9] and 3 cows, what is the maximum minimum distance between any two cows?
DSA Javascript
function canPlaceCows(stalls, cows, dist) {
  let count = 1;
  let last_position = stalls[0];
  for (let i = 1; i < stalls.length; i++) {
    if (stalls[i] - last_position >= dist) {
      count++;
      last_position = stalls[i];
      if (count === cows) return true;
    }
  }
  return false;
}

function aggressiveCows(stalls, cows) {
  stalls.sort((a, b) => a - b);
  let low = 0;
  let high = stalls[stalls.length - 1] - stalls[0];
  let result = 0;
  while (low <= high) {
    let mid = Math.floor((low + high) / 2);
    if (canPlaceCows(stalls, cows, mid)) {
      result = mid;
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return result;
}

console.log(aggressiveCows([1, 2, 8, 4, 9], 3));
A4
B2
C3
D5
Attempts:
2 left
💡 Hint
Think about binary searching the minimum distance and checking feasibility.
Predict Output
intermediate
2:00remaining
Output for stalls [10, 20, 30, 40, 50] and 2 cows
What is the maximum minimum distance between 2 cows placed in stalls at positions [10, 20, 30, 40, 50]?
DSA Javascript
function canPlaceCows(stalls, cows, dist) {
  let count = 1;
  let last_position = stalls[0];
  for (let i = 1; i < stalls.length; i++) {
    if (stalls[i] - last_position >= dist) {
      count++;
      last_position = stalls[i];
      if (count === cows) return true;
    }
  }
  return false;
}

function aggressiveCows(stalls, cows) {
  stalls.sort((a, b) => a - b);
  let low = 0;
  let high = stalls[stalls.length - 1] - stalls[0];
  let result = 0;
  while (low <= high) {
    let mid = Math.floor((low + high) / 2);
    if (canPlaceCows(stalls, cows, mid)) {
      result = mid;
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return result;
}

console.log(aggressiveCows([10, 20, 30, 40, 50], 2));
A40
B20
C30
D10
Attempts:
2 left
💡 Hint
With only 2 cows, place them at the farthest stalls.
🔧 Debug
advanced
2:00remaining
Identify the error in the canPlaceCows function
What error will the following code cause when running aggressiveCows([1, 2, 4, 8, 9], 3)?
DSA Javascript
function canPlaceCows(stalls, cows, dist) {
  let count = 0;
  let last_position = stalls[0];
  for (let i = 1; i < stalls.length; i++) {
    if (stalls[i] - last_position >= dist) {
      count++;
      last_position = stalls[i];
      if (count === cows) return true;
    }
  }
  return false;
}

function aggressiveCows(stalls, cows) {
  stalls.sort((a, b) => a - b);
  let low = 0;
  let high = stalls[stalls.length - 1] - stalls[0];
  let result = 0;
  while (low <= high) {
    let mid = Math.floor((low + high) / 2);
    if (canPlaceCows(stalls, cows, mid)) {
      result = mid;
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return result;
}

console.log(aggressiveCows([1, 2, 4, 8, 9], 3));
AThe function returns false always because count starts at 0 instead of 1
BSyntaxError due to missing semicolon
CTypeError because stalls is undefined
DNo error, output is 3
Attempts:
2 left
💡 Hint
Check the initial count of placed cows.
🧠 Conceptual
advanced
2:00remaining
Why use binary search in Aggressive Cows problem?
Why is binary search used to find the maximum minimum distance in the Aggressive Cows problem?
ABecause binary search sorts the stalls automatically
BBecause the search space of distances is sorted and we can check feasibility efficiently
CBecause it finds the exact positions of cows directly
DBecause it reduces the number of cows needed
Attempts:
2 left
💡 Hint
Think about the range of possible distances and how to test them.
🚀 Application
expert
3:00remaining
Maximum minimum distance for large input
Given stalls at positions [1, 2, 4, 8, 9, 12, 15, 18, 20, 25] and 4 cows, what is the maximum minimum distance between any two cows?
DSA Javascript
function canPlaceCows(stalls, cows, dist) {
  let count = 1;
  let last_position = stalls[0];
  for (let i = 1; i < stalls.length; i++) {
    if (stalls[i] - last_position >= dist) {
      count++;
      last_position = stalls[i];
      if (count === cows) return true;
    }
  }
  return false;
}

function aggressiveCows(stalls, cows) {
  stalls.sort((a, b) => a - b);
  let low = 0;
  let high = stalls[stalls.length - 1] - stalls[0];
  let result = 0;
  while (low <= high) {
    let mid = Math.floor((low + high) / 2);
    if (canPlaceCows(stalls, cows, mid)) {
      result = mid;
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return result;
}

console.log(aggressiveCows([1, 2, 4, 8, 9, 12, 15, 18, 20, 25], 4));
A8
B5
C6
D7
Attempts:
2 left
💡 Hint
Use binary search and feasibility check to find the answer.