Question #219993

Two friends Jameel and Uday went to Games Zone in shopping mall. They have 15 games

in their hand in random order. They have given 10 seconds to find the game in the given

list. Write a program to search a game in the given list using recursion method.


Expert's answer

public class Main {
    static int searchGame(int arr[], int l, int r,
                               int x)
    {
        if (r < l)
            return -1;
 
        if (arr[l] == x)
            return l;
 
        if (arr[r] == x)
            return r;
        return searchGame(arr, l + 1, r - 1, x);
    }
    public static void main(String[] args)
    {
        int x = 11;
        int arr[] = new int[] { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        int index= searchGame(arr, 0, arr.length - 1, x);
        if (index != -1)
            System.out.println("Element " + x
                               + " is present at index "
                               + index);
        else
            System.out.println("Element " + x
                               + " is not present");
    }
}
LATEST TUTORIALS
APPROVED BY CLIENTS