Java icon indicating copy to clipboard operation
Java copied to clipboard

[Request] Add Nearest Greater and Smaller Element Algorithms

Open adiancodes opened this issue 5 months ago • 4 comments

What would you like to Propose?

Proposal: Add a new file containing a set of classic stack-based algorithms.

Algorithm Name and Problem Statement: I propose adding a new file, NearestElement.java, to the datastructures/stacks package. This file will implement four closely related algorithms:

  1. Nearest Greater to Right: For each element in an array, find the first element to its right that is greater.
  2. Nearest Greater to Left: For each element in an array, find the first element to its left that is greater.
  3. Nearest Smaller to Right: For each element in an array, find the first element to its right that is smaller.
  4. Nearest Smaller to Left: For each element in an array, find the first element to its left that is smaller.

Justification: These algorithms are a fundamental application of the Stack data structure. They are highly valuable for educational purposes and are common in technical interviews and for solving many popular LeetCode problems.

Hacktoberfest Contribution: I would like to work on this for Hacktoberfest. I have read the CONTRIBUTING.md guidelines and will ensure my submission includes a main method for demonstration and a full suite of JUnit 5 tests.

Issue details

Is your feature request related to a problem? Please describe. This request is to add a set of classic algorithms that are excellent demonstrations of the Stack data structure. These are frequently asked in technical interviews and would be a valuable addition to the library. They are also fundamental for solving many popular LeetCode problems involving arrays and stacks.

Describe the solution you'd like I would like to add a new file, NearestElement.java, inside the datastructures/stacks package. This file will contain four static methods:

  • nearestGreaterToRight(int[] arr)
  • nearestGreaterToLeft(int[] arr)
  • nearestSmallerToRight(int[] arr)
  • nearestSmallerToLeft(int[] arr)

Additional context I would like to work on this contribution for Hacktoberfest.

I have read the CONTRIBUTING.md guidelines and will ensure my pull request includes:

  1. A class-level Javadoc explaining the algorithms.
  2. A main method inside NearestElement.java for demonstration.
  3. A corresponding NearestElementTest.java file with JUnit 5 tests.

Could you please assign this issue to me?

Additional Information

No response

adiancodes avatar Oct 05 '25 15:10 adiancodes

can you assign this issue to me

shafaq16 avatar Oct 05 '25 16:10 shafaq16

please check PR #6693

sgindeed avatar Oct 08 '25 04:10 sgindeed

Hi @alxkm, I would like to work on this issue as part of Hacktoberfest 2025. I’ll make sure to follow the contribution guidelines and write clean, well-documented Java code. Thank you!

RiyaJoshi0103 avatar Oct 11 '25 12:10 RiyaJoshi0103

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution!

github-actions[bot] avatar Nov 11 '25 00:11 github-actions[bot]