« 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_MIN11#include <unordered_map>12using namespace std;1314bool checkNumber(int input[], int size, int x)15{16 if(size == 1){17 return false;18 }1920 if(input[0] == x){21 return true;22 }2324 return checkNumber(input+1, size-1, x);25}2627int main()28{29 int n;30 cin >> n;3132 int *input = new int[n];3334 for (int i = 0; i < n; i++)35 {36 cin >> input[i];37 }3839 int x;4041 cin >> x;4243 if (checkNumber(input, n, x))44 {45 cout << "true" << endl;46 }47 else48 {49 cout << "false" << endl;50 }51 return 0;52}