0
0
DSA Typescriptprogramming~20 mins

Aggressive Cows Maximum Minimum Distance in DSA Typescript - 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, 4, 8, 9] and 3 cows, what is the maximum minimum distance between any two cows?
DSA Typescript
function canPlaceCows(stalls: number[], cows: number, dist: number): boolean {
  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;
}

function aggressiveCows(stalls: number[], cows: number): number {
  stalls.sort((a, b) => a - b);
  let low = 1;
  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));
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Try placing cows starting with the smallest distance and increase it using binary search.
🧠 Conceptual
intermediate
1:30remaining
Why do we sort stalls before placing cows?
In the Aggressive Cows problem, why is it necessary to sort the stall positions before applying the binary search approach?
ATo reduce the number of stalls to consider by removing duplicates.
BSorting is not necessary; cows can be placed in any order.
CTo ensure we can check distances in increasing order and place cows greedily.
DTo make the stalls positions negative for easier calculation.
Attempts:
2 left
💡 Hint
Think about how distance checking works between stalls.
🔧 Debug
advanced
1:30remaining
Identify the error in this code snippet
What error will this TypeScript code produce when trying to find the maximum minimum distance for aggressive cows? function canPlaceCows(stalls: number[], cows: number, dist: number): boolean { 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; } // rest of code omitted for brevity
AThe condition 'stalls[i] - lastPos > dist' causes incorrect placement, missing valid positions.
BSyntaxError due to missing semicolon after 'let count = 1'
CTypeError because 'stalls' is not defined
DNo error, code runs correctly
Attempts:
2 left
💡 Hint
Check the comparison operator in the if condition.
🚀 Application
advanced
1:30remaining
Maximum minimum distance for large input
Given 5 stalls at positions [10, 20, 30, 40, 50] and 3 cows, what is the maximum minimum distance to place all cows?
A20
B10
C15
D25
Attempts:
2 left
💡 Hint
Try placing cows with distances 10, 15, 20 and check feasibility.
🧠 Conceptual
expert
2:00remaining
Why binary search works on distance, not positions?
In the Aggressive Cows problem, why do we apply binary search on the minimum distance between cows instead of the stall positions themselves?
ABecause searching positions directly would require checking all permutations of cow placements.
BBecause stall positions are unsorted and cannot be searched.
CBecause binary search only works on numeric arrays, not positions.
DBecause the answer is a distance value, and binary searching distances narrows down the feasible minimum gap efficiently.
Attempts:
2 left
💡 Hint
Think about what the problem is asking to maximize and how binary search helps.