From 8f1480ec0105678b99a2a0d5de21d71435299fe0 Mon Sep 17 00:00:00 2001 From: Priyanshu Date: Sat, 20 Jun 2026 22:45:33 +0530 Subject: [PATCH 1/2] Fix division by zero in interpolation search --- .../thealgorithms/searches/InterpolationSearch.java | 5 ++++- .../searches/InterpolationSearchTest.java | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/InterpolationSearch.java b/src/main/java/com/thealgorithms/searches/InterpolationSearch.java index 272627fc48b4..52dbc0b7e2c5 100644 --- a/src/main/java/com/thealgorithms/searches/InterpolationSearch.java +++ b/src/main/java/com/thealgorithms/searches/InterpolationSearch.java @@ -36,9 +36,12 @@ public int find(int[] array, int key) { // Since array is sorted, an element present // in array must be in range defined by corner while (start <= end && key >= array[start] && key <= array[end]) { + if (array[start] == array[end]) { + return start; + } // Probing the position with keeping // uniform distribution in mind. - int pos = start + (((end - start) / (array[end] - array[start])) * (key - array[start])); + int pos = start + (int) (((long) (end - start) * (key - array[start])) / ((long) array[end] - array[start])); // Condition of target found if (array[pos] == key) { diff --git a/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java b/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java index b3b7e7ef129c..b4b108e547a2 100644 --- a/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java +++ b/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java @@ -87,4 +87,15 @@ void testInterpolationSearchLargeNonUniformArray() { int key = 21; // Present in the array assertEquals(6, interpolationSearch.find(array, key), "The index of the found element should be 6."); } + + /** + * Test for interpolation search with specific sorted arrays that previously caused division by zero. + */ + @Test + void testInterpolationSearchDivisionByZeroEdgeCases() { + InterpolationSearch interpolationSearch = new InterpolationSearch(); + assertEquals(3, interpolationSearch.find(new int[]{0, 0, 0, 2}, 2)); + assertEquals(0, interpolationSearch.find(new int[]{2, 2, 2, 2}, 2)); + assertEquals(3, interpolationSearch.find(new int[]{0, 1, 2, 4}, 4)); + } } From b7be994014be0dc57f120ea6cb61effa17333542 Mon Sep 17 00:00:00 2001 From: Priyanshu Date: Sat, 20 Jun 2026 23:04:10 +0530 Subject: [PATCH 2/2] style: fix formatting in InterpolationSearchTest --- .../com/thealgorithms/searches/InterpolationSearchTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java b/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java index b4b108e547a2..b7ef64125da8 100644 --- a/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java +++ b/src/test/java/com/thealgorithms/searches/InterpolationSearchTest.java @@ -94,8 +94,8 @@ void testInterpolationSearchLargeNonUniformArray() { @Test void testInterpolationSearchDivisionByZeroEdgeCases() { InterpolationSearch interpolationSearch = new InterpolationSearch(); - assertEquals(3, interpolationSearch.find(new int[]{0, 0, 0, 2}, 2)); - assertEquals(0, interpolationSearch.find(new int[]{2, 2, 2, 2}, 2)); - assertEquals(3, interpolationSearch.find(new int[]{0, 1, 2, 4}, 4)); + assertEquals(3, interpolationSearch.find(new int[] {0, 0, 0, 2}, 2)); + assertEquals(0, interpolationSearch.find(new int[] {2, 2, 2, 2}, 2)); + assertEquals(3, interpolationSearch.find(new int[] {0, 1, 2, 4}, 4)); } }