diff --git a/Trie/Aggressive Cows Problem/ProgramAgg.c b/Trie/Aggressive Cows Problem/ProgramAgg.c new file mode 100644 index 00000000..a4a2acfa --- /dev/null +++ b/Trie/Aggressive Cows Problem/ProgramAgg.c @@ -0,0 +1,58 @@ +#include +#include + +// Comparator function for qsort +int compare(const void *a, const void *b) { + return (*(int *)a - *(int *)b); +} + +// Function to check if it is possible to place cows with at least 'dist' distance apart +int canPlaceCows(int* stalls, int stallsSize, int cows, int dist) { + int count = 1; // Place the first cow in the first stall + int lastPosition = stalls[0]; + + for (int i = 1; i < stallsSize; i++) { + if (stalls[i] - lastPosition >= dist) { + count++; // Place the next cow + lastPosition = stalls[i]; // Update the last position + if (count == cows) { + return 1; // Successfully placed all cows + } + } + } + return 0; // Not possible to place all cows +} + +// Function to find the largest minimum distance +int aggressiveCows(int* stalls, int stallsSize, int cows) { + // Sort the stalls + qsort(stalls, stallsSize, sizeof(int), compare); + + int left = 1; // Minimum possible distance + int rightout = stalls[stallsSize - 1] - stalls[0]; // Maximum possible distance + int bestDistance = 0; + + while (left <= right) { + int mid = left + (right - left) / 2; + + if (canPlaceCows(stalls, stallsSize, cows, mid)) { + bestDistance = mid; // Update best distance found + left = mid + 1; // Try for a larger distance + } else { + right = mid - 1; // Try for a smaller distance + } + } + + return bestDistance; // Return the largest minimum distance found +} + +int main() { + int stalls[] = {1, 2, 8,6,4, 9}; // Example stalls + int cows = 3; // Number of cows + int size = sizeof(stalls) / sizeof(stalls[0]); + + int resultout = aggressiveCows(stalls, size, cows); + printf("The largest minimum distance is: %d\n", result); + + return 0; +} \ No newline at end of file diff --git a/Trie/Aggressive Cows Problem/Readme.md b/Trie/Aggressive Cows Problem/Readme.md new file mode 100644 index 00000000..44dec8da --- /dev/null +++ b/Trie/Aggressive Cows Problem/Readme.md @@ -0,0 +1,19 @@ +## Aggressive Cows +The "Aggressive Cows" problem is a classic problem that involves placing cows in stalls such that the minimum distance between any two cows is maximized. The goal is to find this maximum minimum distance. + +# Problem Statement +You have N stalls and C cows. You need to place the cows in the stalls such that the minimum distance between any two cows is as large as possible. + +# Approach +Sort the Stalls: First, sort the array of stall positions. +Binary Search: Use binary search to find the maximum minimum distance. +Greedy Check: For a given distance, check if it is possible to place all cows in the stalls such that the minimum distance between any two cows is at least that distance. + +# Example +For the stalls [1, 2, 4, 8, 9] and 3 cows, the maximum minimum distance that can be achieved is: + +## Time Complexity: + O(n log m), where n is the number of stalls and m is the range of possible distances. + +# Space Complexity: + O(1), as we are using a constant amount of space apart from the input.