<p>Given two integer arrays <code>nums1</code> and <code>nums2</code>, return <em>the maximum length of a subarray that appears in <strong>both</strong> arrays</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The repeated subarray with maximum length is [3,2,1].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The repeated subarray with maximum length is [0,0,0,0,0].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums1.length, nums2.length &lt;= 1000</code></li>
	<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 100</code></li>
</ul>

class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        static const std::int64_t mod = 100000007;
        static const std::int64_t base = 137;

        if(nums1.size() > nums2.size()){
            std::swap(nums1, nums2);
        }

        std::vector<std::int64_t> power(nums2.size()+1, 1);
        for(std::size_t i = 1; i < power.size(); i++){
            power[i] = (power[i-1] * base) % mod;
        }

        std::vector<std::int64_t> hash1(nums1.size(), nums1[0] + 1);
        for(std::size_t i = 1; i < nums1.size(); i++){
            hash1[i] = (hash1[i-1] + (power[i] * (nums1[i] + 1)) % mod) % mod;
        }

        std::vector<std::int64_t> hash2(nums2.size(), nums2[0] + 1);
        for(std::size_t i = 1; i < nums2.size(); i++){
            hash2[i] = (hash2[i-1] + (power[i] * (nums2[i] + 1)) % mod) % mod;
        }

        const auto getSubarrayHash = [&](const std::vector<std::int64_t>& hash, std::size_t start, std::size_t size){
            std::int64_t positionalHash;
            if(start == 0){
                positionalHash = hash[start + size - 1];
            }
            else{
                positionalHash = (hash[start + size - 1] - hash[start - 1]) % mod;
            }
            if(positionalHash < 0){
                positionalHash += mod;
            }

            return (positionalHash * power[power.size() - (start + size - 1) - 1]) % mod;
        };

        const auto hasEqualSubarray = [&](std::size_t size){
            std::unordered_set<std::int64_t> hashes;

            for(std::size_t i = 0; i < nums1.size() - size + 1; i++){
                hashes.insert(getSubarrayHash(hash1, i, size));    
            }

            for(std::size_t i = 0; i < nums2.size() - size + 1; i++){
                if(hashes.find(getSubarrayHash(hash2, i, size)) != hashes.end()){
                    return true;
                }
            }

            return false;
        };

        std::size_t l = 0, r = nums1.size() + 1;
        while(l + 1 < r){
            const std::size_t mid = (l + r) / 2;

            if(hasEqualSubarray(mid)){
                l = mid;
            }
            else{
                r = mid;
            }
        }

        return l;
    }
};
19318