<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>

<p>A <strong>good subarray</strong> is a subarray where:</p>

<ul>
	<li>its length is <strong>at least two</strong>, and</li>
	<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
</ul>

<p><strong>Note</strong> that:</p>

<ul>
	<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
	<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
</ul>

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

<pre>
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
</pre>

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

<pre>
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
<strong>Output:</strong> true
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
</pre>

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

<pre>
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
<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>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li>
	<li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Solution {
public:
    bool checkSubarraySum(vector<int>& nums, int k) {
        vector<std::pair<int, int>> prefix_modulos;
        int current_sum = 0;
        int last_num = 0;
        for (int index = 0; index < nums.size(); index++) {
            if (index != 0 && last_num == 0 && nums[index] == 0) { return true; }
            current_sum += nums[index];
            prefix_modulos.push_back(std::make_pair(current_sum % k, index));
            last_num = nums[index];
             //cout << "\nidx " << index << " CS " << current_sum << "  m " << prefix_modulos[index].first;
        }

        std::sort(prefix_modulos.begin(), prefix_modulos.end());

         //cout << "\n sorted --- ";
         //for (auto p : prefix_modulos) {
              //cout << "\n" << p.first << " - " << p.second;
         //}

        for (int start = 0; start < nums.size(); start++) {
            int modulo = prefix_modulos[start].first;
            int start_index = prefix_modulos[start].second;
            if (modulo == 0 && start_index != 0) {
                 //cout << " start idx " << start_index;
                return true;
            } else {
                //cout << "\n Looking for modulo " << modulo;
                auto iter = std::lower_bound(prefix_modulos.begin(),
                    prefix_modulos.end(), modulo, [](const std::pair<int, int>& elem, int modulo){
                    return elem.first < modulo;
                });
                //if (iter == prefix_modulos.end()) {
                    //cout << "\n Not found modulo " << modulo;
                //} else {
                    //cout << "\n found first " << iter->first;
                //}
                while (iter != prefix_modulos.end() && iter->first == modulo) {
                    cout << "\n    Index " << start_index << " " << iter->second;
                    if (start_index - iter->second > 1) {
                        // cout << "\n ss " << start_index << " "  << iter->second << " req " << modulo;
                        // cout << " k " << k << " m " << modulo;
                        return true;
                    }
                    iter++;
                }
            }
        }
        return false;
    }
};
130737