<p>A <strong>good meal</strong> is a meal that contains <strong>exactly two different food items</strong> with a sum of deliciousness equal to a power of two.</p>

<p>You can pick <strong>any</strong> two different foods to make a good meal.</p>

<p>Given an array of integers <code>deliciousness</code> where <code>deliciousness[i]</code> is the deliciousness of the <code>i<sup>​​​​​​th</sup>​​​​</code>​​​​ item of food, return <em>the number of different <strong>good meals</strong> you can make from this list modulo</em> <code>10<sup>9</sup> + 7</code>.</p>

<p>Note that items with different indices are considered different even if they have the same deliciousness value.</p>

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

<pre>
<strong>Input:</strong> deliciousness = [1,3,5,7,9]
<strong>Output:</strong> 4
<strong>Explanation: </strong>The good meals are (1,3), (1,7), (3,5) and, (7,9).
Their respective sums are 4, 8, 8, and 16, all of which are powers of 2.
</pre>

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

<pre>
<strong>Input:</strong> deliciousness = [1,1,1,3,3,3,7]
<strong>Output:</strong> 15
<strong>Explanation: </strong>The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.</pre>

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

<ul>
	<li><code>1 &lt;= deliciousness.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= deliciousness[i] &lt;= 2<sup>20</sup></code></li>
</ul>

class Solution {
public:
    int countPairs(vector<int>& arr) {
        vector<int> odd;
        vector<int> even;
        unordered_map<int, long long int> m;

        int n = arr.size();
        long long int ans = 0;
        long long int mod = 1e9 + 7;


        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 0 and m[arr[i]] == 0) {
                even.push_back(arr[i]);
            }
            if (arr[i] % 2 == 1 and m[arr[i]] == 0) {
                odd.push_back(arr[i]);
            }
            m[arr[i]]++;
        }

        for (int i = 0; i < odd.size(); i++) {
            long long int sum1 = 2 * odd[i];
            if (((sum1&(sum1 - 1)) == 0) and m[odd[i]] > 1) {
                long long int val = (m[odd[i]] * (m[odd[i]] - 1)) / 2;
                ans = (ans + val) % mod;
            }
            for (int j = i + 1; j < odd.size(); j++) {
                long long int sum = odd[i] + odd[j];
                if ((sum & (sum - 1)) == 0) {
                    long long int val = m[odd[i]] * m[odd[j]];
                    ans = (ans + val) % mod;
                }
            }
        }

        for (int i = 0; i < even.size(); i++) {
            long long int sum2 = 2 * even[i];
            if (sum2!=0 and  (sum2 & (sum2 - 1)) == 0 and m[even[i]] > 1) {

                long long int val =(m[even[i]] * (m[even[i]]-1))/2;
                ans = (ans + val) % mod;
            }
            for (int j = i + 1; j < even.size(); j++) {
               long long int sum = even[i] + even[j];
                if ((sum & (sum - 1) )== 0) {
                    long long int val = m[even[i]] * m[even[j]];
                    ans = (ans + val) % mod;
                }
            }
        }
        long long int val = m[0]*m[1];
        ans = (ans + val) % mod;
        return ans;
    }
};

80473