« Find if x is present in the array or not.

Problem:

Find if x is present in the array or not

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.

Input Format :

Line 1 : An Integer N i.e. size of array

Line 2 : N integers which are elements of the array, separated by spaces

Line 3 : Integer x

Output Format :

'true' or 'false'

Constraints :

1 <= N <= 10^3

Sample Input 1 :

3 9 8 10 8

Sample Output 1 :

true

Sample Input 2 :

3 9 8 10 2

Sample Output 2 :

false

Solution

1#include <iostream>
2#include <iomanip>
3#include <algorithm>
4#include <string>
5#include <cstring>
6#include <vector>
7#include <cmath>
8#include <map>
9#include <climits>
10// climits for INT_MIN
11#include <unordered_map>
12using namespace std;
13
14bool checkNumber(int input[], int size, int x)
15{
16 if(size == 1){
17 return false;
18 }
19
20 if(input[0] == x){
21 return true;
22 }
23
24 return checkNumber(input+1, size-1, x);
25}
26
27int main()
28{
29 int n;
30 cin >> n;
31
32 int *input = new int[n];
33
34 for (int i = 0; i < n; i++)
35 {
36 cin >> input[i];
37 }
38
39 int x;
40
41 cin >> x;
42
43 if (checkNumber(input, n, x))
44 {
45 cout << "true" << endl;
46 }
47 else
48 {
49 cout << "false" << endl;
50 }
51 return 0;
52}