Untitled

mail@pastecode.io avatar
unknown
java
8 days ago
1.7 kB
2
Indexable
Never
import java.util.*;

public class Solution {

    public static int get_ans(int N, List<Integer> P, List<Integer> A) {
        int maxGoodPathLength = 1;
        int[] xorPrefix = new int[N];
        xorPrefix[0] = A.get(0);

        for (int i = 1; i < N; i++) {
            xorPrefix[i] = xorPrefix[i - 1] ^ A.get(i);
        }

        Map<Integer, Integer> xorIndexMap = new HashMap<>();
        xorIndexMap.put(0, -1);

        for (int i = 0; i < N; i++) {
            if (xorIndexMap.containsKey(xorPrefix[i])) {
                maxGoodPathLength = Math.max(maxGoodPathLength, i - xorIndexMap.get(xorPrefix[i]));
            } else {
                xorIndexMap.put(xorPrefix[i], i);
            }
        }

        // Additional validation to ensure we're considering valid paths
        int maxLength = 0;
        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                if ((xorPrefix[j] ^ xorPrefix[i]) < A.get(j)) {
                    maxLength = Math.max(maxLength, j - i);
                }
            }
        }
        
        return maxLength;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int N = Integer.parseInt(scan.nextLine().trim());

        List<Integer> P = new ArrayList<>(N);
        for (int j = 0; j < N; j++) {
            P.add(Integer.parseInt(scan.nextLine().trim()));
        }

        List<Integer> A = new ArrayList<>(N);
        for (int j = 0; j < N; j++) {
            A.add(Integer.parseInt(scan.nextLine().trim()));
        }

        int result = get_ans(N, P, A);
        System.out.println(result);
    }
}
Leave a Comment