Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main goal of the Aggressive Cows Maximum Minimum Distance problem?
easy
A. To place cows in stalls so that the minimum distance between any two cows is as large as possible
B. To find the maximum number of cows that can fit in a single stall
C. To minimize the total distance cows travel between stalls
D. To arrange cows so that all are placed in consecutive stalls

Solution

  1. Step 1: Understand the problem objective

    The problem asks to place cows in stalls such that the smallest distance between any two cows is as large as possible.
  2. Step 2: Identify the correct goal

    This means maximizing the minimum distance between cows, not minimizing travel or fitting all cows in one stall.
  3. Final Answer:

    To place cows in stalls so that the minimum distance between any two cows is as large as possible -> Option A
  4. Quick Check:

    Maximize minimum distance = place cows far apart [OK]
Hint: Maximize smallest gap between cows, not total or max distance [OK]
Common Mistakes:
  • Confusing maximum minimum distance with total distance
  • Thinking cows must be in consecutive stalls
  • Trying to minimize distance instead of maximizing
2. Which of the following is the correct way to start the binary search for the minimum distance in the aggressive cows problem in JavaScript?
easy
A. let low = stalls[0]; let high = stalls[stalls.length - 1];
B. let low = 0; let high = stalls[stalls.length - 1] - stalls[0];
C. let low = 1; let high = stalls.length;
D. let low = 0; let high = stalls.length - 1;

Solution

  1. Step 1: Understand binary search range for distance

    The minimum distance can be as low as 0 and as high as the difference between the last and first stall positions.
  2. Step 2: Choose correct initialization

    Setting low = 0 and high = stalls[stalls.length - 1] - stalls[0] correctly covers all possible distances.
  3. Final Answer:

    let low = 0; let high = stalls[stalls.length - 1] - stalls[0]; -> Option B
  4. Quick Check:

    Distance range = 0 to max stall gap [OK]
Hint: Binary search distance between 0 and max stall position gap [OK]
Common Mistakes:
  • Using stall indices instead of stall positions for high
  • Starting low at 1 instead of 0
  • Confusing stall count with distance range
3. What will be the output of the following JavaScript code snippet for stalls = [1, 2, 8, 4, 9] and cows = 3?
function canPlaceCows(stalls, cows, dist) {
  let count = 1;
  let lastPos = stalls[0];
  for (let i = 1; i < stalls.length; i++) {
    if (stalls[i] - lastPos >= dist) {
      count++;
      lastPos = stalls[i];
      if (count === cows) return true;
    }
  }
  return false;
}

const stallsSorted = stalls.sort((a,b) => a-b);
console.log(canPlaceCows(stallsSorted, cows, 3));
medium
A. TypeError
B. false
C. undefined
D. true

Solution

  1. Step 1: Sort stalls and check placement with distance 3

    Sorted stalls: [1, 2, 4, 8, 9]. Place first cow at 1.
  2. Step 2: Place next cows at positions with at least 3 distance

    Next cow at 4 (4-1=3), third cow at 8 (8-4=4). 3 cows placed successfully.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    Can place 3 cows with distance 3 = true [OK]
Hint: Sort stalls first, then check placement with given distance [OK]
Common Mistakes:
  • Not sorting stalls before checking
  • Miscounting cows placed
  • Confusing distance condition with <= instead of >=
4. Identify the bug in this JavaScript function for checking if cows can be placed with a given distance:
function canPlaceCows(stalls, cows, dist) {
  let count = 0;
  let lastPos = stalls[0];
  for (let i = 1; i < stalls.length; i++) {
    if (stalls[i] - lastPos >= dist) {
      count++;
      lastPos = stalls[i];
      if (count === cows) return true;
    }
  }
  return false;
}
medium
A. count should start at 1 because first cow is placed at stalls[0]
B. lastPos should be initialized to 0 instead of stalls[0]
C. The loop should start from i = 0 instead of i = 1
D. The function should return false inside the loop when count < cows

Solution

  1. Step 1: Analyze initial count value

    First cow is placed at stalls[0], so count should start at 1, not 0.
  2. Step 2: Understand impact of count initialization

    Starting count at 0 causes undercounting cows placed, leading to incorrect results.
  3. Final Answer:

    count should start at 1 because first cow is placed at stalls[0] -> Option A
  4. Quick Check:

    Count starts at 1 for first cow placed [OK]
Hint: Count first cow placement before loop starts [OK]
Common Mistakes:
  • Starting count at 0 and missing first cow
  • Changing lastPos incorrectly
  • Returning false too early inside loop
5. Given stalls at positions [2, 4, 7, 11, 14, 17] and 4 cows, what is the maximum minimum distance to place all cows? Use the aggressive cows approach.
hard
A. 7
B. 6
C. 4
D. 5

Solution

  1. Step 1: Sort stalls and set search range

    Stalls sorted: [2, 4, 7, 11, 14, 17]. Distance range low=0, high=15 (17-2).
  2. Step 2: Use binary search to find max minimum distance

    Check mid=7: place cows at 2, 11, only 2 cows placed, fail.
    Check mid=5: place cows at 2, 7, 14, only 3 cows placed, fail.
    Check mid=6: place cows at 2, 11, 17, only 3 cows placed, fail.
    Check mid=4: place cows at 2, 7, 11, 17 (gaps 5,4,6 all >=4), success.
    Check mid=4 works, mid=5 fails, so max distance is 4.
  3. Step 3: Trace placement for mid=5 carefully

    Place first cow at 2.
    Next at 4 (4-2=2 <5), skip.
    Next at 7 (7-2=5 >=5), place second cow, lastPos=7.
    Next at 11 (11-7=4 <5), skip.
    Next at 14 (14-7=7 >=5), place third cow, lastPos=14.
    Next at 17 (17-14=3 <5), skip.
    Only 3 cows placed, need 4, so 5 fails.
  4. Final Answer:

    4 -> Option C
  5. Quick Check:

    Max min distance = 4 [OK]
Hint: Binary search max distance, verify placement count carefully [OK]
Common Mistakes:
  • Not sorting stalls before binary search
  • Miscounting cows placed at mid distance
  • Confusing mid distance checks in binary search
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard