0
0
DSA C++programming~10 mins

Aggressive Cows Maximum Minimum Distance in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to sort the stall positions before placing cows.

DSA C++
#include <algorithm>
#include <vector>

void placeCows(std::vector<int>& stalls) {
    std::sort(stalls.begin(), stalls.[1]);
}
Drag options to blanks, or click blank then click option'
Arend
Brbegin
Cbegin
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using reverse iterators like rbegin or rend which are incorrect for std::sort.
Using begin twice which does not specify the full range.
2fill in blank
medium

Complete the code to check if cows can be placed with at least 'dist' minimum distance.

DSA C++
bool canPlaceCows(const std::vector<int>& 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 >= [1]) {
            count++;
            last_position = stalls[i];
        }
    }
    return count >= cows;
}
Drag options to blanks, or click blank then click option'
Acows
Bdist
Clast_position
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using the number of cows instead of the distance.
Using the last position variable incorrectly.
3fill in blank
hard

Fix the error in the binary search loop condition to find the maximum minimum distance.

DSA C++
int aggressiveCows(std::vector<int>& stalls, int cows) {
    std::sort(stalls.begin(), stalls.end());
    int low = 1;
    int high = stalls.back() - stalls.front();
    int result = 0;
    while (low [1] high) {
        int mid = low + (high - low) / 2;
        if (canPlaceCows(stalls, cows, mid)) {
            result = mid;
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return result;
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which may skip checking the last possible distance.
Using '>' or '>=' which reverses the logic.
4fill in blank
hard

Fill both blanks to correctly update the binary search bounds based on placement feasibility.

DSA C++
if (canPlaceCows(stalls, cows, mid)) {
    result = mid;
    low = [1];
} else {
    high = [2];
}
Drag options to blanks, or click blank then click option'
Amid + 1
Bmid - 1
Clow + 1
Dhigh - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the updates of low and high.
Using incorrect arithmetic on mid.
5fill in blank
hard

Fill all three blanks to complete the main function that reads input and outputs the result.

DSA C++
#include <iostream>
#include <vector>

int main() {
    int n, c;
    std::cin >> [1] >> [2];
    std::vector<int> stalls([3]);
    for (int i = 0; i < n; i++) {
        std::cin >> stalls[i];
    }
    std::cout << aggressiveCows(stalls, c) << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
An
Bc
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of inputs.
Using wrong variable for vector size.