Skip to content

Commit

Permalink
Made the modification
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipanita45 committed Oct 29, 2024
1 parent 3f04612 commit c796541
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Trie/Aggressive Cows Problem/ProgramAgg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>

// 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;
}
19 changes: 19 additions & 0 deletions Trie/Aggressive Cows Problem/Readme.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit c796541

Please sign in to comment.