Answer to Question #219993 in Java | JSP | JSF for Imran

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.


1
Expert's answer
2021-07-24T07:06:46-0400
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");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS