<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,1], k = 3
<strong>Output:</strong> true
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,0,1,1], k = 1
<strong>Output:</strong> true
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,1,2,3], k = 2
<strong>Output:</strong> false
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        const size_t n = nums.size();
        const int HashBits = log2(n) + 3;
        const int HashSize = 1 << HashBits;
        const int HashMask = (1 << HashBits) - 1;
        vector<bool> bloom(HashSize, false);
        for (int i = 0; i < nums.size(); ++i) {
            int x = nums[i];
            int h1 = hash1(x) & HashMask;
            int h2 = hash2(x) & HashMask;
            if (bloom[h1] && bloom[h2]) {
                for (int j = max(0, i - k); j < i; ++j) {
                    if (nums[i] == nums[j])
                        return true;
                }
            }
            bloom[h1] = true;
            bloom[h2] = true;
        }
        return false;
    }
private:
    inline int hash1(int x) {
	    x ^= x >> 16;
	    x *= 0x21f0aaadU;
	    x ^= x >> 15;
	    x *= 0xd35a2d97U;
	    x ^= x >> 15;
	    return x;
    }
    inline int hash2(int x) { return x; }
    int log2(size_t n) {
        int bits = 0;
        while (n > 0) { ++bits; n >>= 1; }
        return bits;
    }
};
63809