Skip to content

[Request] Add Nearest Greater and Smaller Element Algorithms #6656

@adiancodes

Description

@adiancodes

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions