Skip to content

feat: Add NearestElement stack algorithms and fix Javadoc build warnings#7434

Open
sabarirajan83 wants to merge 2 commits into
TheAlgorithms:masterfrom
sabarirajan83:nearest-elements-in-array
Open

feat: Add NearestElement stack algorithms and fix Javadoc build warnings#7434
sabarirajan83 wants to merge 2 commits into
TheAlgorithms:masterfrom
sabarirajan83:nearest-elements-in-array

Conversation

@sabarirajan83
Copy link
Copy Markdown

Description

This PR introduces NearestElement.java, which provides stack-based algorithms to efficiently find the nearest greater or smaller elements in an array (for both left and right directions).

  • Implements a Monotonic Stack approach to achieve O(n) time complexity.
  • Includes explicit safety checks (using short-circuit evaluation and ternary operators) to prevent EmptyStackException.
  • Includes comprehensive JUnit 5 tests in NearestElementTest.java.

Additionally, this PR fixes pre-existing documentation comment is not attached to any declaration errors in:

  • AnyBaseToAnyBase.java
  • InterpolationSearch.java
  • LinearSearch.java

These documentation changes ensure strict build compatibility with modern JDK versions (JDK 21+).

Fixes #7322

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Fixed 'documentation comment is not attached to any declaration' errors in:
- AnyBaseToAnyBase.java
- InterpolationSearch.java
- LinearSearch.java
These changes ensure build compatibility with modern JDK versions.
Copy link
Copy Markdown
Contributor

@prashantpiyush1111 prashantpiyush1111 left a comment

Choose a reason for hiding this comment

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

Good implementation overall! Reviewed the NearestElement
stack algorithm. Suggest using ArrayDeque instead of
Stack class for better performance.

int n = arr.length;
int[] result = new int[n];
Stack<Integer> stack = new Stack<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider using ArrayDeque instead of Stack class,
it is faster and recommended in Java docs.

// --- Exception Handling ---

@Test
void testNullInput() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great test coverage for null input edge case!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Permutations in java

2 participants