You are given a 2D integer array tiles where tiles[i] = [li, ri] represents that every tile j in the range li &lt;= j &lt;= ri is colored white.

You are also given an integer carpetLen, the length of a single carpet that can be placed anywhere.

Return the maximum number of white tiles that can be covered by the carpet.

&nbsp;
Example 1:

Input: tiles = [[1,5],[10,11],[12,18],[20,25],[30,32]], carpetLen = 10
Output: 9
Explanation: Place the carpet starting on tile 10. 
It covers 9 white tiles, so we return 9.
Note that there may be other places where the carpet covers 9 white tiles.
It can be shown that the carpet cannot cover more than 9 white tiles.


Example 2:

Input: tiles = [[10,11],[1,1]], carpetLen = 2
Output: 2
Explanation: Place the carpet starting on tile 10. 
It covers 2 white tiles, so we return 2.


&nbsp;
Constraints:


	1 &lt;= tiles.length &lt;= 5 * 104
	tiles[i].length == 2
	1 &lt;= li &lt;= ri &lt;= 109
	1 &lt;= carpetLen &lt;= 109
	The tiles are non-overlapping.


class Solution:
    def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int:
        tiles.sort()
		#j: window index
        j = cover = res = 0
        for i in range(len(tiles)):
			#slide the window as far as we can to cover fully the intervals with the carpet
            while j<len(tiles) and tiles[j][1]-tiles[i][0] + 1 <= carpetLen:
                cover += tiles[j][1]-tiles[j][0] + 1
                j += 1
			#process the remnant, that is, when the tiles[j] is covered by the carpet partially(not fully)
            if j<len(tiles) and tiles[j][0]-tiles[i][0] + 1 <= carpetLen:
                res = max(res, cover + carpetLen-(tiles[j][0]-tiles[i][0]))
            else:
                res = max(res, cover)
            #when the tiles[j] is covered partially, the interval is not added to the variable cover
			if i!=j:
                cover -= tiles[i][1]-tiles[i][0]+1
			j = max(j, i+1)
        return res
class Solution
{
    public int maximumWhiteTiles(int[][] tiles, int carpetLen)
    {
        Arrays.sort(tiles,(a,b)->{return a[0]-b[0];});
        int x = 0;
        int y = 0;
        long maxCount = 0;
        long count = 0;
        
        while(y < tiles.length && x <= y)
        {
            long start = tiles[x][0];
            long end = tiles[y][1];
            
            if(end-start+1 <= carpetLen) 
            {
                count += tiles[y][1] - tiles[y][0]+1;
                maxCount = Math.max(maxCount,count);
                y++;
            }
            else 
            {
                long midDist = start+carpetLen-1;
                long s = tiles[y][0];
                long e = tiles[y][1];
                if(midDist <= e && midDist >= s)  maxCount = Math.max(maxCount,count+midDist-s+1);
                count -= tiles[x][1] - tiles[x][0] + 1;
                x++;
            }
        }
        return (int)maxCount;
    }
}
class Solution {
public:
    int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen) {
        long long n = tiles.size() , ans = INT_MIN;
        sort(tiles.begin(),tiles.end());
        vector<long long> len(n) , li(n);
		//len array stores the prefix sum of tiles
		//li array stores the last index tiles[i]
        for(int i=0;i<n;i++){
            len[i] = (long long)(tiles[i][1] - tiles[i][0] + 1);
            len[i]+=(i==0) ? 0 : len[i-1];
            li[i] = tiles[i][1];
        }
        
        for(int i=0 ; i<n ; i++){
		   //sp means starting tile index
		   //ep means ending tile index
            long long sp = tiles[i][0] , ep = tiles[i][0] + (long long)carpetLen-1 , tc=0;
            int idx = lower_bound(li.begin(),li.end(),ep) - li.begin();
			
			//logic to take count of tiles covered
            if(idx==n){
                tc = len[n-1];
                tc-=(i==0) ? 0 : len[i-1];
            }else{
                tc = ep<tiles[idx][0] ? 0 : ep - (long long)tiles[idx][0]  + 1;
                idx--;
                if(idx>=0){
                    tc+=len[idx];
                    tc-=(i==0) ? 0 : len[i-1];
                }
            }
            ans = max(ans,tc);
        }
        return (int)ans;
    }
};
/**
 * @param {number[][]} tiles
 * @param {number} carpetLen
 * @return {number}
 */
var maximumWhiteTiles = function(tiles, carpetLen) {
    const sorted = tiles.sort((a, b) => a[0]-b[0])
    let res = 0

    let total = 0
    let right = 0

    for (let tile of sorted){
        const start = tile[0]
        const end = start + carpetLen - 1
        while(right < sorted.length && tiles[right][1] < end){
            total += tiles[right][1] - tiles[right][0] + 1
            right+=1
        }
        if(right === sorted.length || sorted[right][0] > end){
            res = Math.max(res, total)
        } else{
            res = Math.max(res, total + (end-tiles[right][0] + 1))
        }
        total -= tile[1] - tile[0] + 1
    }

    return res
};
Maximum White Tiles Covered by a Carpet