A network tool stores a packet stream as 31-bit integers. A checksum over a run of consecutive packets is the XOR of their values. Given a target checksum k, the tool needs to know how many runs match it, and how short a matching run can be.
A subarray is a run of one or more consecutive elements. Write two functions that take an array nums and a target k:
countSubarraysWithXor(nums, k) returns how many subarrays have XOR exactly k. Subarrays at different positions count separately.
shortestSubarrayWithXor(nums, k) returns the length of the shortest subarray with XOR k, or 0 if there is none.
Rules:
nums is an array of integers from 0 to 2³¹ - 1, and k is in the same range. Otherwise throw a TypeError.
An empty array returns 0 from both functions.
Arrays can hold 200,000 values. Checking every start and end is about 20 billion subarrays and will not finish in time.