From 1aeb33c8ea18ae062ed87895b6c6abc736c7684f Mon Sep 17 00:00:00 2001 From: alxkm Date: Wed, 9 Jul 2025 22:50:31 +0200 Subject: [PATCH 1/3] cleanup: Improve docs, safety, and readability in RangeInSortedArray --- .../misc/RangeInSortedArray.java | 80 ++++++++++++++----- 1 file changed, 62 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java b/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java index 6d3caa1814b6..ff754b24ee7b 100644 --- a/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java +++ b/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java @@ -1,25 +1,46 @@ package com.thealgorithms.misc; +/** + * Utility class for operations to find the range of occurrences of a key + * in a sorted (non-decreasing) array, and to count elements less than or equal to a given key. + */ public final class RangeInSortedArray { + private RangeInSortedArray() { } - // Get the 1st and last occurrence index of a number 'key' in a non-decreasing array 'nums' - // Gives [-1, -1] in case element doesn't exist in array + /** + * Finds the first and last occurrence indices of the key in a sorted array. + * + * @param nums sorted array of integers (non-decreasing order) + * @param key the target value to search for + * @return int array of size two where + * - index 0 is the first occurrence of key, + * - index 1 is the last occurrence of key, + * or [-1, -1] if the key does not exist in the array. + */ public static int[] sortedRange(int[] nums, int key) { - int[] range = new int[] {-1, -1}; - alteredBinSearchIter(nums, key, 0, nums.length - 1, range, true); - alteredBinSearchIter(nums, key, 0, nums.length - 1, range, false); + int[] range = new int[] { -1, -1 }; + alteredBinSearchIter(nums, key, 0, nums.length - 1, range, true); // find left boundary + alteredBinSearchIter(nums, key, 0, nums.length - 1, range, false); // find right boundary return range; } - // Recursive altered binary search which searches for leftmost as well as rightmost occurrence - // of 'key' + /** + * Recursive altered binary search to find either the leftmost or rightmost occurrence of a key. + * + * @param nums the sorted array + * @param key the target to find + * @param left current left bound in search + * @param right current right bound in search + * @param range array to update with boundaries: range[0] for leftmost, range[1] for rightmost + * @param goLeft if true, searches for leftmost occurrence; if false, for rightmost occurrence + */ public static void alteredBinSearch(int[] nums, int key, int left, int right, int[] range, boolean goLeft) { if (left > right) { return; } - int mid = (left + right) >>> 1; + int mid = left + ((right - left) >>> 1); if (nums[mid] > key) { alteredBinSearch(nums, key, left, mid - 1, range, goLeft); } else if (nums[mid] < key) { @@ -41,11 +62,19 @@ public static void alteredBinSearch(int[] nums, int key, int left, int right, in } } - // Iterative altered binary search which searches for leftmost as well as rightmost occurrence - // of 'key' + /** + * Iterative altered binary search to find either the leftmost or rightmost occurrence of a key. + * + * @param nums the sorted array + * @param key the target to find + * @param left initial left bound + * @param right initial right bound + * @param range array to update with boundaries: range[0] for leftmost, range[1] for rightmost + * @param goLeft if true, searches for leftmost occurrence; if false, for rightmost occurrence + */ public static void alteredBinSearchIter(int[] nums, int key, int left, int right, int[] range, boolean goLeft) { while (left <= right) { - final int mid = (left + right) >>> 1; + int mid = left + ((right - left) >>> 1); if (nums[mid] > key) { right = mid - 1; } else if (nums[mid] < key) { @@ -55,33 +84,48 @@ public static void alteredBinSearchIter(int[] nums, int key, int left, int right if (mid == 0 || nums[mid - 1] != key) { range[0] = mid; return; - } else { - right = mid - 1; } + right = mid - 1; } else { if (mid == nums.length - 1 || nums[mid + 1] != key) { range[1] = mid; return; - } else { - left = mid + 1; } + left = mid + 1; } } } } + /** + * Counts the number of elements strictly less than the given key. + * + * @param nums sorted array + * @param key the key to compare + * @return the count of elements less than the key + */ public static int getCountLessThan(int[] nums, int key) { return getLessThan(nums, key, 0, nums.length - 1); } + /** + * Helper method using binary search to count elements less than or equal to the key. + * + * @param nums sorted array + * @param key the key to compare + * @param left current left bound + * @param right current right bound + * @return count of elements less than or equal to the key + */ public static int getLessThan(int[] nums, int key, int left, int right) { int count = 0; while (left <= right) { - final int mid = (left + right) >>> 1; + int mid = left + ((right - left) >>> 1); if (nums[mid] > key) { right = mid - 1; - } else if (nums[mid] <= key) { - count = mid + 1; // At least mid+1 elements exist which are <= key + } else { + // nums[mid] <= key + count = mid + 1; // all elements from 0 to mid inclusive are <= key left = mid + 1; } } From 78f27ec121f6858d7d5db2c017095c5d48c9f11c Mon Sep 17 00:00:00 2001 From: alxkm Date: Wed, 9 Jul 2025 22:53:20 +0200 Subject: [PATCH 2/3] formatting: fix comment formatting issue --- src/main/java/com/thealgorithms/misc/RangeInSortedArray.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java b/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java index ff754b24ee7b..3e7b7bd94cbe 100644 --- a/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java +++ b/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java @@ -21,7 +21,7 @@ private RangeInSortedArray() { */ public static int[] sortedRange(int[] nums, int key) { int[] range = new int[] { -1, -1 }; - alteredBinSearchIter(nums, key, 0, nums.length - 1, range, true); // find left boundary + alteredBinSearchIter(nums, key, 0, nums.length - 1, range, true); // find left boundary alteredBinSearchIter(nums, key, 0, nums.length - 1, range, false); // find right boundary return range; } @@ -125,7 +125,7 @@ public static int getLessThan(int[] nums, int key, int left, int right) { right = mid - 1; } else { // nums[mid] <= key - count = mid + 1; // all elements from 0 to mid inclusive are <= key + count = mid + 1; // all elements from 0 to mid inclusive are <= key left = mid + 1; } } From 10488da0c76ab5cb877992d38ad23780540bb275 Mon Sep 17 00:00:00 2001 From: alxkm Date: Wed, 9 Jul 2025 22:54:39 +0200 Subject: [PATCH 3/3] formatting: fix array formatting issue --- src/main/java/com/thealgorithms/misc/RangeInSortedArray.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java b/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java index 3e7b7bd94cbe..0ae73dd73f09 100644 --- a/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java +++ b/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java @@ -20,7 +20,7 @@ private RangeInSortedArray() { * or [-1, -1] if the key does not exist in the array. */ public static int[] sortedRange(int[] nums, int key) { - int[] range = new int[] { -1, -1 }; + int[] range = new int[] {-1, -1}; alteredBinSearchIter(nums, key, 0, nums.length - 1, range, true); // find left boundary alteredBinSearchIter(nums, key, 0, nums.length - 1, range, false); // find right boundary return range;