java solution using prefix sum and binary search!
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
import java.util.*;

class Solution {

    public long maximumCoins(int[][] num, int k) {
        // Sorting the input 2D array based on the first element of each sub-array
        Arrays.sort(num, (a, b) -> Integer.compare(a[0], b[0]));

        int n = num.length;
        long ans = 0;

        // Preparing the x 2D array with same structure as the C++ code
        long[][] x = new long[n][3];
        for (int i = 0; i < n; i++) {
            x[i][0] = num[i][0];
            x[i][1] = num[i][1];
            x[i][2] = num[i][2];
        }

        // Arrays for prefix sums, left and right ranges
        long[] pref = new long[n];
        long[] l = new long[n];
        long[] r = new long[n];

        // Initialize prefix array
        pref[0] = (x[0][1] - x[0][0] + 1) * x[0][2];
        for (int i = 1; i < n; i++) {
            pref[i] = pref[i - 1] + (x[i][1] - x[i][0] + 1) * x[i][2];
        }

        // Fill left and right arrays
        for (int i = 0; i < n; i++) {
            l[i] = x[i][0];
            r[i] = x[i][1];
        }

        // First pass - checking the case where the end range is modified
        for (int i = 0; i < n; i++) {
            long run = 0;

            long c = r[i] - k + 1;
            if (c <= 0) continue;

            // Binary search for lower bound
            int lb = Arrays.binarySearch(r, c);
            if (lb < 0) lb = -(lb + 1);

            if (lb < n) {
                run = (r[lb] - Math.max(c, l[lb]) + 1) * x[lb][2] + pref[i] - pref[lb];
            }

            ans = Math.max(ans, run);
        }

        // Second pass - checking the case where the start range is modified
        for (int i = 0; i < n; i++) {
            long run = 0;

            long c = l[i] + k - 1;
            // Binary search for upper bound
            int lb = Arrays.binarySearch(l, c);
            if (lb < 0) lb = -(lb + 1) - 1;

            if (lb >= 0) {
                run = (Math.min(r[lb], c) - l[lb] + 1) * x[lb][2] + (lb > 0 ? pref[lb - 1] : 0) - (i > 0 ? pref[i - 1] : 0);
            }

            ans = Math.max(ans, run);
        }

        return ans;
    }
}

```
['Binary Search', 'Prefix Sum', 'Java']