LC 1608. Special Array With X Elements Greater Than or Equal X

Nilanjan Deb · October 11, 2020

This is my CPP solution.

class Solution {
public:
    int specialArray(vector<int>& v) {
        int n = v.size();
        if(n==0)
            return 0;
        vector<int> arr;
        sort(v.begin(), v.end());
        for(int i = 0; i <= n; i++) {
            int lb = lower_bound(v.begin(), v.end(), i) - v.begin();
            if(n-lb == i) {
                return i;
            }
        }
        return -1;
    }
};


Dicussion Forum