Solution based on counting Overlap, Not Overlap intervals 🔥
Firstly, the ranges array should be sorted so that we can easily compare each interval with the others. Then, we can start counting the number of overlapping an
Firstly, the ranges array should be sorted so that we can easily compare each interval with the others. Then, we can start counting the number of overlapping and non-overlapping intervals in the range.\n\nFor overlapping intervals, we can group them together and keep them in either Group 1 or Group 2. It doesn\'t matter which group they are in, as long as they are in one of them. Therefore, we only need to count the number of overlapping intervals and multiply it by 2 to get the total contribution to the answer.\n\nFor non-overlapping intervals, we can split them into two groups as they can be placed in either Group 1 or Group 2. Therefore, for each non-overlapping interval, we need to multiply the answer by 2 twice (once for each group) to get the total contribution to the answer.\n\nPosting the solution in cpp as well as java. \n\n# Code\n```cpp\nclass Solution {\npublic:\n    const long long int MOD = 1e9 + 7;\n\n    int countWays(vector<vector<int>>& ranges) \n    {\n        long long int ans = 1;\n        int size = ranges.size();\n        \n        if (size == 1) return 2;\n        \n        sort(ranges.begin(), ranges.end());\n        \n        int overlap = 0, notOverlap = 0;\n        int tillMax = INT_MIN;\n        \n        for (int idx = 1; idx < ranges.size(); idx++)\n        {\n            tillMax = max(tillMax, ranges[idx - 1][1]);\n            \n            if (ranges[idx][0] <= ranges[idx - 1][1] or ranges[idx][0] <= tillMax) overlap++;\n            else notOverlap++; \n        }\n        \n        if (overlap) ans *= 2;\n        else notOverlap++; // for 0 based indexing as we know all pairs are non overlapping\n        \n        for (int idx = 1; idx <= notOverlap; idx++) {\n            ans = ((ans * 2) % (long)MOD);\n        }\n        \n        return ans;\n    }\n};\n```\n\n```java\nclass Solution {\n    public int countWays(int[][] ranges) \n    {\n        final long MOD = 1000000007;\n        long ans = 1, size = ranges.length;\n        \n        if (size == 1) return 2;\n        \n        Arrays.sort(ranges,(int[] o1, int[] o2) -> o1[0] - o2[0]);\n\n        int overlap = 0, notOverlap = 0;\n        int tillMax = Integer.MIN_VALUE;\n        \n        for (int idx = 1; idx < size; idx++)\n        {\n            tillMax = Math.max(tillMax , ranges[idx-1][1]);\n            \n            if (ranges[idx][0] <=ranges[idx-1][1] || ranges[idx][0] <= tillMax) overlap++;\n            else notOverlap++;\n        }\n        \n        if (overlap > 0) ans *= 2;\n        else notOverlap++;\n        \n        for (int idx = 1; idx <= notOverlap; idx++) {\n            ans = ((ans * 2) % (long)MOD);\n        }\n        \n        return (int)ans;\n    }\n}\n```\n\nTime Complexity: O(N * log N)\nSpace Complexity : O(1)\n\nFeel free to ask your queries. Do upvote, it motivates me to write more.
['C++', 'Java']