import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        int[] A = {0, 1, 2, 3}; // Using 1-based indexing layout
        int[] B = {0, 2, 1, 3};
        int n = 3;

        int[] prefixA = new int[n + 1];
        int[] prefixB = new int[n + 1];

        // Step 1: Build prefix sums
        for (int i = 1; i <= n; i++) {
            prefixA[i] = prefixA[i - 1] + A[i];
            prefixB[i] = prefixB[i - 1] + B[i];
        }
        //Here, instead of looking for a constant K, we are looking for a subarray where Sum of A = Sum of B.
        //If you subtract the sum of B from the sum of A, you get:Sum of A - Sum of B = 0
        // Step 2: Use HashMap to track first occurrence of (prefixA[k] - prefixB[k])
        HashMap<Integer, Integer> firstOccurrence = new HashMap<>();
        int maxG = 0;

        for (int j = 0; j <= n; j++) {
            int diff = prefixA[j] - prefixB[j];

            if (firstOccurrence.containsKey(diff)) {
                int prevIndex = firstOccurrence.get(diff);
                int currentLength = j - prevIndex;
                maxG = Math.max(maxG, currentLength);
            } else {
                // Store the earliest index to maximize length
                firstOccurrence.put(diff, j);
            }
        }

        System.out.println("Max length G: " + maxG);
    }
}