Help me out in this



it is giving tle

@KartikKhariwal are u there ?

Yeah checking…

@Kartikkhariwal1 okay bro

Hey @shivammishra20121999
Complexity of distance() is O(n)
So ur complexity becomes O( n^2) hence giving tle for 1 case

@Kartikkhariwal1 then what should i do instead of that?

Here refer to this simple solution

class Solution {
public:
    vector<int> countSmaller(vector<int>& nums) {
        std::vector<int> sortedNums;
        std::vector<int> count (nums.size());
        
        for (long i = nums.size() - 1; i >= 0; i--)
        {
            auto itr = std::lower_bound(sortedNums.begin(), sortedNums.end(), nums[i]);
            count[i] = itr - sortedNums.begin();
            sortedNums.insert(itr, nums[i]);
        }
        
        return count;
    }
};

@Kartikkhariwal1
auto itr = std::lower_bound(sortedNums.begin(), sortedNums.end(), nums[i]); time complexirty iska kya haan bhai?

O(logn)…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.