Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Rank Transform of an Array

class Solution {
  public:
    vector<int> arrayRankTransform(vector<int> &arr);
};

vector<int> Solution::arrayRankTransform(vector<int> &a) {
    int n = a.size();

    map<int, int> rank;
    for (int i = 0; i < n; i++) {
        rank[a[i]] = 0;
    }

    int rank_now = 0;
    for (auto itr = rank.begin(); itr != rank.end(); itr++) {
        itr->second = ++rank_now;
    }

    vector<int> ans(n);
    for (int i = 0; i < n; i++) {
        ans[i] = rank[a[i]];
    }
    return ans;
}