Skip to content

Fix division by zero in interpolation search#7484

Open
priyanshuvishwakarma273403 wants to merge 2 commits into
TheAlgorithms:masterfrom
priyanshuvishwakarma273403:fix-7466-interpolation-search
Open

Fix division by zero in interpolation search#7484
priyanshuvishwakarma273403 wants to merge 2 commits into
TheAlgorithms:masterfrom
priyanshuvishwakarma273403:fix-7466-interpolation-search

Conversation

@priyanshuvishwakarma273403

Copy link
Copy Markdown
Contributor

Description

This PR fixes a java.lang.ArithmeticException: / by zero in InterpolationSearch.find(...) and resolves an issue with the interpolation calculation logic.

Related Issue

Fixes #7466

Changes Made

  1. Division by Zero Prevention: Added a check if (array[start] == array[end]) inside the search loop. If the bounds are equal and the search key lies within them, it returns start directly. This avoids division by zero when calculating the probing position.
  2. Interpolation Formula Fix: Modified the probing index calculation to cast the numerator to long before performing division:
    int pos = start + (int) (((long) (end - start) * (key - array[start])) / ((long) array[end] - array[start]));
    This prevents truncation from integer division (which was causing the algorithm to fallback to a linear search in most cases) and prevents integer overflow.
  3. Tests Added: Added testInterpolationSearchDivisionByZeroEdgeCases in InterpolationSearchTest.java covering the cases described in the issue:
    • [0, 0, 0, 2], 2 -> 3
    • [2, 2, 2, 2], 2 -> 0
    • [0, 1, 2, 4], 4 -> 3

Checklist

  • Code compiles successfully.
  • Unit tests added and passed.
  • Followed repository code style.

@codecov-commenter

codecov-commenter commented Jun 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.86%. Comparing base (8a3bd3a) to head (b7be994).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7484      +/-   ##
============================================
+ Coverage     79.85%   79.86%   +0.01%     
- Complexity     7328     7331       +3     
============================================
  Files           807      807              
  Lines         23817    23819       +2     
  Branches       4687     4688       +1     
============================================
+ Hits          19018    19023       +5     
  Misses         4036     4036              
+ Partials        763      760       -3     

☔ 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.

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.

[BUG] InterpolationSearch().find(...) breaks on certain sorted arrays

2 participants