Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Count of Smaller Numbers After Self.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@


/* Approach 1: Using merge-sort technique */
void merge(vector<pair<int ,int > > &v,int l,int mid,int r,vector<int > &ans){
int i=l,j=mid,k=0;
vector<pair<int ,int > > temp(r-l+1);
Expand Down Expand Up @@ -46,3 +48,57 @@ class Solution {
return ans;
}
};



/* Approach 2: Using Policy Based Data Structure technique */
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace __gnu_pbds;
using namespace std;


typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;


class Solution {
public:

void myerase(pbds &t, int v){

int rank = t.order_of_key(v);
pbds::iterator it = t.find_by_order(rank);
t.erase(it);
}
vector<int> countSmaller(vector<int>& nums) {

int n = nums.size();
pbds st;
for(int i = n - 1 ; i >= 0 ; i--)
{
st.insert(nums[i]);
}

vector<int> count(n);
for(int i = 0 ; i < n ; i++)
{

myerase(st, nums[i]);

auto it = st.upper_bound(nums[i]);
if(it == st.end())
count[i] = st.size();

else
count[i] = st.order_of_key(*it);

if(count[i] < 0)
count[i] = 0;
}

return count;

}
};