Lower Bound-STL

  • + 0 comments

    This one worked for me.

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int N{0}; cin >> N;

    vector<int>vec;
    vec.reserve(N);
    
    int num;
    for(int i{0}; i<N; ++i){
        cin >> num;       
        vec.push_back(num);        
    }
    
    int Q{0};
    cin >> Q;
    vector<int>query;
    query.reserve(Q);
    
    for(int i{0}; i<Q; ++i){
        cin >> num;       
        query.push_back(num);        
    }
    
    int Y{0}, index{0};    
    for(int i{0}; i<Q; ++i){
        Y = query.at(i);
    
        auto it = lower_bound(vec.begin(), vec.end(), Y);
        index = distance(vec.begin(), it);
    
        if(vec[index] == Y)
            cout << "Yes " << (index + 1) << endl; 
        else
             cout << "No " << (index + 1) << endl; 
    }
    
    return 0;
    

    }