Skip to content

Fix division by zero in interpolation search#7484

Merged
DenizAltunkapan merged 3 commits into
TheAlgorithms:masterfrom
priyanshuvishwakarma273403:fix-7466-interpolation-search
Jun 20, 2026
Merged

Fix division by zero in interpolation search#7484
DenizAltunkapan merged 3 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.85%. Comparing base (fafc5a2) to head (851e282).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #7484   +/-   ##
=========================================
  Coverage     79.85%   79.85%           
- Complexity     7328     7329    +1     
=========================================
  Files           807      807           
  Lines         23817    23819    +2     
  Branches       4687     4688    +1     
=========================================
+ Hits          19019    19021    +2     
  Misses         4036     4036           
  Partials        762      762           

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

@DenizAltunkapan DenizAltunkapan enabled auto-merge (squash) June 20, 2026 19:53
@DenizAltunkapan DenizAltunkapan merged commit e57a675 into TheAlgorithms:master Jun 20, 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.

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

3 participants