Solution :

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
 
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,q;
    cin>>n>>q;
    ll a[n];
    for(ll i=0;i<n;i++)
    cin>>a[i];
    sort(a,a+n);
    while(q--)
    {
        ll key;
        cin>>key;
        ll low=0;
        ll high =n-1;
        ll flag=0;
        while(low<=high)
        {
           ll mid = (low + high) /2;
           if(a[mid]<key)
           {
             low=mid+1;
           }
           else if(a[mid]>key)
           {
              high=mid-1;
           }
          else
            { flag=1;
            break;
        }
        }
        if(flag==1)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
         
    }
    return 0;
}

Problem Name: Discover the Monk

The "Discover the Monk" problem involves checking if a given element exists in an array for multiple queries. For each query, you are given an integer XX and you need to determine if XX exists in the array. The goal is to do this efficiently due to the constraints.

Problem Breakdown

Input Details:

  1. N: The size of the array AA.
  2. Q: The number of queries.
  3. The second line contains NN integers representing the elements of the array AA.
  4. The next QQ lines each contain a single integer XX, which is the target number you need to search for in the array AA.

Output:

  • For each query, you must print "YES" if XX exists in the array, and "NO" if it doesn't.

Constraints

  • 1N,Q1051 \leq N, Q \leq 10^5
  • 1A[i],X1091 \leq A[i], X \leq 10^9

Solution Strategy

Given the constraints (up to 10510^5 elements in the array and 10510^5 queries), a brute-force linear search for each query would be inefficient. Instead, you can:

  1. Sort the array.
  2. Use binary search for each query to check if the element XX is present.

Binary search is efficient because it runs in O(logN)O(\log N), and sorting the array takes O(NlogN)O(N \log N). For each query, binary search can find if the number exists in O(logN)O(\log N), making the approach much faster than a linear search.

Step-by-Step Explanation

  1. Input Parsing:

    • Read NN (size of the array) and QQ (number of queries).
    • Read the array AA.
    • Sort the array AA to allow for binary search.
  2. Binary Search:

    • For each query, use binary search to check if the queried element XX exists in AA. This is done using a simple iterative approach or functions like std::binary_search in C++.
  3. Output:

    • If the element is found during the binary search, output "YES".
    • Otherwise, output "NO".

Code Walkthrough


#include<bits/stdc++.h> using namespace std; #define ll long long int int main() { ios_base::sync_with_stdio(false); // To optimize I/O cin.tie(NULL); // Unnecessary synchronization with C-style I/O is disabled ll n, q; // n: size of array, q: number of queries cin >> n >> q; ll a[n]; // Array to hold elements for(ll i = 0; i < n; i++) { cin >> a[i]; // Read array elements } // Sorting the array to prepare for binary search sort(a, a + n); // Processing each query while(q--) { ll key; // Query number (key) cin >> key; // Binary search algorithm ll low = 0; ll high = n - 1; ll flag = 0; // Flag to indicate if the key was found // Perform binary search while(low <= high) { ll mid = (low + high) / 2; if(a[mid] < key) { low = mid + 1; // Search in the right half } else if(a[mid] > key) { high = mid - 1; // Search in the left half } else { flag = 1; // Key found break; } } // Output the result for the current query if(flag == 1) cout << "YES" << endl; else cout << "NO" << endl; } return 0; // Return success }

Detailed Explanation of Code

  1. Input Parsing and Sorting:

    • The input begins by reading nn (the number of elements in the array) and qq (the number of queries).
    • The array AA is then read and sorted in ascending order to facilitate binary search.
  2. Binary Search:

    • For each query, the code reads the value XX (the target) and performs a binary search within the sorted array.
    • The binary search works by comparing the middle element with the target. If the middle element is smaller than the target, we move to the right half of the array. If it's larger, we move to the left half. If the target is found, we set a flag and break the loop.
  3. Output:

    • If the flag is set, indicating that the target was found, the program prints "YES".
    • Otherwise, it prints "NO".

Time Complexity Analysis

  • Sorting the array: O(NlogN)O(N \log N).
  • Binary search for each query: O(logN)O(\log N). For QQ queries, this becomes O(QlogN)O(Q \log N).

Thus, the total time complexity of the solution is O(NlogN+QlogN)O(N \log N + Q \log N), which is efficient enough given the problem constraints.

Example Walkthrough

Test Case 1:


Input: 4 2 20 30 40 10 10 20 Output: YES YES
  • The array A=[20,30,40,10]A = [20, 30, 40, 10] is first sorted to become [10,20,30,40][10, 20, 30, 40].
  • For the first query, X=10X = 10, which is found in the sorted array, so the output is "YES".
  • For the second query, X=20X = 20, which is also found, so the output is "YES".

Test Case 2:


Input: 5 10 50 40 30 20 10 10 20 30 40 50 60 70 80 90 100 Output: YES YES YES YES YES NO NO NO NO NO
  • The sorted array is [10,20,30,40,50][10, 20, 30, 40, 50].
  • The first five queries match elements in the array, so the outputs are "YES".
  • The remaining queries do not match any elements in the array, so the outputs are "NO".

This problem showcases efficient searching using sorting and binary search, leveraging time complexity improvements to handle large inputs effectively.