diff --git a/README.md b/README.md index 48882c8c..fd975252 100644 --- a/README.md +++ b/README.md @@ -1469,8 +1469,8 @@ In order to achieve greater coverage and encourage more people to contribute to - - + + diff --git a/src/java/LinearSearchRecursive.java b/src/java/LinearSearchRecursive.java new file mode 100644 index 00000000..1abd10ff --- /dev/null +++ b/src/java/LinearSearchRecursive.java @@ -0,0 +1,33 @@ +public class LinearSearchRecursive { + + public static int linearSearchRecursive(int[] arr, int n, int index, int target) { + // Base case: If index exceeds array bounds, return -1 indicating target is not found + if (index >= n) { + return -1; + } + + // If the current element matches the target, return the index + if (arr[index] == target) { + return index; + } + + // Recursive case: move to the next index in the array + return linearSearchRecursive(arr, n, index + 1, target); + } + + public static void main(String[] args) { + int[] arr = {10, 20, 60, 30, 60, 70, 80, 22}; + + int n = arr.length; + + int target = 70; + + int result = linearSearchRecursive(arr, n, 0, target); + + if (result >= 0) { + System.out.println("Target element found at index: " + result); + } else { + System.out.println("Target element not found."); + } + } +}