Java Program To Recursively Linearly Search An Element In An Array

Last Updated : 18 Aug, 2026

Recursive Linear Search is a searching technique in which an element is searched in an array using recursion. The method checks elements recursively until the required element is found or the entire array has been searched.

  • Searches elements sequentially.
  • Works with sorted and unsorted arrays.

Illustration

Given an array arr[] and an element x, the program returns the index of x if it is present. Otherwise, it returns -1.

Input : arr[] = {25, 60, 18, 3, 10}, Element to be searched x = 3
Output : 3 (index )

Input : arr[] = {10,20,30,24,15,40}, Element to be searched x = 3
Output : -1
For x = 35
Element x is not present in arr[]

  • Start with the first index of the array.
  • Check whether the current element is equal to the search element.
  • If a match is found, return its index.
  • If the current index reaches the end of the array, return -1.
  • Otherwise, recursively search the next element.
  • Stop when the element is found or the array has been completely searched.
Java
// Main class
public class Geeks
{
    // Method 1
    // Recursive method to search for an element and
    // its index in the array
    static int recursiveSearch(int arr[], int l, int r,
                               int x)
    {
        // if r<l,it means that element is not present in
        // the array
        if (r < l)
            return -1;

        if (arr[l] == x)
            return l;

        if (arr[r] == x)
            return r;

        // Since element has not found on both left most and
        // rightmost boundary,ie at l and r, now recursive the
        // array to find position of x.
        return recursiveSearch(arr, l + 1, r - 1, x);
    }

    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Element to be searched for
        // Custom input number
        int x = 3;

        // Declaring and initializing the integer array
        int arr[] = new int[] { 25, 60, 18, 3, 10 };

        // Calling the above recursive method method to
        // search for the element in the array

        // Recursive function is called over array to
        // get the index of the element present in an array
        int index
        = recursiveSearch(arr, 0, arr.length - 1, x);

        // If index is found means element exists
        if (index != -1)

            // Print the element and its index
            System.out.println("Element " + x
                               + " is present at index "
                               + index);

        // If we hit else case means
        // element is not present in the array
        else

            // Simply display the corresponding element
            // is not present
            System.out.println("Element " + x
                               + " is not present");
    }
}

Output
Element 3 is present at index 3

Explanation: The method starts searching from index 0. It compares each element with 3. When it reaches index 3, the value matches 3, so the method returns 3.

Comment