Merge Overlapping intervals and Count || Easy C++ Code
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nHere, firstly we will s
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere, firstly we will sort the given vector of intervals.\nThen we will merge the overlapping intervals and will count the total number of intervals (mergesize) that will remain after merging.\n\nNow, we have to distribute every unoverlapped interval in 2 groups i.e. either in group 1 or in group 2 and we can do that by multiplying the answer with 2 every single we get an unoverlapped interval.\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    \n    int countWays(vector<vector<int>>&v) {\n        long long i=0,n=v.size(),mod=1e9+7;\n        sort(v.begin(),v.end());\n        int mergesize=1; // size=1 because we started to iterate from i=1 i.e. second interval.\n        long long ans=2; // distributing the first interval in 2 ways\n        //int first = v[0][0];\n        int second = v[0][1];\n        for(i=1;i<n;i++){\n            if(v[i][0]<=second)second = max(second,v[i][1]);\n            else{\n                mergesize++;\n                //first=v[i][0]; (incase,if u want to store the merged intervals)\n                second=v[i][1];\n                ans=(ans*2)%mod;  // distributing the current unoverlapped interval we got either in group 1 or in group 2.\n            }\n        }\n        return ans;\n    }\n};\n```
['Sorting', 'C++']