0
0
DSA C++programming~20 mins

Aggressive Cows Maximum Minimum Distance in DSA C++ - 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 maximum minimum distance calculation
What is the output of the following C++ code that finds the largest minimum distance to place 3 cows in given stalls?
DSA C++
/*
Stalls positions: 1, 2, 8, 4, 9
Number of cows: 3
*/
#include <iostream>
#include <vector>
#include <algorithm>

bool canPlaceCows(const std::vector<int>& stalls, int cows, int dist) {
    int count = 1; // place first cow at first stall
    int last_position = stalls[0];
    for (int i = 1; i < stalls.size(); i++) {
        if (stalls[i] - last_position >= dist) {
            count++;
            last_position = stalls[i];
            if (count == cows) return true;
        }
    }
    return false;
}

int maxMinDistance(std::vector<int>& stalls, int cows) {
    std::sort(stalls.begin(), stalls.end());
    int low = 1;
    int high = stalls.back() - stalls[0];
    int result = 0;
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (canPlaceCows(stalls, cows, mid)) {
            result = mid;
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return result;
}

int main() {
    std::vector<int> stalls = {1, 2, 8, 4, 9};
    int cows = 3;
    std::cout << maxMinDistance(stalls, cows) << std::endl;
    return 0;
}
A3
B4
C5
D2
Attempts:
2 left
💡 Hint
Think about binary searching the minimum distance and checking feasibility.
🧠 Conceptual
intermediate
1:30remaining
Understanding the feasibility check in Aggressive Cows problem
In the Aggressive Cows problem, what does the feasibility function (like canPlaceCows) check?
AIf cows can be placed in all stalls without any distance restriction
BIf cows can be placed to maximize the total distance sum between them
CIf cows can be placed only in consecutive stalls
DIf cows can be placed such that the minimum distance between any two cows is at least a given value
Attempts:
2 left
💡 Hint
Focus on the minimum distance condition.
🔧 Debug
advanced
2:00remaining
Identify the bug in this Aggressive Cows implementation
What error does the following code produce when run, and why? #include #include #include bool canPlaceCows(std::vector& stalls, int cows, int dist) { int count = 1; int last_position = stalls[0]; for (int i = 1; i <= stalls.size(); i++) { if (stalls[i] - last_position >= dist) { count++; last_position = stalls[i]; if (count == cows) return true; } } return false; } int main() { std::vector stalls = {1, 2, 4, 8, 9}; std::sort(stalls.begin(), stalls.end()); std::cout << canPlaceCows(stalls, 3, 3) << std::endl; return 0; }
ACompilation error due to missing return statement
BRuns successfully and prints 1
CThrows std::out_of_range exception
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop boundary condition.
🚀 Application
advanced
2:00remaining
Applying Aggressive Cows logic to a new scenario
You have stalls at positions [10, 20, 30, 40, 50, 60] and want to place 4 cows. What is the maximum minimum distance possible?
A10
B15
C20
D25
Attempts:
2 left
💡 Hint
Try placing cows greedily with binary search on distance.
🧠 Conceptual
expert
1:30remaining
Why binary search is used in Aggressive Cows problem?
Why is binary search an efficient approach to find the maximum minimum distance in the Aggressive Cows problem?
ABecause the search space of possible minimum distances is sorted and monotonic in feasibility
BBecause the stalls are sorted and binary search finds the exact stall positions
CBecause binary search can directly place cows in optimal positions
DBecause it reduces the number of stalls to consider
Attempts:
2 left
💡 Hint
Think about how feasibility changes with distance.