Open In App

Aggressive Cows Problem using JavaScript

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In the aggressive cow’s problem, we are given an array of size n, where elements of the array denote the position of stalls. We are given k number of cows that we have to place in these stalls such that the minimum distance between any of two cows is maximum.

It is a standard problem that can be solved optimally by using a binary search algorithm.

Below are the approaches to solving the Aggressive cow’s problem:

Brute force Approach

We will first sort the stall array. Now we will use a for loop to check all possible distances. Inside the loop create a “canweplace()” function to checks if it’s possible to place all cows with a certain minimum distance between them. Now, Iterate through all possible pairs of stalls and check if it’s possible to place cows with the distance between those stalls. Return the maximum minimum distance obtained.

Example: To demonstrate aggressive cows problem using Naive Approach.

JavaScript
function canWePlace(stalls, dist, cows) {
    let cntCows = 1;
    let last = stalls[0];
    for (let i = 1; i < stalls.length; i++) {
        if (stalls[i] - last >= dist) {
            cntCows++;
            last = stalls[i];
        }
        if (cntCows >= cows) return true;
    }
    return false;
}

function aggressiveCows(stalls, k) {

    // first we will Sort the stalls
    stalls.sort((a, b) => a - b);
    const maxLimit = stalls[stalls.length - 1] - stalls[0];
    for (let i = 1; i <= maxLimit; i++) {
        if (!canWePlace(stalls, i, k)) {
            return i - 1;
        }
    }
    // else 
    return maxLimit;
}

const stalls = [1, 2, 4, 8, 9];
const k = 3;
const ans = aggressiveCows(stalls, k);
console.log(`The maximum possible minimum distance using
  binary search algo is ${ans}`);

Output
The maximum possible minimum distance using
binary search algo is 3

Time Complexity: O(NlogN) + O(N*max(stalls[]) – min(stalls[])))

Space Complexity: O(1)

Binary Search Approach

We will use binary search algorithm to optimize the code and reduce time complexity of problem by reducing search space by half. First sort the stalls array. Take two pointers low and high such that low = 1 and high = stalls[n-1] – stalls[0]. Now perform binary search. Calculate the mid. Call “canWePlace()” function and Update the maximum possible minimum distance and search on right half , if not possible search on left half. Return the maximum minimum distance.

Example: To demonstrate aggressive cows problem using binary search algorithm.

JavaScript
function canWePlace(stalls, dist, cows) {
    let cntCows = 1;
    let last = stalls[0];
    for (let i = 1; i < stalls.length; i++) {
        if (stalls[i] - last >= dist) {
            cntCows++;
            last = stalls[i];
        }
        if (cntCows >= cows) return true;
    }
    return false;
}

function aggressiveCows(stalls, k) {
    stalls.sort((a, b) => a - b);
    let low = 1, high = stalls[stalls.length - 1] - stalls[0];
    let ans = 0;
    // Initialize answer variable

    // Binary search
    while (low <= high) {
        // Calculate mid 
        let mid = Math.floor((low + high) / 2);
        if (canWePlace(stalls, mid, k)) {
            // Update answer
            ans = mid;
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }

    return ans;
}


const stalls = [1, 2, 4, 8, 9];
const k = 3;
const ans = aggressiveCows(stalls, k);
console.log(`The maximum possible minimum distance using
  binary search algo is ${ans}`);

Output
The maximum possible minimum distance using
binary search algo is 3


Time Complexity: O(NlogN) + O(N*log(stalls[]) – min(stalls[])))

Space Complexity: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads