Lower Bound-STL

Sort by

recency

|

426 Discussions

|

  • + 0 comments
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        
        //taking input
        int n{};
        cin >> n;
        vector<int> v;
        for (int i{0}; i < n; ++i)
        {
            int t{};
            cin >> t;
            v.push_back(t);
        }
        // s is number of queries
        int s;
        cin >> s;
        // foreach query, get the value and print necessary value
        for (int i{0}; i < s; ++i)
        {
            int num{}; cin >> num;
            // lower bound returns an iterator type variable
            vector<int>::iterator x;
            x = lower_bound(v.begin(), v.end(), num);
            // getting the index of the lowerbound in the vector
            int pos = x - v.begin(); 
            // if value of lb = the query num, then yes it exists
            // in the vector exists
            if (v[pos] == num) cout << "Yes "<< pos+1;
            else cout << "No "<< pos+1;
            cout << endl;
        }
        
        return 0;
    }
    
  • + 0 comments
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
         int n;
        cin >> n;
    
        vector<int> v(n);
        for (int i = 0; i < n; i++) {
            cin >> v[i];
        }
    
        int q;
        cin >> q; 
    
        for (int i = 0; i < q; i++) {
            int x;
            cin >> x; 
    
            vector<int>::iterator low = lower_bound(v.begin(), v.end(), x);
    
            if (v[low - v.begin()] == x) {
                cout << "Yes " << (low - v.begin() + 1) << endl; 
            } else {
                cout << "No " << (low - v.begin() + 1) << endl; 
            }
        }
        return 0;
    }
    
  • + 0 comments
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        int n, n1;
    
        vector<int> vec;
        
        std::cin >> n;
        
        for (int i=0; i<n; i++)
        {
            std::cin >> n1;
    
            vec.push_back(n1);
        }
        
        std::cin >> n;
    
        for (int i=0; i<n; i++)
        {
            std::cin >> n1;
            
            auto result = std::lower_bound(vec.begin(), vec.end(), n1);
            
            std::cout << (*result == n1 ? "Yes " : "No ") << (result - vec.begin() + 1) << endl;
        }
        
        return 0;
    }
    
  • + 0 comments
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
        vector<int> arr(n);
        for (int i = 0; i < n; ++i) {
            cin >> arr[i];
        }
        int q;
        cin >> q;
        for (int i = 0; i < q; ++i) {
            int x;
            cin >> x;
            auto it = lower_bound(arr.begin(), arr.end(), x);
            if (*it == x) {
                cout << "Yes " << it - arr.begin() + 1 << endl;
            } else {
                cout << "No " << it - arr.begin() + 1 << endl;
            }
        }
        return 0;
    }
    
  • + 1 comment

    Solving the problem itself was easy, but testing result said:

    Time limit exceeded Your code did not execute within the time limits. Please optimize your code. For more information on execution time limits, refer to the environment page

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
        int n, num, numOfQ, q, targetIndex;
        cin >> n;
        vector<int> ints;
        for(int i = 0; i < n; i++){
            cin >> num;
            ints.push_back(num);
        }
        
        cin >> numOfQ;
        
        for(int i = 0; i < numOfQ; i++){
            cin >> q;
            for(int j = 0; j < n; j++){
                if(q == ints[j]){
                    targetIndex = j;
                    cout << "Yes " << targetIndex + 1 << endl;
                    break;
                }else if(ints[j] > q){
                    targetIndex = j;
                    cout << "No " << targetIndex + 1 << endl;
                    break;
                }
            }
        }
        return 0;
    }