<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) {
        // sol 2) binary search with rolling hash
        auto binary_power = [](long long base, long long exp, long long mod) {
            long long res = 1;
            base %= mod;
            while (exp > 0) {
                if (exp & 1) {
                    res = res * base % mod;
                }
                base = base * base % mod;
                exp >>= 1;
            }
            return res;
        };

        auto possible = [&](int k) {
            unordered_map<long long, vector<int> > seen;
            long long p = 103;
            long long mod = 1e9 + 7;
            long long hash = 0;

            for (int i = 0; i < nums1.size(); ++i) {
                hash = (hash * p + nums1[i]) % mod;
                if (i >= k) {
                    hash = (hash - nums1[i - k] * binary_power(p, k, mod) % mod + mod) % mod;
                }
                if (i >= k - 1) {
                    seen[hash].push_back(i);
                }
            }

            hash = 0;
            for (int i = 0; i < nums2.size(); ++i) {
                hash = (hash * p + nums2[i]) % mod;
                if (i >= k) {
                    hash = (hash - nums2[i - k] * binary_power(p, k, mod) % mod + mod) % mod;
                }
                if (i >= k - 1 && seen.count(hash)) {
                    for (auto pos: seen[hash]) {
                        if (equal(nums1.begin() + pos - k + 1, nums1.begin() + pos + 1, nums2.begin() + i - k + 1)) {
                            return true;
                        }
                    }
                }
            }
            return false;
        };

        // find the last True
        int lo = 0, hi = nums1.size();
        while (lo < hi) {
            int mid = lo + (hi - lo + 1) / 2;
            if (possible(mid)) {
                lo = mid;
            } else {
                hi = mid - 1;
            }
        }
        return lo;

        // // dp(i, j) := the length of longest common prefix in nums1[i:] and nums2[j:]
        // int N = nums1.size(), M = nums2.size();
        // vector<vector<int>> dp(N+1, vector<int>(M+1, 0));
        // int ans = 0;
        // for (int i = N-1; i >= 0; --i) {
        //     for (int j = M-1; j >= 0; --j) {
        //         if (nums1[i] == nums2[j]) {
        //             dp[i][j] = dp[i+1][j+1] + 1;
        //         } else {
        //             dp[i][j] = 0;
        //         }
        //         ans = max(ans, dp[i][j]);
        //     }
        // }
        // return ans;
    }
};
23083