Sorting | Merging O(nlogn) Time Complexity
Intuition\n- We merge the intervals after sorting the ranges.\n- Then get the combinations after merging all the mergable ranges.\n\n# Complexity\n- Time comple
# Intuition\n- We merge the intervals after **sorting the ranges**.\n- Then get the combinations after merging all the mergable ranges.\n\n# Complexity\n- Time complexity: $$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int mod = 1e9+7;\n\n    int power(long long x, unsigned int y, int p) \n    { \n        int res = 1;     // Initialize result     \n        while (y > 0) \n        { \n            // If y is odd, multiply x with result \n            if (y & 1) \n                res = (res*x) % p; \n    \n            // y must be even now \n            y = y>>1; // y = y/2 \n            x = (x*x) % p; \n        } \n        return res; \n    } \n\n    bool isMerged(vector<int> &int1, vector<int> &int2) {\n        //min point of interval2 in interval 1\n        if(int1[0] <= int2[0] && int2[0] <= int1[1]) return true;\n        //max point of interval2 in interval 1\n        if(int1[0] <= int2[1] && int2[1] <= int1[1]) return true;\n        //min point of interval1 in interval 2\n        if(int2[0] <= int1[0] && int1[0] <= int2[1]) return true;\n        //max point of interval1 in interval 2\n        if(int2[0] <= int1[1] && int1[1] <= int2[1]) return true;\n        return false;\n    }\n    \n    int countWays(vector<vector<int>>& ranges) {\n        vector<vector<int>> res;\n        int n = ranges.size();\n        sort(ranges.begin(),ranges.end());\n        int i = 0,j = 0;\n        while(i < n) {\n            int j = i+1;\n            while(j < n && isMerged(ranges[i],ranges[j])) {\n                ranges[i][0] = min(ranges[i][0],ranges[j][0]);\n                ranges[i][1] = max(ranges[i][1],ranges[j][1]);\n                j++;\n            }\n            res.push_back(ranges[i]);\n            i = j;\n        }\n        return power(2,res.size(),mod);\n    }\n};\n```
['C++']