Skip to content

fix(dynamicprogramming): handle n=0 and negative inputs safely in CatalanNumber - #7731

Merged
alxkm merged 2 commits into
TheAlgorithms:masterfrom
Bhumit267:fix/catalan-number-zero-input
Nov 5, 2026
Merged

alxkm merged 2 commits into
TheAlgorithms:masterfrom
Bhumit267:fix/catalan-number-zero-input

Conversation

@Bhumit267

Copy link
Copy Markdown
Contributor
  • 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

Description

Fixes an ArrayIndexOutOfBoundsException when computing the 0-th Catalan number (findNthCatalan(0)) in com.thealgorithms.dynamicprogramming.CatalanNumber, and prevents NegativeArraySizeException on negative inputs.

Problem

In CatalanNumber.java, findNthCatalan(int n) initializes catalanArray = new long[n + 1]. For n = 0, the allocated array has length 1 (valid index 0 only). The subsequent line unconditionally sets catalanArray[1] = 1;, throwing an ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1. By mathematical definition, $C_0 = 1$. Additionally, passing negative values previously triggered NegativeArraySizeException.

Changes

  • Added base boundary checks at the beginning of findNthCatalan:
    • Returns 1L for n <= 1 (n == 0 and n == 1).
    • Throws IllegalArgumentException when n < 0.
  • Updated Javadoc to document @throws IllegalArgumentException if n is negative.
  • Added test cases in CatalanNumberTest for n = 0, n = 1, and n = -1.

Tests

  • mvn test -Dtest=CatalanNumberTest (2 tests, 0 failures)
  • mvn checkstyle:check (0 violations)

@codecov-commenter

codecov-commenter commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.28%. Comparing base (0a76bf8) to head (392cf58).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7731      +/-   ##
============================================
- Coverage     82.28%   82.28%   -0.01%     
  Complexity     8412     8412              
============================================
  Files           843      843              
  Lines         26146    26150       +4     
  Branches       5103     5104       +1     
============================================
+ Hits          21514    21517       +3     
  Misses         3841     3841              
- Partials        791      792       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good. Thank you for the contribution.

@alxkm
alxkm merged commit 8b19b87 into TheAlgorithms:master Nov 5, 2026
7 checks passed
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.

3 participants