Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerEarth Discover the Monk problem solution

YASH PAL, 31 July 2024
In this HackerEarth Discover the Monk problem solution You are given an array A of size N, and Q queries to deal with. For each query, you are given an integer X, and you’re supposed to find out if X is present in the array A or not.
HackerEarth Discover the Monk problem solution

HackerEarth Discover the Monk problem solution.

#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>

using namespace std;
vector <long long > v;

int main()
{
int n, q;
long long x;
cin >> n >> q;
assert(1 <= n and n <= 100000);
assert(1 <= q and q <= 100000);
for(int i = 0;i < n;++i)
{
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
string s;
for(int i = 0;i < q;++i)
{
cin >> x;
assert(1 <= x and x <= 1000000000LL);
if(binary_search(v.begin(), v.end(), x))
s = "YES";
else
s = "NO";
cout << s << endl;
}
}

Second solution

#include <bits/stdc++.h>
using namespace std;
int main()
{
int N,Q;
cin>>N>>Q;
int a[N];
for(int i=0;i<N;i++)
cin>>a[i];
sort(A,A+N);
for(int i=0;i<Q;i++)
{
int X;
cin>>X;
bool flag=false;
int lb=0,ub=N-1;
while(lb<=ub)
{
int mid=(lb+ub)/2;
if(a[mid]>X)
ub=mid-1;
else if(a[mid]<X)
lb=mid+1;
else
{
flag=true;
break;
}
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes