Skip to content

feat: add aggressive cows algorithm - #3233

Open
aartigawade2586 wants to merge 3 commits into
TheAlgorithms:masterfrom
aartigawade2586:aggressive_cows
Open

aartigawade2586 wants to merge 3 commits into
TheAlgorithms:masterfrom
aartigawade2586:aggressive_cows

Conversation

@aartigawade2586

@aartigawade2586 aartigawade2586 commented Sep 27, 2026 •

Copy link
Copy Markdown

Description of Change

Added an implementation of the Aggressive Cows problem using binary search
on the answer and a greedy feasibility check.

The solution:

  • Sorts the stall positions.
  • Uses binary search to find the maximum possible minimum distance.
  • Uses a greedy approach to check whether all cows can be placed.
  • Includes self-test cases using assert().

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational - Doxygen guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes: Added search/aggressive_cows.cpp with a binary-search-based solution and self-tests.

@aartigawade2586 aartigawade2586 changed the title Add aggressive cows solution feat: add aggressive cows algorithm Sep 27, 2026
@realstealthninja

Copy link
Copy Markdown
Collaborator

could you add some references as to where this problem originates from?

@realstealthninja realstealthninja added the question Further information is requested label Sep 29, 2026
@aartigawade2586

Copy link
Copy Markdown
Author

could you add some references as to where this problem originates from?

Added the reference to the problem and its original source (USACO February 2005 Gold Division) in the file header.

@realstealthninja realstealthninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing! A few changes need to be made. Please read the contributing.md in the root of the repository and the doxygen documentation.

Comment thread search/aggressive_cows.cpp Outdated
Comment on lines +5 to +9
* (https://www.spoj.com/problems/AGGRCOW/)
* problem from the USACO February 2005 Gold Division.
*/
/* Aggressive cow
*Problem :

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* (https://www.spoj.com/problems/AGGRCOW/)
* problem from the USACO February 2005 Gold Division.
*/
/* Aggressive cow
*Problem :
* @details
* [Aggressive Cows](https://www.spoj.com/problems/AGGRCOW/)

Comment thread search/aggressive_cows.cpp Outdated
Comment on lines +21 to +22
*/
/*

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
/*
*
*

Comment thread search/aggressive_cows.cpp Outdated
* The minimum distance between any two cows must be maximized.
*/
/*
###Time and Space complexity

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
###Time and Space complexity
* ###Time and Space complexity

Comment thread search/aggressive_cows.cpp Outdated
Comment on lines +24 to +27
* Time complexity in worst case is O(n log n + n log(maxDistance))
* Time complexity for best case is O(n log n)
* Time complexity for average case is O(n log n + n log(maxDistance))
* Space complexity is O(1) , excluding sorting overhead

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should format these time/space complexities using latex .i.e. \f$ O(n) \f$

Comment thread search/aggressive_cows.cpp Outdated
#include <cassert> /// for std::assert
#include <iostream> // for IO operations
#include <vector> // for std::vector
using namespace std;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using namespace std;

By using namespace std, you'll pollute the global name space, It is bad practice to do that.

Comment thread search/aggressive_cows.cpp Outdated
Comment on lines +37 to +38
// checking if given distance is valid for given number of cows
bool isValid(int mid, int n, const vector<int>& stalls) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function is not documented in doxygen style.

Comment thread search/aggressive_cows.cpp Outdated
test2(); // run self-test implementation #2
test3(); // run self-test implementation #3
return 0;
} No newline at end of file

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Comment thread search/aggressive_cows.cpp Outdated
int main() {
test1(); // run self-test implementation #1
test2(); // run self-test implementation #2
test3(); // run self-test implementation #3

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer wrapping all these functions into one static tests function.

Comment thread search/aggressive_cows.cpp Outdated
****************************************************************************************************************
*/
// Will return maximum minimum distance between two cows
int aggressiveCows(vector<int>& stalls, int n) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must wrap all non test functions in the search name space.

* If we are given stall positions 1 and 5, then the distance between
* these two stalls will be 4, i.e., '5 - 1'.
* Here, as the cows are aggressive, we need to allocate them stalls in such
* a way that there is a maximum distance between any two cows.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should write about what kind of search algorithm is actually used here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed the review comments: updated Doxygen documentation, clarified the search algorithm, fixed complexity formatting, removed using namespace std, added documentation for isValid, and cleaned up the test structure.

@realstealthninja realstealthninja added requested changes changes have been requested and removed question Further information is requested labels Oct 7, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

requested changes changes have been requested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants