Knowee
Questions
Features
Study Tools

Given an array of length N and an integer x, you need to find if x is present in the array or not. Return true or false.Do this recursively.

Question

Given an array of length N and an integer x, you need to find if x is present in the array or not. Return true or false.Do this recursively.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step solution in Python:

  1. Define a function, say is_present(arr, x, n), where arr is the array, x is the integer to be found, and n is the length of the array.

  2. The base case of the recursion would be when n is 0. If n is 0, it means we have traversed the entire array and did not find x. So, in this case, return False.

  3. Now, check if the last element of the array is x. If it is, return True.

  4. If the last element is not x, then call the function is_present recursively with the same array, same x, but with n-1 as the length.

  5. The function will keep checking the last element of the array and reducing the length by 1 until it either finds x or the length becomes 0.

Here is the Python code for the above steps:

def is_present(arr, x, n):
    if n == 0:
        return False
    if arr[n-1] == x:
        return True
    return is_present(arr, x, n-1)

You can call this function with your array, the integer to be found, and the length of the array. For example:

print(is_present([1, 2, 3, 4, 5], 3, 5))  # Returns: True
print(is_present([1, 2, 3, 4, 5], 6, 5))  # Returns: False

This problem has been solved

Similar Questions

Given an integer array and another integer element. The task is to find if the given element is present in array or not.

Given an array of length N, you need to find and return the sum of all elements of the array.Do this recursively.Detailed explanation

Given an array, check if there exist 2 elements of the array such that their sum is equal to the sum of the remaining elements of the array.

Problem Statement You are given a function, int ElementsAndIndices(int arr[], int n); The function takes an integer array 'arr' of size 'n' as its arguments. Implement the function to find and return the number of array elements which are equal to their index value in array i.e. arr[k] = k, 0 <= k < n. Note: Indexing starts from 0. Return -1 if 'arr' is empty or None in case of python Example: Input: 10 1 12 3 5 8 9 7 12 23 Output: 3 Explanation: Index Element 0 10 1 1 2 12 3 3 4 5 5 8 6 9 7 7 8 12 9 23 Elements at index {1, 3, 7} are equal to their index values {1, 3, 7} respectively. Since, these are 3 elements, thus, output is 3. The custom input format for the above case: 10 10 1 12 3 5 8 9 7 12 23 (The first line represents the size of the array, the second line represents the elements of the array) Sample input -3 0 1 3 5 7 Sample Output 1 The custom input format for the above case: 6 -3 0 1 3 5 7 (The first line represents the size of the array, the second line represents the elements of the array) Instructions : This is a template based question, DO NOT write the "main" function. Your code is judged by an automated system, do not write any additional welcome/greeting messages. "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring. Additional score will be given for writing optimized code both in terms of memory and execution time.

You are given an integer array nums of size n and a positive integer k.Divide the array into one or more arrays of size 3 satisfying the following conditions:Each element of nums should be in exactly one array.The difference between any two elements in one array is less than or equal to k.Return a 2D array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.