[Request] Add Nearest Greater and Smaller Element Algorithms
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:
- Nearest Greater to Right: For each element in an array, find the first element to its right that is greater.
- Nearest Greater to Left: For each element in an array, find the first element to its left that is greater.
- Nearest Smaller to Right: For each element in an array, find the first element to its right that is smaller.
- 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:
- A class-level Javadoc explaining the algorithms.
- A
mainmethod insideNearestElement.javafor demonstration. - A corresponding
NearestElementTest.javafile with JUnit 5 tests.
Could you please assign this issue to me?
Additional Information
No response
can you assign this issue to me
please check PR #6693
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!
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!