-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1445 from Dipanita45/DipanitaA
Made the modification
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |