Add tests, remove main in RangeInSortedArray (#5778)

This commit is contained in:
Hardik Pawar
2024-10-14 16:18:55 +05:30
committed by GitHub
parent 9c76b30271
commit e19378d56c
3 changed files with 36 additions and 9 deletions

View File

@ -1,18 +1,9 @@
package com.thealgorithms.misc;
import java.util.Arrays;
public final class RangeInSortedArray {
private RangeInSortedArray() {
}
public static void main(String[] args) {
// Testcases
assert Arrays.equals(sortedRange(new int[] {1, 2, 3, 3, 3, 4, 5}, 3), new int[] {2, 4});
assert Arrays.equals(sortedRange(new int[] {1, 2, 3, 3, 3, 4, 5}, 4), new int[] {5, 5});
assert Arrays.equals(sortedRange(new int[] {0, 1, 2}, 3), new int[] {-1, -1});
}
// 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
public static int[] sortedRange(int[] nums, int key) {