SEARCHINARR - Editorial

Problem Link - Search an element in an array in Arrays

Problem Statement:

You are given an array A of size N and an element X. Your task is to find whether the array A contains the element X or not.

Approach:

  • We iterate through the array and check whether any element in the array is equal to X.
  • If we find X in the array, we can stop the search and output “YES”.
  • If we complete the iteration without finding X, we output “NO”.

Complexity:

  • Time Complexity: O(N) Iterated through the whole array.
  • Space Complexity: O(1) No extra space is used.
1 Like

include <bits/stdc++.h>
using namespace std;

void searchEle(int arr, int n , int X){

for (int i=0; i<n; i++){
    if(arr[i] == X){
        cout<<"YES"<<endl;
        return;
    } 
}
cout<< "NO"<<endl;

}

int main() {

int X ,n;

cin >> n >> X;

int arr[n];
for(int i=0; i<n; i++){
cin >> arr[i];
}
searchEle(arr , n ,X);

return 0;
}