id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.length();\n\n int tillNowSmallest = 0;\n\n set<int> toRemove;\n\n for(int i=0;i<s.length();i++){\n if(s[tillNowSmallest]>=s[i] && s[i]!='*'){\n tillNowSmallest=i;\n }\n if(s[i]=='*'){\n toRemove.insert(tillNowSmallest);\n toRemove.insert(i);\n }\n }\n\n string ans = \"\";\n\n for(int i=0;i<s.length();i++){\n if(toRemove.find(i)!=toRemove.end()){\n continue;\n }\n else ans+=s[i];\n }\n stack<int> indexes[26];\n vector<bool> removed(n, 0);\n for(int j = 0; j < n; j++){\n char c = s[j];\n if(c == '*'){\n removed[j] = 1;\n for(int i = 0; i < 26; i++){\n if(!indexes[i].empty()){\n int ind = indexes[i].top();\n indexes[i].pop();\n removed[ind] = 1;\n break;\n }\n }\n }\n else{\n int ind = c - 'a';\n indexes[ind].push(j);\n }\n }\n string new_s;\n for(int i = 0; i < n; i++){\n if(!removed[i]) new_s += s[i];\n }\n return ans = new_s;\n }\n};", "memory": "138190" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size();\n vector<vector<int>> adj(26); // To store the indices of each character ('a' to 'z')\n unordered_set<int> rem; // To store indices to be removed\n string ans = \"\"; // Result string\n priority_queue<char, vector<char>, greater<char>> pq; // Min-heap for characters\n \n // First pass: process the string\n for (int i = 0; i < n; i++) {\n if (s[i] == '*') {\n rem.insert(i); // Mark the '*' index for removal\n \n // Remove the last occurrence of the lexicographically smallest character\n if (!pq.empty()) {\n char c = pq.top();\n pq.pop();\n int charIndex = c - 'a';\n if (!adj[charIndex].empty()) {\n int ind = adj[charIndex].back(); \n adj[charIndex].pop_back(); \n rem.insert(ind); \n }\n }\n } else {\n \n pq.push(s[i]);\n adj[s[i] - 'a'].push_back(i); \n }\n }\n\n \n for (int i = 0; i < n; i++) {\n if (rem.find(i) == rem.end()) {\n ans += s[i]; \n }\n }\n\n return ans;\n }\n};\n", "memory": "146665" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size();\n vector<vector<int>> adj(26); \n unordered_set<int> rem; \n string ans = \"\"; \n priority_queue<char, vector<char>, greater<char>> pq; \n for (int i = 0; i < n; i++) {\n if (s[i] == '*') {\n rem.insert(i); \n if (!pq.empty()) {\n char c = pq.top();\n pq.pop();\n int charIndex = c - 'a';\n if (!adj[charIndex].empty()) {\n int ind = adj[charIndex].back(); \n adj[charIndex].pop_back(); \n rem.insert(ind); \n }\n }\n } else {\n \n pq.push(s[i]);\n adj[s[i] - 'a'].push_back(i); \n }\n }\n\n \n for (int i = 0; i < n; i++) {\n if (rem.find(i) == rem.end()) {\n ans += s[i]; \n }\n }\n\n return ans;\n }\n};\n", "memory": "146665" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n int n = s.size();\n vector<vector<int>> adj(26); \n unordered_set<int> rem; \n string ans = \"\"; \n priority_queue<char, vector<char>, greater<char>> pq; \n for (int i = 0; i < n; i++) {\n if (s[i] == '*') {\n rem.insert(i); \n if (!pq.empty()) {\n char c = pq.top();\n pq.pop();\n int charIndex = c - 'a';\n if (!adj[charIndex].empty()) {\n int ind = adj[charIndex].back(); \n adj[charIndex].pop_back(); \n rem.insert(ind); \n }\n }\n } else {\n \n pq.push(s[i]);\n adj[s[i] - 'a'].push_back(i); \n }\n }\n\n \n for (int i = 0; i < n; i++) {\n if (rem.find(i) == rem.end()) {\n ans += s[i]; \n }\n }\n\n return ans;\n }\n};\n", "memory": "148784" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n auto compa = [](const auto& a, const auto& b){\n if(a.first == b.first) return a.second < b.second;\n return a.first > b.first;\n };\n\n priority_queue<pair<char,int>, vector<pair<char,int>>, decltype(compa)> pq(compa);\n unordered_set<int> indicesToSkip;\n\n for(int i = 0; i < s.size(); ++i){\n char c = s[i];\n if(c == '*'){\n pair<char, int> p = pq.top(); pq.pop();\n indicesToSkip.insert(p.second);\n indicesToSkip.insert(i);\n }\n else pq.push({c, i});\n }\n\n string res = \"\";\n for(int i = 0; i < s.size(); ++i){\n if(!indicesToSkip.count(i)) res += s[i];\n }\n return res;\n }\n};", "memory": "150903" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n class Pair {\n public:\n char ch; // The character\n int idx; // The index of the character in the string\n Pair(char ch, int idx) {\n this->ch = ch;\n this->idx = idx;\n }\n };\n\n struct Compare {\n bool operator()(const Pair& a, const Pair& b) {\n // Min-heap based on the character value\n if(a.ch!=b.ch){\n return a.ch > b.ch;}\n return a.idx< b.idx;\n }\n };\n\n string clearStars(string s) {\n priority_queue<Pair, vector<Pair>, Compare> pq; // Min-heap for smallest char\n unordered_set<int> removeSet; // To track indices to be removed\n\n // Process the string to handle '*' characters\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == '*') {\n // Remove the lexicographically smallest character on the left\n while (!pq.empty() && removeSet.count(pq.top().idx)) {\n pq.pop(); // Skip characters already marked for removal\n }\n if (!pq.empty()) {\n removeSet.insert(pq.top().idx); // Remove the smallest character\n pq.pop();\n }\n removeSet.insert(i); // Remove the '*' itself\n } else {\n pq.push(Pair(s[i], i)); // Add non-star characters to the heap\n }\n }\n\n // Build the resulting string\n string result;\n for (int i = 0; i < s.size(); i++) {\n if (removeSet.find(i) == removeSet.end()) {\n result += s[i]; // Add characters that are not marked for removal\n }\n }\n\n return result;\n }\n};\n", "memory": "153021" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n map<char,stack<int>>m;\n priority_queue<char,vector<char>,greater<char>>q;\n unordered_set<int> index;\n for(int i=0;i<s.length();i++){\n if(s[i]!='*'){\n if(m[s[i]].empty())\n q.push(s[i]);\n m[s[i]].push(i);\n }\n else{\n char ch=q.top();\n int ind=m[ch].top();\n m[ch].pop();\n if(m[ch].empty()){\n q.pop();\n }\n index.insert(ind);\n index.insert(i);\n }\n }\n string ans=\"\";\n for(int i=0;i<s.length();i++){\n if(index.find(i)==index.end())\n {\n ans.push_back(s[i]);\n }\n }\n return ans;\n \n }\n};", "memory": "155140" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n list<char> str;\n map<char, std::vector<list<char>::iterator>> posMap;\n\n for (const auto& c : s) {\n if (c == '*') {\n auto itr = posMap.begin();\n auto listItr = itr->second.back();\n itr->second.pop_back();\n str.erase(listItr);\n\n if (itr->second.empty()) {\n posMap.erase(itr);\n }\n /*for (auto x : str) {\n cout << x <<\" \" ;\n }\n cout << endl;*/\n } else {\n str.push_back(c);\n auto itr = --str.end();\n posMap[c].push_back(itr);\n /*for (auto x : str) {\n cout << x <<\" \" ;\n }\n cout << endl;*/\n }\n }\n\n string result(str.size(), ' ');\n std::copy(str.begin(), str.end(), result.begin());\n\n return result;\n }\n};", "memory": "157259" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n list<char> str;\n map<char, std::vector<list<char>::iterator>> posMap;\n\n for (const auto& c : s) {\n if (c == '*') {\n auto itr = posMap.begin();\n auto listItr = itr->second.back();\n itr->second.pop_back();\n str.erase(listItr);\n\n if (itr->second.empty()) {\n posMap.erase(itr);\n }\n /*for (auto x : str) {\n cout << x <<\" \" ;\n }\n cout << endl;*/\n } else {\n str.push_back(c);\n auto itr = --str.end();\n posMap[c].push_back(itr);\n /*for (auto x : str) {\n cout << x <<\" \" ;\n }\n cout << endl;*/\n }\n }\n\n string result(str.size(), ' ');\n std::copy(str.begin(), str.end(), result.begin());\n\n return result;\n }\n};", "memory": "159378" }
3,445
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <strong>smallest</strong> non-<code>&#39;*&#39;</code> character to its <em>left</em>. If there are several smallest characters, you can delete any of them.</li> </ul> <p>Return the <span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> resulting string after removing all <code>&#39;*&#39;</code> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaba*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aab&quot;</span></p> <p><strong>Explanation:</strong></p> <p>We should delete one of the <code>&#39;a&#39;</code> characters with <code>&#39;*&#39;</code>. If we choose <code>s[3]</code>, <code>s</code> becomes the lexicographically smallest.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;abc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There is no <code>&#39;*&#39;</code> in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of lowercase English letters and <code>&#39;*&#39;</code>.</li> <li>The input is generated such that it is possible to delete all <code>&#39;*&#39;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string clearStars(string s) {\n list<char> str;\n map<char, std::vector<list<char>::iterator>> posMap;\n\n for (const auto& c : s) {\n if (c == '*') {\n auto itr = posMap.begin();\n auto listItr = itr->second.back();\n itr->second.pop_back();\n str.erase(listItr);\n\n if (itr->second.empty()) {\n posMap.erase(itr);\n }\n } else {\n str.emplace_back(c);\n auto itr = --str.end();\n posMap[c].push_back(itr);\n }\n }\n\n string result(str.size(), ' ');\n std::copy(str.begin(), str.end(), result.begin());\n\n return result;\n }\n};", "memory": "159378" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumDifference(std::vector<int>& array, int target) {\n int arrayLength = array.size();\n int startIndex = -1;\n int currentResult = std::abs(array[0] - target);\n int orAccumulator = 0;\n\n for (int endIndex = 0; endIndex < arrayLength; ++endIndex) {\n orAccumulator |= array[endIndex];\n \n if (orAccumulator > target) {\n orAccumulator = 0;\n startIndex = endIndex;\n while ((orAccumulator | array[startIndex]) <= target) {\n orAccumulator |= array[startIndex--];\n }\n }\n \n if (startIndex != endIndex) {\n currentResult = std::min(currentResult, std::abs(orAccumulator - target));\n }\n \n if (startIndex >= 0) {\n currentResult = std::min(currentResult, std::abs(target - (orAccumulator | array[startIndex])));\n }\n }\n\n return currentResult;\n }\n};\nstatic const int KDS = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n//Kartikdevsharmaa", "memory": "96375" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumDifference(std::vector<int>& array, int target) {\n int arrayLength = array.size();\n int startIndex = -1;\n int currentResult = std::abs(array[0] - target);\n int orAccumulator = 0;\n\n for (int endIndex = 0; endIndex < arrayLength; ++endIndex) {\n orAccumulator |= array[endIndex];\n \n if (orAccumulator > target) {\n orAccumulator = 0;\n startIndex = endIndex;\n while ((orAccumulator | array[startIndex]) <= target) {\n orAccumulator |= array[startIndex--];\n }\n }\n \n if (startIndex != endIndex) {\n currentResult = std::min(currentResult, std::abs(orAccumulator - target));\n }\n \n if (startIndex >= 0) {\n currentResult = std::min(currentResult, std::abs(target - (orAccumulator | array[startIndex])));\n }\n }\n\n return currentResult;\n }\n};\nstatic const int KDS = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n//Kartikdevsharmaa", "memory": "96375" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "#define ll long long \nclass Solution {\npublic:\n int minimumDifference(vector<int>& v, int k) {\n ll ans=INT_MAX;\n int i=0,j=0;\n map<ll,ll> m;\n ll sum=0;\n int n=v.size();\n while (j<v.size()){\n while (j<n&&sum<=k){\n sum|=v[j];\n // cout<<sum<<endl;\n // cout<<abs(sum-k)<<endl;\n ans=min(ans,abs(sum-k));\n cout<<\"ans\"<<ans<<endl;\n ll x=v[j];\n int p=1;\n while (x>0){\n if (x&1)m[p]++;\n p++;\n x=x>>1;\n }\n j++;\n // cout<<j<<endl;\n }\n // cout<<ans<<endl;\n // cout<<sum<<endl;\n while (i<j&&sum>k){\n ll x=v[i];\n ll ne=0,init=1,p=1;\n ll s=32;\n while (s--){\n if (x&1)m[p]--;\n if (m[p]>0){\n ne+=init;\n }\n init*=2;\n x=x>>1;\n p++;\n }\n // cout<<ne<<endl;\n // if(i+1!=j)\n sum=ne;\n i++;\n // cout<<\"i\"<<i<<\" \"<<\"j\"<<j<<endl;\n if (i!=j) {\n // cout<<\"here\"<<endl;\n ans=min(ans,abs(k-ne));\n }\n cout<<\"ans\"<<ans<<endl;\n }\n }\n return ans;\n }\n};", "memory": "100126" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic: \nint getsum(unordered_map<int , int>&c){\n int ans = 0 ;\n int bit = 0 ; \n while(bit<32){\n if(c[bit]>0){\n ans+=(1<<bit) ; \n } \n bit++ ; \n }\n return ans ; \n}\nvoid setbit(unordered_map<int , int>&c , int num , bool check){\n int bit = 0; \n while(bit<32){\n int temp = 1<<bit ; \n if(temp&num)\n if(check)\n c[bit]++; \n else\n c[bit]-- ; \n bit++ ; \n }\n}\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size() ; \n int ans = INT_MAX ; \n int i = 0 ; \n int j = -1 ; \n int curr= 0 ; \n unordered_map<int , int>c ; \n while(j<n&&i<n){\n if(curr<=k&&j<n-1){\n j++ ; \n setbit(c , nums[j] , 1) ; \n curr = getsum(c) ; \n ans = min(ans , abs(curr-k)) ; \n }\n else{\n setbit(c , nums[i] , 0) ; \n curr = getsum(c) ; \n if(curr!=0)\n ans = min(ans , abs(curr-k)) ;\n i++ ; \n }\n }\n return ans ; \n } \n};", "memory": "100126" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "const int N = 1e5 + 1;\nint dp[N + 1][21]; \nclass Solution {\n \n int ans = INT_MAX;\n int n, k;\n int maxj;\n \n void dfs(int cur, int prev) {\n if(cur >= n) {\n return;\n }\n if(prev >= k) {\n return;\n }\n auto& v = dp[cur];\n \n int lo = 0, hi = maxj + 1;\n // Find largest lo that [(v[lo] | prev) < k]\n while(lo < hi) {\n int mid = (lo + hi + 1) / 2;\n if(v[mid] == -1 || (v[mid] | prev) >= k) {\n hi = mid - 1;\n }else{\n lo = mid;\n }\n }\n ans = min(ans, abs((v[lo] | prev) - k));\n int len = 1 << lo;\n dfs(cur + len, prev | v[lo]);\n }\n \npublic:\n int minimumDifference(vector<int>& A, int k) {\n n = A.size();\n this->k = k;\n memset(dp, 0xff, sizeof(dp));\n for(int i = 0; i < n; ++i) {\n dp[i][0] = A[i];\n }\n maxj = 0;\n for(int j = 1, len = 2; len <= n; ++j, len <<= 1) {\n maxj = j;\n for(int i = 0; i < n; ++i) {\n if(i + len > n) {\n break;\n }\n dp[i][j] = dp[i][j - 1] | dp[i + len/2][j - 1];\n }\n }\n \n for(int i = 0; i < n; ++i) {\n dfs(i, 0);\n }\n \n return ans;\n }\n};", "memory": "103878" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "#if 0\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int res = abs(nums[0] - k);\n int n = nums.size();\n\n for (int r = 0; r < n; ++r) {\n res = min(res, abs(nums[r] - k));\n for (int l = r - 1; l >= 0; --l) {\n if ((nums[l] | nums[r]) == nums[l]) {\n break;\n }\n nums[l] |= nums[r];\n res = min(res, abs(nums[l] - k));\n }\n }\n\n return res;\n }\n};\n\n// 給陣列和 k\n// 回傳子陣列的 all_or 和 k 的最小差值\n// ===\n\n// 靈神\n// 這題有偷改題意 因此以原題方法講解一下\n// 原題是用 and, 所以用 and 去講解\n\n// 先想一個做法, 如果某個模擬過程可以把子陣列的所有 and 都遇到\n// 那這樣就可以找到答案\n\n// 方法是枚舉每個子陣列的右端點, 並且把每個左側的原始值給蓋掉\n\n// 例如\n// nums = [a0 a1 a2 a3]\n// i = 0 a0\n// i = 1 (a0)&a1 a1\n// i = 2 (a0&a1)&a2 (a1)&a2 a2 \n// i = 3 (a0&a1&a2)&a3 (a1&a2)&a3 (a2)&a3 a3\n\n// 觀察一下 有些更新操作是多餘的\n\n// 具體用數字表示一下\n// nums = [ 2 3 6 4 5 ]\n// 010 011 110 100 101\n// i = 0 010\n// i = 1 010 011\n// i = 2 010 010 110\n// i = 3 000 000 100 100\n// => 這時候在尾巴多加一個數字 5 再觀察規律\n// i = 4 000 000 100 100 101\n// ^\n// 觀察發現算到數字 4 位置的時候\n// 4 & 5 = 4 算出來的結果不變\n// 那這可以表示後面的 6 3 2 剩下的位置就不用算\n// why? \n\n// 因為拿來 & 的數字 (5) 不能改變當下的結果 不能使得結果變更小\n// 擴展一點來說 更小的數字 更不會被改變結果\n\n// 用集合的觀點去思考\n// 如果數字and後, 那數字要馬不變 要馬變少\n// 用集合的術語來說\n// A 包含於 B, B 包含於 C (想像 B 包含於 C 就是 B 被一個大 C 給圍住)\n// 那麼 B 交集(AND) C 為 B\n// 可以推論 A 交集 C 為 A\n\n// 代例子去解釋就是\n// 現在要算 100 AND 101 會得到 100\n// 這就跟 B 交集 C 為 B 是一樣的意思\n// 那麼我們就不用算剩下的 A 交集 B 交集 C\n// 也就是說不用算 100 (索引6, A和B) AND 101(C) 和之後的索引 3, 索引 2\n// 因為結果都不變\n\n// 那我們可以不用計算左邊的部分, 也就省了很多時間\n// 又 因為一直取 & 的過程 會讓值越來越小, 所以這方法其實就是 O(n*logU), 其中 U 就是最大值\n\n// 複雜度分析\n// 看起來像是 n^2 不過不是\n// 數字要馬變小要馬不變, 如果不變就是跳出\n// 那就分析一下他可能變小幾次 nums[i] 最大1e9 也就是約 2^29\n// 那就只能變小29次 所以時間複雜度是 29*n\n\n// PS. 這題改成 OR 所以列一下 OR 的分析\n// nums = [ 2 3 6 4 5 ]\n// 010 011 110 100 101\n// i = 0 010\n// i = 1 011 011\n// i = 2 111 111 110\n// i = 3 111 111 \"110\" 100\n// i = 4 111 \"111\" 111 101 101\n\n// 跟 & 類似\n// 如果和一個數字 OR 沒辦法改變目前的結果 增加他的上限 讓數字變大\n// 那這個數字也沒辦法讓其他更大的數字變大\n// 極端又明顯的例子是 如果出現 111... 那很明顯後面根本不用算\n\n// 同樣的觀念可以套在 AND, OR, GCD, LCM\n// 自己覺得是只要某個操作 可以使得數字有單調性 (逐漸遞增或遞減) 那就可以用這種觀念去解題目\n\n// TIME: O(n*log(U)), U = max(nums)\n// space: O(1)\n#endif\n\n#if 1\n// #define localMinMax\ntemplate <typename T>\nclass SegTreeNode {\npublic:\n // Note: all arrays are 1-index\n vector<T> tree;\n vector<T> lazy;\n#ifdef localMinMax\n const long long LINF = 0x3f3f3f3f3f3f3f3fLL;\n vector<T> treeMin;\n vector<T> treeMax;\n#endif\n\n SegTreeNode() {}\n\n // init for range [1, n] with val\n SegTreeNode(int n, T val = 0) {\n initSize(n);\n init(1, 1, n, val);\n }\n\n // init for range [1, n] with nums\n // 1-index\n SegTreeNode(int n, vector<int>& nums) {\n initSize(n);\n init(1, 1, n, nums);\n }\n\n // init for range [1, n] with nums\n // 1-index\n SegTreeNode(int n, vector<long long>& nums) {\n initSize(n);\n init(1, 1, n, nums);\n }\n int my__builtin_clz(int n) {\n int sum = 0;\n for (int i = 31; i >= 0; --i) {\n if (n & (1 << i)) {\n sum = i;\n break;\n }\n }\n return 31 - sum;\n }\n void initSize(int n) {\n int SIZE = 2 << (32 - my__builtin_clz(n));\n tree.assign(SIZE, 0);\n // lazy.assign(SIZE, 0);\n#ifdef localMinMax\n treeMin.assign(SIZE, LINF);\n treeMax.assign(SIZE, -LINF);\n#endif\n }\n\n void pull(int id) {\n tree[id] = tree[id * 2] | tree[id * 2 + 1];\n#ifdef localMinMax\n treeMin[id] = min(treeMin[id * 2], treeMin[id * 2 + 1]);\n treeMax[id] = max(treeMax[id * 2], treeMax[id * 2 + 1]);\n#endif\n }\n\n void init(int id, int L, int R, T val) {\n if (L == R) {\n tree[id] = val;\n#ifdef localMinMax\n treeMin[id] = val;\n treeMax[id] = val;\n#endif\n return;\n }\n int mid = L + (R - L) / 2;\n init(id * 2, L, mid, val);\n init(id * 2 + 1, mid + 1, R, val);\n pull(id);\n }\n\n // 1-index, so nums index should -1\n void init(int id, int L, int R, vector<int>& nums) {\n if (L == R) {\n tree[id] = nums[L - 1];\n#ifdef localMinMax\n treeMin[id] = nums[L - 1];\n treeMax[id] = nums[L - 1];\n#endif\n return;\n }\n int mid = L + (R - L) / 2;\n init(id * 2, L, mid, nums);\n init(id * 2 + 1, mid + 1, R, nums);\n pull(id);\n }\n\n // 1-index, so nums index should -1\n void init(int id, int L, int R, vector<long long>& nums) {\n if (L == R) {\n tree[id] = nums[L - 1];\n#ifdef localMinMax\n treeMin[id] = nums[L - 1];\n treeMax[id] = nums[L - 1];\n#endif\n return;\n }\n int mid = L + (R - L) / 2;\n init(id * 2, L, mid, nums);\n init(id * 2 + 1, mid + 1, R, nums);\n pull(id);\n }\n\n // setValue(1, 1, n, pos, val)\n // set treeVal[pos] as value, 1-index\n // time: O(log(n))\n void setValue(int id, int l, int r, int pos, T val) {\n if (l == r) {\n tree[id] = val;\n#ifdef localMinMax\n treeMin[id] = val;\n treeMax[id] = val;\n#endif\n return;\n }\n\n int mid = l + (r - l) / 2;\n if (pos <= mid) {\n setValue(id * 2, l, mid, pos, val);\n }\n else {\n setValue(id * 2 + 1, mid + 1, r, pos, val);\n }\n pull(id);\n }\n\n // updateDelta(1, 1, n, pos, val)\n // increase treeVal[pos] by delta\n // time: O(log(n))\n void updateDelta(int id, int l, int r, int pos, T val) {\n if (l == r) {\n tree[id] += val;\n#ifdef localMinMax\n treeMin[id] += val;\n treeMax[id] += val;\n#endif\n return;\n }\n\n int mid = l + (r - l) / 2;\n if (pos <= mid) {\n updateDelta(id * 2, l, mid, pos, val);\n }\n else {\n updateDelta(id * 2 + 1, mid + 1, r, pos, val);\n }\n pull(id);\n }\n\n void do_(int id, int l, int r, T val) {\n tree[id] += 0;\n lazy[id] += val;\n#ifdef localMinMax\n treeMin[id] += val;\n treeMax[id] += val;\n#endif\n }\n\n // updateRangeDelta(1, 1, n, L, R, val)\n // increase treeVal[L, R] by val\n // time: O(log(n))\n void updateRangeDelta(int id, int l, int r, int L, int R, T val) {\n if (L <= l && r <= R) {\n do_(id, l, r, val);\n // not update to the next level\n return;\n }\n\n int mid = l + (r - l) / 2;\n // need update lazy tag to the left and right sub node\n if (lazy[id]) {\n do_(id * 2, l, mid, lazy[id]);\n do_(id * 2 + 1, mid + 1, r, lazy[id]);\n lazy[id] = 0;\n }\n\n if (mid >= L) {\n updateRangeDelta(id * 2, l, mid, L, R, val);\n }\n if (mid < R) {\n updateRangeDelta(id * 2 + 1, mid + 1, r, L, R, val);\n }\n pull(id);\n }\n\n // queryRangeSum(1, 1, n, L, R)\n // sum of a range nums[L, R] inclusively\n // time: O(log(n))\n T queryRangeSum(int id, int l, int r, int L, int R) {\n if (L > R) {\n return 0;\n }\n if (L == l && R == r) {\n return tree[id];\n }\n int mid = (l + r) / 2;\n\n // if (lazy[id]) {\n // do_(id * 2, l, mid, lazy[id]);\n // do_(id * 2 + 1, mid + 1, r, lazy[id]);\n // lazy[id] = 0;\n // }\n\n return queryRangeSum(id * 2, l, mid, L, min(R, mid)) |\n queryRangeSum(id * 2 + 1, mid + 1, r, max(L, mid + 1), R);\n }\n\n#ifdef localMinMax\n // queryRangeMinMax(1, 1, n, L, R)\n // query min and max of range nums[L, R] inclusively \n // time: O(log(n))\n array<T, 2> queryRangeMinMax(int id, int l, int r, int L, int R) {\n if (L > R) {\n return {LINF, -LINF};\n }\n if (L == l && R == r) {\n return {treeMin[id], treeMax[id]};\n }\n int mid = (l + r) / 2;\n\n if (lazy[id]) {\n do_(id * 2, l, mid, lazy[id]);\n do_(id * 2 + 1, mid + 1, r, lazy[id]);\n lazy[id] = 0;\n }\n\n array<T, 2> leftRes = queryRangeMinMax(id * 2, l, mid, L, min(R, mid));\n array<T, 2> rightRes = queryRangeMinMax(id * 2 + 1, mid + 1, r, max(L, mid + 1), R);\n array<T, 2> res = { min(leftRes[0], rightRes[0]), max(leftRes[1], rightRes[1]) };\n\n return res;\n }\n#endif\n};\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n SegTreeNode<int> seg(n, nums);\n int res = abs(nums[0] - k);\n \n // O(n)\n for (int i = 0; i < n; ++i) {\n int left = i, right = n;\n // log(n)\n while (left < right) {\n int mid = left + (right - left) / 2;\n // log(n)\n int rangeOr = seg.queryRangeSum(1, 1, n, i + 1, mid + 1);\n\n res = min(res, abs(rangeOr - k));\n if (rangeOr == k) {\n right = mid;\n return 0;\n }\n else if (rangeOr < k) {\n left = mid + 1;\n }\n else if (rangeOr > k) {\n right = mid;\n }\n }\n }\n return res;\n }\n};\n\n// 給陣列和 k\n// 回傳 子陣列的 all_or 和 k 的最小差值\n// ===\n\n// 區域 OR => 線段樹\n// 線段樹弄好後走訪每個索引 以當下索引當成左邊界 去找右邊界的可能位置 (二分搜)\n// 因為是區域 OR, 所以有單調性 (越多 OR 值只會增加, 越少 OR 值只會減少)\n// 如果 rangeOr 的結果就是 k, 那可以 return 0 因為題目問你的最小誤差是 |rangeOr - k|\n// 否則看一下 rangeOr 比 k 大 還是比 k 小\n// 比 k 大 表示結果太大 所以要縮小調整右側邊界\n// 比 k 小 表示結果太小 所以要增加左側邊界\n// 接著就更新答案\n\n// 把一些模板不必要的判斷和空間拿掉 會變更快一點\n\n// time: O(n * log(n) * log(n))\n// space: O(n)\n#endif", "memory": "103878" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\n vector<int> seg;\n void build(int i,int l,int r,vector<int> &nums){\n if(l==r){\n seg[i] = nums[l];\n return;\n }\n int m = l+((r-l)>>1);\n build(i<<1,l,m,nums);\n build(i<<1|1,m+1,r,nums);\n seg[i] = seg[i<<1]|seg[i<<1|1];\n }\n int query(int i,int l,int r,int ql,int qr){\n if(l>qr || ql>r){\n return 0;\n }\n else if(ql<=l && r<=qr){\n return seg[i];\n }\n int m = l+((r-l)>>1);\n int left = query(i<<1,l,m,ql,qr);\n int right = query(i<<1|1,m+1,r,ql,qr);\n return left|right;\n }\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n int n = nums.size();\n seg.resize(4*n+1);\n build(1,0,n-1,nums);\n int ans = 1e9;\n for(int i=0;i<n;i++){\n int l = i,r = n-1;\n int res = i-1;\n while(l<=r){\n int m = l+((r-l)>>1);\n int cur_xor = query(1,0,n-1,i,m);\n if(cur_xor<=k){\n res = m;\n l = m+1;\n }\n else{\n r = m-1;\n }\n }\n if(res>=i){\n ans = min(ans,k-query(1,0,n-1,i,res));\n }\n if(res<n-1){\n ans = min(ans,query(1,0,n-1,i,res+1)-k);\n }\n }\n return ans;\n }\n};", "memory": "107629" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "/*\nThe nature of Bitwise And is Decreasing\nSay V = bitwise And of subarray from L to R\nNow we need to find the minimum of abs(K - V)\nIf V < K = we try to increase V by shrinking our subarray\nIf V > K = we try to decrease V by expanding our subarray\nIf V = K = we found the minimum abs(K - V) . So return it\n*/\n\nint _ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); return 0; }();\n\nclass Solution {\npublic:\n vector<int> tree;\n\n int build(int node , int low , int high , vector<int> &nums){\n if(low == high) return tree[node] = nums[low];\n int mid = (low + high) >> 1;\n return tree[node] = build(node << 1 , low , mid , nums) | build(node << 1 | 1 , mid + 1 , high , nums);\n }\n\n int bitwiseOr(int node , int low , int high , int ql , int qr){\n if(high < ql || low > qr) return 0;\n if(low >= ql && high <= qr) return tree[node];\n int mid = (low + high) >> 1;\n return bitwiseOr(node << 1 , low , mid , ql , qr) | bitwiseOr(node << 1 | 1 , mid + 1 , high , ql , qr);\n }\n\n int minimumDifference(vector<int> &nums, int K){\n int N = nums.size() , minDiff = 1e9;\n\n tree.resize(N << 2 | 1);\n build(1 , 0 , N - 1 , nums);\n\n for(int L = 0 ; L < N ; L++){\n int low = L , high = N - 1;\n while(low <= high){\n int R = (low + high) >> 1;\n int V = bitwiseOr(1 , 0 , N - 1 , L , R);\n minDiff = min(minDiff , abs(K - V));\n if(V < K) low = R + 1 ; // Shrink\n else if(V > K) high = R - 1; // Expand\n else return minDiff; // Found Optimal\n }\n }\n return minDiff;\n }\n};", "memory": "107629" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "const int MX = 1e5;\nconst int LOG = 20;\n\nlong long m[MX][LOG];\n\nint query(int l, int r) {\n\tint len = r - l + 1;\n // cout << len << '\\n';\n\tint c = log2(len);\n\treturn m[l][c] | m[r - (1 << c) + 1][c]; // m[r - (1 << c) + 1][c]\n}\n\nint search2(int x, int left, int right) {\n int l = left, r = right;\n while (l < r) {\n int mid = (l + r) / 2;\n if (x <= query(left, mid))\n r = mid;\n else\n l = mid + 1;\n }\n\n return l;\n}\n\nint lower_bound2(int left, int right, int X) {\n int mid;\n int low = left;\n int high = right;\n while (low < high) {\n mid = low + (high - low) / 2;\n if (X <= query(left, mid)) {\n high = mid;\n }\n else {\n low = mid + 1;\n }\n }\n \n if (low < right && query(left, low) < X) {\n low++;\n }\n\n return low;\n}\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& a, int k) {\n int n = a.size();\n if (n == 1) {\n return abs(k - a[0]);\n }\n\n for (int i = 0; i < n; i++)\n m[i][0] = a[i];\n for (int j = 1; j <= LOG; j++)\n for (int i = 0; i + (1 << j) <= n; i++)\n m[i][j] = m[i][j - 1] | m[i + (1 << (j - 1))][j - 1];\n int ans = INT_MAX;\n for (int i = 0; i < n; i++) {\n int pos1 = lower_bound2(i, n, k);\n if (pos1 == n) pos1--;\n ans = min(ans, abs(k - query(i, pos1)));\n if (pos1 > 0 && pos1 - 1 >= i) pos1--;\n ans = min(ans, abs(k - query(i, pos1)));\n }\n\n // for (int i = 0; i < a.size(); i++) {\n // for (int j = i; j < a.size(); j++)\n // cout << query(i, j) << ' '; \n // }\n\n return ans;\n }\n};", "memory": "111380" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\n struct SparseTable {\n long long lg[100005], st[18][100005];\n void init(const vector<int>& a, int n) {\n for(int i = 1; i <= n; ++i) st[0][i] = a[i - 1];\n for(int j = 1; j <= 17; ++j) for(int i = 1; i <= n - (1 << j) + 1; ++i) st[j][i] = st[j - 1][i] | st[j - 1][i + (1 << (j - 1))];\n }\n long long query(int l, int r) {\n int k = __lg(r - l + 1);\n //cout << k << \" \" << l << \" \" << r << endl;\n return st[k][l] | st[k][r - (1 << k) + 1];\n }\n }st;\n\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n st.init(nums, nums.size());\n int n = nums.size();\n long long mn = LLONG_MAX;\n // binary search on right index (1-indexing)\n // bitwise OR increasing allows us to do binary search\n for (int left = 1; left <= n; left++){\n // include n in search space\n // always rounded down so n + 1 never searched because \n // we take left < n + 1\n int high = n;\n int low = left;\n long long s;\n //cout << \"HERE\" << endl;\n while (low <= high) {\n //cout << \"LEFT \" << left << endl;\n int mid = (high + low) >> 1;\n //cout << left << \" \" << mid << endl;\n s = st.query(left, mid);\n mn = min(mn, abs(k - s));\n //cout << s << \" \" << low << \" \" << mid << endl;\n if (s < k) low = mid + 1;\n else if (s > k) high = mid-1;\n else return 0;\n }\n //mn = min(mn, s)\n }\n return mn;\n }\n};", "memory": "111380" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "using int64 = long long;\n\n class segtree {\n public:\n struct node {\n // don't forget to set default value (used for leaves)\n // not necessarily neutral element!\n int64 sum = 0;\n int64 tag = 0;\n \n void apply(int l, int r, int64 v) {\n tag = tag + v;\n sum = sum + v * (r - l + 1);\n }\n };\n \n node unite(const node &a, const node &b) const {\n node res;\n res.sum = a.sum | b.sum;\n return res;\n }\n \n inline void push(int x, int l, int r) {\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n // push from x into (x + 1) and z\n if (tree[x].tag != 0) {\n tree[x + 1].apply(l, y, tree[x].tag);\n tree[z].apply(y + 1, r, tree[x].tag);\n tree[x].tag = 0;\n }\n }\n \n inline void pull(int x, int z) {\n tree[x] = unite(tree[x + 1], tree[z]);\n }\n \n int n;\n vector<node> tree;\n \n void build(int x, int l, int r) {\n if (l == r) {\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n build(x + 1, l, y);\n build(z, y + 1, r);\n pull(x, z);\n }\n \n template <typename M>\n void build(int x, int l, int r, const vector<M> &v) {\n if (l == r) {\n tree[x].apply(l, r, v[l]);\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n build(x + 1, l, y, v);\n build(z, y + 1, r, v);\n pull(x, z);\n }\n \n node get(int x, int l, int r, int ll, int rr) {\n if (ll <= l && r <= rr) {\n return tree[x];\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n push(x, l, r);\n node res{};\n if (rr <= y) {\n res = get(x + 1, l, y, ll, rr);\n } else {\n if (ll > y) {\n res = get(z, y + 1, r, ll, rr);\n } else {\n res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));\n }\n }\n pull(x, z);\n return res;\n }\n \n template <typename... M>\n void modify(int x, int l, int r, int ll, int rr, const M&... v) {\n if (ll <= l && r <= rr) {\n tree[x].apply(l, r, v...);\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n push(x, l, r);\n if (ll <= y) {\n modify(x + 1, l, y, ll, rr, v...);\n }\n if (rr > y) {\n modify(z, y + 1, r, ll, rr, v...);\n }\n pull(x, z);\n }\n \n segtree(int _n) : n(_n) {\n assert(n > 0);\n tree.resize(2 * n - 1);\n build(0, 0, n - 1);\n }\n \n template <typename M>\n segtree(const vector<M> &v) {\n n = v.size();\n assert(n > 0);\n tree.resize(2 * n - 1);\n build(0, 0, n - 1, v);\n }\n \n node get(int ll, int rr) {\n assert(0 <= ll && ll <= rr && rr <= n - 1);\n return get(0, 0, n - 1, ll, rr);\n }\n \n node get(int p) {\n assert(0 <= p && p <= n - 1);\n return get(0, 0, n - 1, p, p);\n }\n \n template <typename... M>\n void modify(int ll, int rr, const M&... v) {\n assert(0 <= ll && ll <= rr && rr <= n - 1);\n modify(0, 0, n - 1, ll, rr, v...);\n }\n };\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n segtree st(nums);\n int64 ans = 1e9 + 10;\n \n int i = 0;\n for (int j = 0; j < n; j++) {\n while (i < j && st.get(i, j).sum > k) {\n ans = min(ans, abs(st.get(i, j).sum - k));\n i++;\n }\n ans = min(ans, abs(st.get(i, j).sum - k));\n }\n return ans;\n }\n};\n", "memory": "115131" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#define ll long long\n#define vll vector<ll>\nclass Tree {\n public:\n ll n;\n vll t;\n \n Tree(ll n) {\n t.resize(4*n);\n this->n = n;\n }\n\n \n void build(vll &v, ll i, ll l, ll r) {\n if(l==r) t[i]=v[l];\n else {\n ll mid = (l+r)/2;\n build(v, i*2, l, mid);\n build(v, i*2+1, mid+1, r);\n t[i] = t[i*2] | t[i*2+1];\n }\n }\n \n ll sum(ll i, ll l, ll r, ll ql, ll qr) {\n if(ql>qr) return 0;\n if(l==ql && r==qr) return t[i];\n \n ll mid = (l+r)/2;\n \n return sum(i*2, l, mid, ql, min(qr, mid)) | sum(i*2+1, mid+1, r, max(ql, mid+1), qr);\n }\n \n void range_update(ll i, ll l, ll r, ll ql, ll qr, ll new_val) {\n if(ql>qr) return;\n if(l==ql && r==qr) t[i]+=new_val;\n else {\n ll mid = (l+r)/2;\n \n range_update(i*2, l, mid, ql, min(mid, qr), new_val);\n range_update(i*2+1, mid+1, r, max(mid+1, ql), qr, new_val);\n }\n }\n \n ll get(ll i, ll l, ll r, ll pos) {\n if(l==r) return t[i];\n ll mid = (l+r)/2;\n \n if(pos<=mid) return t[i]+get(i*2, l, mid, pos);\n else return t[i]+get(i*2+1, mid+1, r, pos);\n }\n \n void update(ll i, ll l, ll r, ll pos, ll new_val) {\n if(l==r) t[i] = new_val;\n else {\n ll mid = (l+r)/2;\n \n if(pos<=mid) update(i*2, l, mid, pos, new_val);\n else update(i*2+1, mid+1, r, pos, new_val);\n t[i] = t[i*2] | t[i*2+1];\n }\n }\n};\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n ll ans = INT_MAX, n=nums.size();\n \n vll v(n);\n for(ll i=0; i<n; i++) v[i]=nums[i];\n \n Tree* tree = new Tree(n);\n tree->build(v, 1, 0, n-1);\n \n for(int i=0; i<n; i++) {\n ll l = i, r = n-1;\n while(r-l>1) {\n ll mid = l+(r-l)/2;\n if(tree->sum(1, 0, n-1, i, mid)<k) l = mid;\n else r=mid;\n }\n ans = min(ans, abs(k-tree->sum(1, 0, n-1, i, l)));\n ans = min(ans, abs(k-tree->sum(1, 0, n-1, i, r)));\n }\n \n return ans;\n }\n};", "memory": "118883" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class SparseTable{\n int n, m;\n vector<vector<int>> dp;\npublic:\n SparseTable(vector<int> &a) {\n n = a.size();\n m = log2(n) + 1;\n dp.resize(m, vector<int>(n));\n for (int i = 0 ; i < n ; i++) dp[0][i] = a[i];\n for (int i = 1 ; i < m ; i++) {\n for (int j = 0 ; j + (1 << i) <= n ; j++) {\n dp[i][j] = dp[i-1][j] | dp[i-1][j + (1 << (i-1))];\n }\n }\n }\n\n int query(int i, int len) {\n int res = 0;\n for (int b = m - 1 ; b >= 0 ; b--) {\n if (len >= (1 << b)) {\n res |= dp[b][i];\n i += (1 << b);\n len -= (1 << b);\n }\n }\n return res;\n }\n};\n\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n if (n == 1) return abs(k - nums[0]);\n SparseTable st(nums);\n int res = INT_MAX;\n for (int i = 0 ; i < n ; i++) {\n int l = i, r = n;\n while (l < r) {\n int m = (l + r) / 2;\n if (st.query(i, m - i + 1) < k) l = m + 1;\n else r = m; \n }\n if (l < n) res = min(res, abs(k - st.query(i, l - i + 1)));\n if (l - i) res = min(res, abs(k - st.query(i, l - i)));\n }\n return res;\n return 0;\n }\n};", "memory": "122634" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n const int inf=1e9+5;\n int minimumDifference(vector<int>& a, int k) {\n int n=a.size();\n int p=0,x=1;\n while(x<=n){\n x<<=1;\n p++;\n }\n p++;\n vector<vector<int>>t(p,vector<int>(n));\n for(int i=0;i<n;i++) t[0][i]=a[i];\n for(int i=1;i<p;i++){\n for(int j=0;j+(1<<i)-1<n;j++){\n t[i][j]=t[i-1][j] | t[i-1][j+(1<<(i-1))];\n }\n }\n auto f=[&](int l,int r){\n int len=(r-l+1);\n int p=0,x=1;\n while((x<<1)<=len){\n x<<=1;\n p++;\n }\n int res=t[p][l] | t[p][r-(1<<p)+1];\n return res;\n };\n int res=inf;\n for(int i=0;i<n;i++){\n int l=i-1,r=n;\n while(r-l>1){\n int mid=(r+l)/2;\n int x=f(i,mid);\n if(x>=k){\n r=mid;\n }\n else l=mid;\n }\n if(r>=0 && r<n && r>=i){\n res=min(res,abs(k-f(i,r)));\n }\n if(r-1>=0 && r-1>=i){\n res=min(res,abs(k-f(i,r-1)));\n }\n }\n return res;\n }\n};", "memory": "126385" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class RMQ {\n static const int maxk = 16;\n int n;\n vector<vector<int>> rmq;\npublic:\n RMQ(vector<int> &vec) {\n n = vec.size();\n rmq = vector<vector<int>>(maxk+2, vector<int>(n+5, 0));\n for(int i = 0; i < n; ++i)\n rmq[0][i] = vec[i];\n for(int k = 1, len = 2; k <= maxk; ++k, len<<=1) {\n for(int i = 0; i+len-1 < n; ++i)\n rmq[k][i] = rmq[k-1][i] | rmq[k-1][i+(len>>1)];\n }\n }\n int que(int l, int r) {\n // assert(0<=l && r<=n-1);\n int k = 0, len = 1;\n while((len<<1) <= r-l+1) ++k, len <<= 1;\n // if(l == 0 && r == 6) {\n // cout << \"k = \" << k << \", len = \" << len << \", rmq[k][l] = \" << rmq[k][l] << \", rmq[k][r-len+1] = \" << rmq[k][r-len+1] << '\\n';\n // }\n return rmq[k][l] | rmq[k][r-len+1];\n }\n};\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n RMQ rmq(nums);\n int n = nums.size();\n int ans = 1e9;\n for(int i = 0; i < n; ++i) {\n int l = i, r = n-1, res = i-1;\n while(l <= r) {\n int mid = (l + r) >> 1;\n if(rmq.que(i,mid) <= k) res = mid, l = mid+1;\n else r = mid-1;\n }\n if(res >= i) {\n // cout << \"i = \" << i << \", res = \" << res << \", rmq.que(i,res) = \" << rmq.que(i,res) << '\\n';\n ans = min(ans, k - rmq.que(i,res));\n }\n if(res+1 <= n-1) {\n // cout << \"i = \" << i << \", res+1 = \" << (res+1) << \", rmq.que(i,res+1) = \" << rmq.que(i,res+1) << '\\n';\n ans = min(ans, rmq.que(i,res+1) - k);\n }\n }\n return ans;\n }\n};", "memory": "130136" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\n/* Macros {{{ */\n/* A lot of this is from some of Benq's submissions\n [https://codeforces.com/profile/Benq]\n Ugly af to the eyes, but with vim fold its barable\n Hopefully c++20 concepts can make all this stuff must cleaner */\n\n/* Basics {{{ */\nusing ll = long long;\nusing ld = long double;\nusing str = string;\n\nusing pi = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\n#define mp make_pair\n#define fi first\n#define se second\n\n#define arr array\n#define ve vector\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vld = vector<ld>;\n\nusing vpi = vector<pi>;\nusing vpll = vector<pll>;\nusing vpld = vector<pld>;\n\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\nusing vvld = vector<vld>;\n\nusing vvpi = vector<vpi>;\nusing vvpll = vector<vpll>;\nusing vvpld = vector<vpld>;\n\n#define pb push_back\n#define lb lower_bound\n#define ub upper_bound\n#define sz size()\n#define rsz(a) resize(a)\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n\n#define For(i, a, b) for (int i = a; i < b; ++i)\n#define Rof(i, a, b) for (int i = (b)-1; i >= (a); --i)\n#define rep(a) For(_, 0, a)\n#define each(a, x) for (auto &a : x)\n#define reach(a, x) for (auto a = x.rbegin(); a != x.rend(); ++a)\n\ntemplate <typename T, typename U>\ninline void cmin(T &x, U y) {\n if (y < x) x = y;\n}\ntemplate <typename T, typename U>\ninline void cmax(T &x, U y) {\n if (x < y) x = y;\n}\n/*}}}*/\n\n/* IO {{{ */\n\n/* Template Macros {{{ */\n#define tcT template <class T\n#define tcTU tcT, class U\n#define tcTUU tcT, class... U\n/*}}}*/\n\ninline namespace Helpers { /*{{{*/\ntcT, class = void > struct is_iterable : false_type {};\ntcT > struct is_iterable<\n T, void_t<decltype(begin(declval<T>())), decltype(end(declval<T>()))>>\n : true_type {};\ntcT > constexpr bool is_iterable_v = is_iterable<T>::value;\n\ntcT, class = void > struct is_readable : false_type {};\ntcT > struct is_readable<T, typename std::enable_if_t<is_same_v<\n decltype(cin >> declval<T &>()), istream &>>>\n : true_type {};\ntcT > constexpr bool is_readable_v = is_readable<T>::value;\n\ntcT, class = void > struct is_printable : false_type {};\ntcT > struct is_printable<T, typename std::enable_if_t<is_same_v<\n decltype(cout << declval<T>()), ostream &>>>\n : true_type {};\ntcT > constexpr bool is_printable_v = is_printable<T>::value;\n} /* namespace Helpers */\n/*}}}*/\n\ninline namespace Input { /*{{{*/\ntcT > constexpr bool needs_input_v = !is_readable_v<T> && is_iterable_v<T>;\ntcTUU > void re(T &t, U &...u);\ntcTU > void re(pair<T, U> &p); /* pairs */\n\n/* re: read{{{ */\ntcT > typename enable_if<is_readable_v<T>, void>::type re(T &x) {\n cin >> x;\n} /* default */\ntcT > typename enable_if<needs_input_v<T>, void>::type re(\n T &i); // vectors, arrays, etc...\ntcTU > void re(pair<T, U> &p) { re(p.fi, p.se); } // pairs\ntcT > typename enable_if<needs_input_v<T>, void>::type re(T &i) {\n each(x, i) re(x);\n}\ntcTUU > void re(T &t, U &...u) {\n re(t);\n re(u...);\n} /* read multiple}}} */\n\n/* rv: resize and read vectors{{{ */\nvoid rv(size_t) {}\ntcTUU > void rv(size_t N, ve<T> &t, U &...u);\ntemplate <class... U>\nvoid rv(size_t, size_t N2, U &...u);\ntcTUU > void rv(size_t N, ve<T> &t, U &...u) {\n t.rsz(N);\n re(t);\n rv(N, u...);\n}\ntemplate <class... U>\nvoid rv(size_t, size_t N2, U &...u) {\n rv(N2, u...);\n} /*}}}*/\n\n/* dumb shortcuts to read in ints{{{ */\nvoid decrement() {} /* subtract one from each */\ntcTUU > void decrement(T &t, U &...u) {\n --t;\n decrement(u...);\n}\n#define ints(...) \\\n int __VA_ARGS__; \\\n re(__VA_ARGS__);\n#define int1(...) \\\n ints(__VA_ARGS__); \\\n decrement(__VA_ARGS__); /*}}}*/\n} /* namespace Input */\n/*}}}*/\n\ninline namespace ToString { /*{{{*/\ntcT > constexpr bool needs_output_v = !is_printable_v<T> && is_iterable_v<T>;\n\n/* ts: string representation to print */\ntcT > typename enable_if<is_printable_v<T>, str>::type ts(T v) {\n stringstream ss;\n ss << fixed << setprecision(15) << v;\n return ss.str();\n} /* default */\ntcT > str bit_vec(T t) { /* bit vector to string */\n str res = \"{\";\n For(i, 0, t.sz) res += ts(t[i]);\n res += \"}\";\n return res;\n}\nstr ts(ve<bool> v) { return bit_vec(v); }\ntemplate <size_t SZ>\nstr ts(bitset<SZ> b) {\n return bit_vec(b);\n} /* bit vector */\ntcTU > str ts(pair<T, U> p); /* pairs */\ntcT > typename enable_if<needs_output_v<T>, str>::type ts(\n T v); /* vectors, arrays */\ntcTU > str ts(pair<T, U> p) { return \"(\" + ts(p.fi) + \", \" + ts(p.se) + \")\"; }\ntcT > typename enable_if<is_iterable_v<T>, str>::type ts_sep(T v, str sep) {\n /* convert container to string w/ separator sep */\n bool fst = 1;\n str res = \"\";\n for (const auto &x : v) {\n if (!fst) res += sep;\n fst = 0;\n res += ts(x);\n }\n return res;\n}\ntcT > typename enable_if<needs_output_v<T>, str>::type ts(T v) {\n return \"{\" + ts_sep(v, \", \") + \"}\";\n}\n\n/* for nested DS */\ntemplate <int, class T>\ntypename enable_if<!needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {\n return {ts(v)};\n}\ntemplate <int lev, class T>\ntypename enable_if<needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {\n if (lev == 0 || !v.sz) return {ts(v)};\n ve<str> res;\n for (const auto &t : v) {\n if (res.sz) res.back() += \",\";\n ve<str> tmp = ts_lev<lev - 1>(t);\n res.insert(end(res), all(tmp));\n }\n For(i, 0, res.sz) {\n str bef = \" \";\n if (i == 0) bef = \"{\";\n res[i] = bef + res[i];\n }\n res.back() += \"}\";\n return res;\n}\n} /* namespace ToString */\n/*}}}*/\n\ninline namespace Output { /*{{{*/\ntemplate <class T>\nvoid pr_sep(ostream &os, str, const T &t) {\n os << ts(t);\n}\ntemplate <class T, class... U>\nvoid pr_sep(ostream &os, str sep, const T &t, const U &...u) {\n pr_sep(os, sep, t);\n os << sep;\n pr_sep(os, sep, u...);\n}\n/* print w/ no spaces */\ntemplate <class... T>\nvoid pr(const T &...t) {\n pr_sep(cout, \"\", t...);\n}\n/* print w/ spaces, end with newline */\nvoid ps() { cout << \"\\n\"; }\ntemplate <class... T>\nvoid ps(const T &...t) {\n pr_sep(cout, \" \", t...);\n ps();\n}\n/* debug to cerr */\ntemplate <class... T>\nvoid dbg_out(const T &...t) {\n pr_sep(cerr, \" | \", t...);\n cerr << endl;\n}\nvoid loc_info(int line, str names) {\n cerr << \"Line(\" << line << \") -> [\" << names << \"]: \";\n}\ntemplate <int lev, class T>\nvoid dbgl_out(const T &t) {\n cerr << \"\\n\\n\" << ts_sep(ts_lev<lev>(t), \"\\n\") << \"\\n\" << endl;\n}\n} /* namespace Output */\n/*}}}}}}}}}*/\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.sz;\n int e = 18;\n\n // we can do sliding window\n // the question is how do we undo an or\n // one way would be to have a sparse or table\n\n vvi sp(e, vi(n)); // dp[i][j] = or{nums[i:i+(1<<j)]}\n sp[0].assign(all(nums));\n\n for(int p=0; p+1<e; ++p) {\n for(int i=0; i<n; ++i) {\n sp[p+1][i] = sp[p][i] | (((i+(1<<p)) < n) ? sp[p][i+(1<<p)] : 0);\n }\n }\n\n vi lp2(n, 0);\n for(int i=2; i<n; ++i) lp2[i] = (i==(i&-i)) + lp2[i-1];\n\n // or of [l,r)\n auto qry = [&](auto l, auto r) {\n if(l >= r) return 0;\n int p = lp2[r-l];\n return sp[p][l] | sp[p][r-(1<<p)];\n };\n\n int res = INT_MAX;\n\n for(int cv=0,li=0,ri=0; li<n;) {\n // ri is the first index where we go over\n while(ri < n && (cv|nums[ri])<=k) { cv |= nums[ri]; ++ri; }\n\n if(li < ri) cmin(res, abs(k-cv));\n if(ri < n) cmin(res, abs(k-(cv | nums[ri])));\n\n ++li, cmax(ri, li);\n cv = qry(li, ri);\n }\n\n return res;\n }\n};\n", "memory": "133888" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& arr, int k) {\n unordered_set<int>st;\n int n=arr.size();\n for(int i=0;i<n;i++)\n {\n int pre_or=0, curr_or=arr[i],j=i-1;\n st.insert(arr[i]);\n while(j>=0 && curr_or!=pre_or) \n {\n curr_or|=arr[j];\n pre_or|=arr[j];\n st.insert(curr_or);\n j--;\n }\n }\n int mn=INT_MAX;\n for(auto s:st) mn=min(abs(s-k),mn);\n return mn;\n }\n};\n// 101\n// 100\n// 1001\n// k = 0011\n// 0001\n// 0010\n// 0100\n// 0101", "memory": "137639" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n set<int>s;\n for(int i = 0; i < nums.size(); i++) {\n s.insert(nums[i]);\n for(int j = i - 1; j >= 0; j--) {\n if((nums[i] | nums[j]) == nums[j]) break;\n nums[j] |= nums[i];\n s.insert((nums[j]));\n }\n }\n int ans = 1e9;\n auto iter = s.lower_bound(k);\n if(iter != s.end()) {\n ans = min(ans, abs(*iter-k));\n }\n if(iter != s.begin()) {\n iter--;\n ans = min(ans, abs(*iter-k));\n }\n return ans;\n }\n};", "memory": "141390" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& arr, int k) {\n int n=arr.size();\n set<int>st;\n for (int i=0;i<arr.size();i++)\n {\n int curr=arr[i];\n int prev=0;\n st.insert(curr);\n int j=i-1;\n while (j>=0 && curr!=prev)\n {\n curr=curr|arr[j];\n prev=prev|arr[j];\n st.insert(curr);\n j--;\n }\n }\n int mini=INT_MAX;\n for (auto it:st)mini=min(mini,abs(it-k));\n return mini;\n }\n};", "memory": "145141" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int getsum(vector<int> num){\n int sum=0;\n for(int i=0;i<32;i++){\n if(num[i]>0){\n sum+=(1<<i);\n }\n }\n return sum;\n }\n int minimumDifference(vector<int>& v, int k) {\n int n=v.size();\n if(n==1){\n return abs(k-v[0]);\n }\n vector<int> num(32,0);\n \n // for(int a:v){\n // cout<<a<<\" \";\n // }\n int i=0,j=0;\n int ans=INT_MAX;\n int sum=0;\n while(i<n){\n // cout<<i<<\" \"<<j<<\" \"<<sum<<endl;\n if(sum<k){\n // cout<<\"yes\";\n\n if(j>=n) {\n return ans;\n }\n sum=sum | v[j];\n for(int it=0;it<32;it++){\n if(v[j] & (1<<it)){\n num[it]++;\n }\n }\n ans=min(ans,abs(sum-k));\n j++;\n }\n else if(sum==k){\n return 0;\n }\n else{\n if(i==j){\n i++;\n j++;\n sum=0;\n num.resize(32,0);\n }\n else{\n // i++;\n for(int it=0;it<32;it++){\n if(v[i] & (1<<it)){\n num[it]--;\n }\n }\n sum=getsum(num);\n i++;\n if(i!=j) ans=min(ans,abs(sum-k));\n }\n }\n }\n return ans;\n }\n};", "memory": "148893" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(0);\n int n=nums.size();\n vector<array<int,32>> freq(n);\n for(int k=0;k<n;k++){\n int v=nums[k];\n for(int i=0;i<32;i++){\n if(k>0)\n freq[k][i]=freq[k-1][i];\n if((v>>i)&1){\n freq[k][i]++;\n }\n }\n }\n int ans=INT_MAX;\n\n auto get_ans=[&](int s,int r){\n int l=s;\n int cur=INT_MAX;\n while(l<=r){\n int t=0;\n int mid=l+(r-l)/2;\n if(s==0){\n for(int i=0;i<32;i++){\n if(freq[mid][i])\n t|=(1<<i);\n } \n }else{\n for(int i=0;i<32;i++){\n if((freq[mid][i]-freq[s-1][i])>0)\n t|=(1<<i);\n } \n }\n cur=min(cur,abs(t-k));\n if(t<k){\n l=mid+1;\n }else{\n r=mid-1;\n }\n }\n return cur;\n };\n\n for(int i=0;i<n;i++){\n int val=get_ans(i,n-1);\n ans=min(ans,val);\n }\n return ans;\n }\n};", "memory": "152644" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "static constexpr int M = 32;\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& a, int k) {\n\n cin.sync_with_stdio(false);\n cin.tie(nullptr);\n int n = a.size();\n vector<array<int, 32>> nx(n + 1);\n nx[n].fill(n);\n\n for (int i = n - 1; i >= 0; i--)\n {\n nx[i] = nx[i + 1];\n\n for (int j = 0; j < M; j++)\n {\n if ((1 << j) & a[i])\n nx[i][j] = i;\n }\n }\n\n int ret = 2e9;\n auto upd = [&](int cur)\n {\n // cout << \"upd \" << cur << endl;\n ret = min(ret, abs(k - cur));\n };\n\n auto follow = [&](int pos)\n {\n int cur = a[pos];\n upd(cur);\n \n while (pos < n and cur <= k)\n {\n int nx_pos = n;\n for (int i = 0; i < M; i++)\n {\n if ((1 << i) & cur)\n continue;\n nx_pos = min(nx_pos, nx[pos][i]);\n }\n\n// cout << cur << \" \" << pos << \" -> \" << nx_pos << endl;\n if (nx_pos == n)\n break;\n pos = nx_pos;\n cur |= a[pos];\n upd(cur);\n }\n\n };\n\n for (int i = 0; i < n; i++)\n {\n follow(i);\n }\n\n return ret;\n }\n};", "memory": "156395" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n const int INF = INT_MAX / 2; \n int minimumDifference(vector<int>& nums, int k) {\n vector<array<int,32>> ones(nums.size()); \n for(int i = 0;i<nums.size();i++) {\n if(i > 0) [[likely]] {\n ones[i] = ones[i-1]; \n }\n int x = nums[i]; \n for(int j = 0;j<32;j++) {\n if(x & (1<<j)) {\n ones[i][j] += 1; \n }\n }\n }\n int ret = INF; \n for(int i = 0;i<nums.size();i++) {\n int x = nums[i]; \n int l = 0; \n int r = i; \n while(l <= r) {\n int m = l + (r-l) / 2; \n int t = 0; \n for(int j = 0;j<32;j++) {\n int one = ones[i][j] - ones[m][j]; \n if(nums[m] & (1<<j)) {\n one += 1; \n }\n if(one > 0) {\n t |= 1<<j; \n }\n }\n if(t > k) {\n ret = min(ret, t - k); \n l = m + 1; \n } else if(t < k) {\n ret = min(ret, k - t); \n r = m - 1; \n } else {\n return 0; \n }\n }\n }\n return ret; \n }\n};", "memory": "160146" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n const int INF = INT_MAX / 2; \n int minimumDifference(vector<int>& nums, int k) {\n vector<array<int,32>> ones(nums.size()); \n for(int i = 0;i<nums.size();i++) {\n if(i > 0) [[likely]] {\n ones[i] = ones[i-1]; \n }\n int x = nums[i]; \n for(int j = 0;j<32;j++) {\n if(x & (1<<j)) {\n ones[i][j] += 1; \n }\n }\n }\n int ret = INF; \n for(int i = 0;i<nums.size();i++) {\n int x = nums[i]; \n int l = 0; \n int r = i; \n while(l <= r) {\n int m = l + (r-l) / 2; \n int t = 0; \n for(int j = 0;j<32;j++) {\n int one = ones[i][j] - ones[m][j]; \n if(nums[m] & (1<<j)) {\n one += 1; \n }\n if(one > 0) {\n t |= 1<<j; \n }\n }\n if(t > k) {\n ret = min(ret, t - k); \n l = m + 1; \n } else if(t < k) {\n ret = min(ret, k - t); \n r = m - 1; \n } else {\n return 0; \n }\n }\n }\n \n /*for(int i = 0;i<nums.size();i++) {\n int v = nums[i]; \n for(int j = i;j<nums.size();j++) {\n v &= nums[j]; \n cout<<\"l= \"<<i<<\" r= \"<<j<<\" v = \"<<v<<endl;\n }\n }*/\n\n return ret; \n }\n};", "memory": "163898" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n=nums.size();\n vector<array<int,32>> freq(n);\n for(int k=0;k<n;k++){\n int v=nums[k];\n for(int i=0;i<32;i++){\n if(k>0)\n freq[k][i]=freq[k-1][i];\n if((v>>i)&1){\n freq[k][i]++;\n }\n }\n }\n int ans=INT_MAX;\n\n auto get_ans=[&](int s,int r){\n int l=s;\n int cur=INT_MAX;\n while(l<=r){\n int t=0;\n int mid=l+(r-l)/2;\n if(s==0){\n for(int i=0;i<32;i++){\n if(freq[mid][i])\n t|=(1<<i);\n } \n }else{\n for(int i=0;i<32;i++){\n if((freq[mid][i]-freq[s-1][i])>0)\n t|=(1<<i);\n } \n }\n cur=min(cur,abs(t-k));\n if(t<k){\n l=mid+1;\n }else{\n r=mid-1;\n }\n }\n return cur;\n };\n\n for(int i=0;i<n;i++){\n int val=get_ans(i,n-1);\n ans=min(ans,val);\n }\n return ans;\n }\n};", "memory": "167649" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "static constexpr int M = 32;\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& a, int k) {\n int n = a.size();\n vector<array<int, 32>> nx(n + 1);\n nx[n].fill(n);\n\n for (int i = n - 1; i >= 0; i--)\n {\n nx[i] = nx[i + 1];\n\n for (int j = 0; j < M; j++)\n {\n if ((1 << j) & a[i])\n nx[i][j] = i;\n }\n }\n\n int ret = 2e9;\n auto upd = [&](int cur)\n {\n // cout << \"upd \" << cur << endl;\n ret = min(ret, abs(k - cur));\n };\n\n auto follow = [&](int pos)\n {\n int cur = a[pos];\n upd(cur);\n \n while (pos < n and cur <= k)\n {\n int nx_pos = n;\n for (int i = 0; i < M; i++)\n {\n if ((1 << i) & cur)\n continue;\n nx_pos = min(nx_pos, nx[pos][i]);\n }\n\n// cout << cur << \" \" << pos << \" -> \" << nx_pos << endl;\n if (nx_pos == n)\n break;\n pos = nx_pos;\n cur |= a[pos];\n upd(cur);\n }\n\n };\n\n for (int i = 0; i < n; i++)\n {\n follow(i);\n }\n\n return ret;\n }\n};", "memory": "171400" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n // use the fact that or of subarray always inc with size\n int find_greatest(vector<vector<int>> &prefix, vector<int> &nums, int k , int idx , int n)\n {\n int ans = nums[idx];\n int low = 0 , high = idx;\n while(low <= high)\n {\n int mid = (low+high)/2;\n int bitwiseOr = find_Or(prefix, mid, idx);\n if(bitwiseOr <= k)\n {\n ans = bitwiseOr;\n high = mid-1;\n }\n else low = mid+1;\n }\n return abs(k-ans);\n }\n int find_smallest(vector<vector<int>> &prefix, vector<int> &nums , int k , int idx , int n)\n {\n int ans = nums[idx];\n int low = 0 , high = idx;\n while(low <= high)\n {\n int mid = (low+high)/2;\n int bitwiseOr = find_Or(prefix, mid, idx);\n if(bitwiseOr >= k)\n {\n ans = bitwiseOr;\n low = mid+1;\n }\n else high = mid-1;\n }\n return abs(k-ans);\n }\n int find_Or(vector<vector<int>> &prefix , int st , int end)\n {\n int ans = 0;\n for(int i = 0;i<31;i++)\n {\n int cnt = prefix[i][end];\n if(st) cnt-= prefix[i][st-1];\n if(cnt) ans += (1<<i);\n }\n return ans;\n }\n int minimumDifference(vector<int>& nums, int k) \n {\n int ans = INT_MAX;\n int n = nums.size();\n vector<vector<int>> prefix(31 , vector<int>(n , 0));\n for(int i = 0;i<31;i++)\n {\n for(int j = 0;j<n;j++) prefix[i][j] = ((nums[j]&(1<<i)) != 0);\n for(int j = 1;j<n;j++) prefix[i][j] += prefix[i][j-1];\n }\n \n for(int i = 0;i<n;i++) \n {\n ans = min(ans , min(find_greatest(prefix, nums, k, i, n) , find_smallest(prefix, nums, k, i, n)));\n }\n return ans;\n }\n};", "memory": "175151" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n // Initialize a vector to store the current set of ORs\n // Using a vector of integers since the number of unique ORs is small\n vector<int> prev_or_set;\n int min_diff = INT32_MAX;\n \n for(int num : nums){\n // Temporary set to store new ORs for the current position\n vector<int> current_or_set;\n // Start with the current number as a new subarray\n current_or_set.push_back(num);\n \n // Update ORs with previous subarrays\n for(auto &prev_or : prev_or_set){\n int new_or = prev_or | num;\n // To avoid duplicates, only add if it's different\n if(current_or_set.empty() || current_or_set.back() != new_or){\n current_or_set.push_back(new_or);\n }\n }\n \n // Remove duplicates by sorting and erasing\n sort(current_or_set.begin(), current_or_set.end());\n current_or_set.erase(unique(current_or_set.begin(), current_or_set.end()), current_or_set.end());\n \n // Update the previous OR set\n prev_or_set = current_or_set;\n \n // For each OR value in the current set, update min_diff\n for(auto &current_or : prev_or_set){\n int diff = abs(k - current_or);\n if(diff < min_diff){\n min_diff = diff;\n // Early exit if exact match is found\n if(min_diff == 0){\n return 0;\n }\n }\n }\n }\n \n return min_diff;\n }\n};\n", "memory": "178903" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "struct tab{\n int lg = 17;\n vector<vector<int>> a;\n tab(vector<int>& aa){\n int n = aa.size();\n a.resize(n, vector<int>(lg));\n for (int i = 0; i < n;i ++)\n a[i][0] = aa[i];\n for (int j =0; j < lg - 1; j++)\n for (int i = 0; i + (1 << j) < n; i++)\n a[i][j + 1] = a[i][j] | a[i + (1 << j)][j];\n }\n int get(int l, int r) {\n int c = log2(r - l);\n return a[l][c] | a[r - (1 << c)][c];\n }\n};\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& a, int k) {\n int n = a.size();\n tab t(a);\n int ans = 1e9;\n int j = 0;\n for (int i = 0 ; i < n; i++){\n while (j < i && t.get(j + 1, i + 1) >= k)\n j++;\n ans = min(ans, abs(k - t.get(j, i + 1)));\n if (j < i)\n ans = min(ans, abs(k - t.get(j + 1, i + 1)));\n }\n return ans;\n }\n};", "memory": "182654" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n unordered_set<int> prev, curr;\n int ans = INT_MAX;\n\n for(int i: nums){\n curr = {i};\n for(int j: prev) curr.insert(i | j);\n prev = curr;\n for(int j: prev) ans = min(ans, abs(k - j));\n }\n\n return ans;\n }\n};", "memory": "186405" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n unordered_set<int> prev, curr;\n int ans = INT_MAX;\n\n for(int i: nums){\n curr = {i};\n for(int j: prev) curr.insert(i | j);\n prev = curr;\n for(int j: prev) ans = min(ans, abs(k - j));\n }\n\n return ans;\n }\n};", "memory": "190156" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "struct tab{\n int lg = 17;\n vector<vector<int>> a;\n tab(vector<int> aa){\n int n = aa.size();\n a.resize(n, vector<int>(lg));\n for (int i = 0; i < n;i ++)\n a[i][0] = aa[i];\n for (int j =0; j < lg - 1; j++)\n for (int i = 0; i + (1 << j) < n; i++)\n a[i][j + 1] = a[i][j] | a[i + (1 << j)][j];\n }\n int get(int l, int r) {\n int c = log2(r - l);\n return a[l][c] | a[r - (1 << c)][c];\n }\n};\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& a, int k) {\n int n = a.size();\n tab t(a);\n int ans = 1e9;\n int j = 0;\n for (int i = 0 ; i < n; i++){\n while (j < i && t.get(j + 1, i + 1) >= k)\n j++;\n ans = min(ans, abs(k - t.get(j, i + 1)));\n if (j < i)\n ans = min(ans, abs(k - t.get(j + 1, i + 1)));\n }\n return ans;\n }\n};", "memory": "193908" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "struct tab{\n int lg = 20;\n vector<vector<int>> a;\n tab(vector<int> aa){\n int n = aa.size();\n a.resize(n, vector<int>(lg));\n for (int i = 0; i < n;i ++)\n a[i][0] = aa[i];\n for (int j =0; j < lg - 1; j++)\n for (int i = 0; i + (1 << j) < n; i++)\n a[i][j + 1] = a[i][j] | a[i + (1 << j)][j];\n }\n int get(int l, int r) {\n int c = log2(r - l);\n return a[l][c] | a[r - (1 << c)][c];\n }\n};\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& a, int k) {\n int n = a.size();\n tab t(a);\n int ans = 1e9;\n int j = 0;\n for (int i = 0 ; i < n; i++){\n while (j < i && t.get(j + 1, i + 1) >= k)\n j++;\n ans = min(ans, abs(k - t.get(j, i + 1)));\n if (j < i)\n ans = min(ans, abs(k - t.get(j + 1, i + 1)));\n }\n return ans;\n }\n};", "memory": "197659" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) \n {\n vector<int> A(32,0);\n vector<string> B;\n int mini=INT_MAX,n=nums.size();\n\n for(int i=0;i<n;++i)\n B.push_back(bitset<32>(nums[i]).to_string());\n\n int currl=0,curr=0;\n\n for(int i=0;i<n;++i)\n {\n while (curr>=k && currl!=i)\n { \n mini=min(mini,abs(k-curr));\n for(int j=0;j<32;++j)\n A[j]-=(B[currl][j]-'0');\n \n currl++;\n \n if (currl==i)\n break;\n curr=0;\n\n for(int j=0;j<32;++j)\n if (A[j])\n curr+=(pow(2,31-j));\n \n mini=min(mini,abs(k-curr));\n }\n\n for(int j=0;j<32;++j)\n A[j]+=(B[i][j]-'0');\n\n curr=0;\n for(int j=0;j<32;++j)\n if (A[j])\n curr+=(pow(2,31-j));\n \n // currl=i;\n mini=min(mini,abs(k-curr));\n }\n\n while (curr>=k && currl!=n-1)\n { \n mini=min(mini,abs(k-curr));\n for(int j=0;j<32;++j)\n A[j]-=(B[currl][j]-'0');\n \n currl++;\n if(currl==n)\n break;\n curr=0;\n\n for(int j=0;j<32;++j)\n if (A[j])\n curr+=(pow(2,31-j));\n \n mini=min(mini,abs(k-curr));\n }\n\n\n return mini=min(mini,abs(k-curr));\n }\n};", "memory": "201410" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int res = abs(nums[0] - k);\n vector<int> arr = {nums[0]};\n for (int num: nums) {\n vector<int> tmp = {num};\n res = min(res, abs(num - k));\n for (int pre : arr) {\n tmp.push_back(pre | num);\n res = min(res, abs((pre | num) - k));\n }\n tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());\n arr = tmp;\n }\n return res;\n }\n};", "memory": "205161" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Segtreenode {\npublic:\n int l, r;\n int val;\n Segtreenode *left, *right;\n Segtreenode(int l, int r) {\n this->l = l;\n this->r = r;\n left = NULL;\n right = NULL;\n }\n};\n\nclass SegTree {\npublic:\n Segtreenode *makeTree(vector<int> &arr, int l, int r) {\n if(l == r) {\n Segtreenode* node = new Segtreenode(l, r);\n node->val = arr[l];\n return node;\n }\n int mid = l + (r-l)/2;\n Segtreenode *leftnode = makeTree(arr,l,mid);\n Segtreenode* rightnode = makeTree(arr,mid+1,r);\n Segtreenode *node = new Segtreenode(l,r);\n node->left = leftnode;\n node->right = rightnode;\n node->val = leftnode->val | rightnode->val;\n return node;\n }\n int query(int l, int r, Segtreenode *root) {\n // if(root == NULL) {\n // return 0;\n // }\n int rootl = root->l;\n int rootr = root->r;\n if(l == rootl && r == rootr) {\n return root->val;\n }\n int mid = (rootl) + (rootr - rootl)/2;\n if(r <= mid) {\n return query(l,r,root->left);\n }\n if(l >= mid+1) {\n return query(l,r,root->right);\n }\n int res = query(l,mid,root->left) | query(mid+1,r,root->right);\n return res;\n }\n};\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& arr, int k) {\n int n = arr.size();\n SegTree *tree = new SegTree();\n Segtreenode *root = tree->makeTree(arr, 0, n-1);\n int ans = INT_MAX;\n int l = 0;\n for (int r = 0; r < n; r++) {\n\n while(l <= r && tree->query(l,r,root) > k) {\n ans = min(ans, abs(tree->query(l,r,root) - k));\n l++;\n }\n if(l <= r) {\n ans = min(ans, abs(tree->query(l,r,root) - k));\n }\n }\n return ans;\n }\n};", "memory": "208913" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(0);\n cin.tie(0); cout.tie(0);\n unordered_set<int> set, temp;\n set.insert(nums[0]);\n int minimum = abs(nums[0]-k);\n for(int i=1; i<nums.size(); i++) {\n temp.clear();\n for(auto ele: set) {\n temp.insert(ele|nums[i]);\n minimum = min(minimum, abs((ele|nums[i])-k));\n }\n temp.insert(nums[i]);\n minimum = min(minimum, abs(nums[i]-k));\n set = temp;\n }\n return minimum;\n }\n};", "memory": "212664" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution \n{\npublic:\n int minimumDifference(vector<int>& nums, int k) \n {\n std::unordered_set<int> setNew, setOld;\n int n = nums.size(), result = INT_MAX;\n\n for (int i = 0; i < n; ++i)\n {\n setNew.clear();\n for (auto x : setOld)\n setNew.insert(x | nums[i]);\n setNew.insert(nums[i]);\n\n for (auto x : setNew)\n result = std::min(result, std::abs(k - x));\n \n setOld = setNew;\n }\n\n return result;\n }\n};", "memory": "216415" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int ans = 0x7fffffff;\n unordered_set<int> ps, cs;\n for (int x : nums) {\n cs.insert(x);\n for (int v : ps) cs.insert(v | x);\n for (int v : cs) ans = min(ans, abs(k - v));\n ps = cs;\n cs.clear();\n }\n return ans;\n }\n};", "memory": "220166" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> tmp;\n\n void up(int n, int ind){\n for(int i = 0; i < 32;i++){\n if((n&(1<<i)) != 0){\n tmp[i] = ind;\n }\n }\n }\n\n int minimumDifference(vector<int>& arr, int kk) { // arr = nums\n int target = kk;\n tmp.resize(32,-1);\n int n = arr.size(), ans = abs(target-arr[0]);\n up(arr[0],0);\n for(int i = 0;i < 32;i++){\n cout<<i<<\" -> \"<<tmp[i]<<\"\\n\";\n }\n for(int i = 1;i < n;i++){\n int k = arr[i];\n ans = min(ans,abs(target-arr[i]));\n priority_queue<int> pq(tmp.begin(),tmp.end());\n for(int j = 0; j < 32;j++){\n // cout<<tmp[2]<<\" - ijfijf\\n\";\n int tp = pq.top();pq.pop();\n // cout<<i<<\" - \"<<tp<<\"\\n\";\n if(tp < 0) break;\n k |= arr[tp];\n ans = min(ans,abs(target-k));\n }\n up(arr[i], i);\n }\n \n return ans;\n }\n};", "memory": "235171" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <vector>\n#include <cmath>\n#include <algorithm>\n#include <limits>\n\nusing namespace std;\n\nclass Node {\npublic:\n int l, r, value;\n Node* left;\n Node* right;\n\n Node(const vector<int>& arr, int l, int r) : l(l), r(r), left(nullptr), right(nullptr) {\n if (l == r) {\n value = arr[l];\n } else {\n int mid = (l + r) / 2;\n left = new Node(arr, l, mid);\n right = new Node(arr, mid + 1, r);\n value = left->value | right->value;\n }\n }\n\n ~Node() {\n delete left;\n delete right;\n }\n\n int find(int l, int r) {\n if (r < this->l || l > this->r) return 0;\n if (l <= this->l && this->r <= r) return value;\n return left->find(l, r) | right->find(l, r);\n }\n};\n\nclass Solution {\npublic:\n int minimumDifference(const vector<int>& nums, int k) {\n int N = nums.size();\n Node* root = new Node(nums, 0, N - 1);\n\n auto findSmallestGreaterThanOrEqual = [&](int startIdx, int target) -> int {\n if (root->find(startIdx, N - 1) < target) return N - 1;\n int l = startIdx, r = N - 1;\n while (l < r) {\n int mid = l + (r - l) / 2;\n int val = root->find(startIdx, mid);\n if (val == target) return mid;\n if (val < target) l = mid + 1;\n else r = mid;\n }\n return r;\n };\n\n int ans = numeric_limits<int>::max();\n for (int l = 0; l < N; ++l) {\n int idx = findSmallestGreaterThanOrEqual(l, k);\n\n if (idx - 1 >= l) {\n int closest = root->find(l, idx - 1);\n ans = min(ans, abs(k - closest));\n ans = min(ans, abs(k - (closest | nums[idx])));\n } else {\n int closest = root->find(l, idx);\n ans = min(ans, abs(k - closest));\n }\n }\n\n delete root;\n return ans;\n }\n};", "memory": "238923" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n unordered_set<int> st, st2;\n int ret = INT_MAX;\n for(int i=0;i<nums.size();i++){\n st.insert(0);\n for(int x : st){\n int orr = x | nums[i];\n ret = min(abs(orr - k), ret);\n st2.insert(orr);\n }\n st.clear();\n swap(st, st2);\n }\n return ret;\n }\n};", "memory": "242674" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <vector>\n#include <set>\n#include <map>\n#include <queue>\n#include <stack>\n#include <unordered_set>\n#include <unordered_map>\n#include <bitset>\n#include <limits>\n#include <iostream> \n#include <iomanip> \n#include <string>\n#include <sstream> \n#include <algorithm>\n#include <numeric>\n#include <complex>\n#include <functional>\n#include <cstring>\n#include <cmath>\n#include <cassert>\n\n\n#define INF (int)1000000007\n#define EPS 1e-9\n#define pb push_back\n#define mp make_pair\n#define all(c) c.begin(), c.end()\n#define forall(i,a,b) for(int i=a;i<(b);++i)\n#define trav(a,x) for(auto & a: x)\n#define in(a,b) ((b).find(a) != (b).end())\n#define sz(c) (int)(c).size()\n#define input(a) for(auto & x : a) cin >> x;\n\nusing namespace std;\n\ntypedef vector<int> vi;\ntypedef pair<int,int> ii;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef long long ll;\n\ntemplate<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }\ntemplate<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }\n\n#ifdef DEBUG\n#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)\n#define debug(args...) {dbg,args; clog<<endl;}\n#define print_( a ) for( auto & x : a ) clog << x << ' '; clog << '\\n';\n#define printPair_( a ) for( auto & x : a ) clog << '(' << x.first << ',' << x.second << ')' << ' '; clog << '\\n';\n#else\n#define trace(...) \n#define debug(args...) \n#define print_( a ) \n#define printPair_( a ) \n#endif\n\nstruct debugger {\n template<typename T> debugger& operator , (const T& x) { \n clog << x << \" \"; return *this; \n }\n}dbg;\n\ntemplate <typename Arg1>\nvoid __f(const char* name, Arg1&& arg1){\n cerr << name << \": \" << arg1 << endl;\n}\n\ntemplate <typename Arg1, typename... Args>\nvoid __f(const char* names, Arg1&& arg1, Args&&... args){\n const char* comma = strchr(names + 1, ',');\n cerr.write(names, comma - names) << \": \" << arg1 << \" | \";\n __f(comma + 1, args...);\n}\n\nauto ____ =[]() { std::ios::sync_with_stdio(0); cin.tie(0); return nullptr; }();\nclass Solution {\npublic:\n int minimumDifference(vector<int>& v, int k) {\n unordered_set<int> tot, p, q;\n for(auto x: v){\n q = {x};\n for(auto y: p){\n q.insert(y|x);\n }\n tot.insert(all(q));\n swap(p,q);\n }\n int ans = numeric_limits<int>::max();\n for(auto x: tot){\n amin(ans, abs(x-k));\n }\n return ans;\n\n \n }\n};\n", "memory": "246425" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "typedef long long ll;\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<int>> pref(n, vector<int>(30, 0));\n for (int i = 0; i < 30; i++) {\n if ((1 << i) & nums[0]) {\n pref[0][i]++;\n }\n }\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < 30; j++) {\n pref[i][j] = pref[i - 1][j];\n if ((1 << j) & nums[i]) {\n pref[i][j]++;\n }\n }\n }\n ll ans = 1e18;\n for (int i = 0; i < n; i++) {\n int lo = i, hi = n - 1;\n while (hi - lo > 1) {\n\n int mid = lo + (hi - lo) / 2;\n int x = 0;\n for (int j = 0; j < 30; j++) {\n if (i) {\n if (pref[mid][j] != pref[i - 1][j]) {\n x += (1 << j);\n }\n } else {\n if (pref[mid][j] != 0) {\n x += (1 << j);\n }\n }\n }\n\n if (x >= k) {\n hi = mid;\n } else {\n lo = mid + 1;\n }\n }\n\n ll cur = 0;\n for (int j = 0; j < 30; j++) {\n if (i) {\n if (pref[hi][j] != pref[i - 1][j]) {\n cur += (1ll << j);\n }\n } else {\n if (pref[hi][j]) {\n cur += (1ll << j);\n }\n }\n }\n ans = min(ans, abs(cur - k));\n cur = 0;\n for (int j = 0; j < 30; j++) {\n if (i) {\n if (pref[lo][j] != pref[i - 1][j]) {\n cur += (1ll << j);\n }\n } else {\n if (pref[lo][j]) {\n cur += (1ll << j);\n }\n }\n }\n ans = min(ans, abs(cur - k));\n if (lo && i != lo) {\n cur = 0;\n for (int j = 0; j < 30; j++) {\n if (i) {\n if (pref[lo - 1][j] != pref[i - 1][j]) {\n cur += (1ll << j);\n }\n } else {\n if (pref[lo - 1][j]) {\n cur += (1ll << j);\n }\n }\n }\n ans = min(ans, abs(cur - k));\n }\n \n }\n return ans;\n }\n};", "memory": "250176" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& k, int m) {\n int n=k.size();\n vector<vector<int>> v(n+1,vector<int>(31));\n for(int i=0;i<n;i++){\n int x=k[i];\n for(int j=0;j<31;j++){\n v[i+1][j]=v[i][j];\n if(x%2)v[i+1][j]++;\n x=x/2;\n }\n \n }\n \n int diff=1e9;\n for(int i=0;i<n;i++){\n int l=i;int r=n-1;int mid;\n while(l<=r){\n mid=(l+r)/2;\n int val=0;\n for(int j=0;j<31;j++){\n int a1=v[mid+1][j]-v[i][j];\n if(a1)val=val|(1<<j);\n }\n if(val>m){\n r=mid-1;\n }else l=mid+1;\n diff=min(diff,abs(val-m));\n }\n\n }\n return diff;\n }\n};", "memory": "253928" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& k, int m) {\n int n=k.size();\n vector<vector<int>> v(n+1,vector<int>(31));\n for(int i=0;i<n;i++){\n int x=k[i];\n for(int j=0;j<31;j++){\n v[i+1][j]=v[i][j];\n if(x%2)v[i+1][j]++;\n x=x/2;\n }\n \n }\n \n int diff=1e9;\n for(int i=0;i<n;i++){\n int l=i;int r=n-1;int mid;\n \n while(l<=r){\n mid=(l+r)/2;\n int val=0;\n for(int j=0;j<31;j++){\n int a1=v[mid+1][j]-v[i][j];\n if(a1)val=val|(1<<j);\n }\n if(val>m){\n r=mid-1;\n }else l=mid+1;\n \n diff=min(diff,abs(val-m));\n }\n\n }\n return diff;\n }\n};", "memory": "257679" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& k, int m) {\n int n=k.size();\n vector<vector<int>> v(n+1,vector<int>(31));\n for(int i=0;i<n;i++){\n int x=k[i];\n for(int j=0;j<31;j++){\n v[i+1][j]=v[i][j];\n if(x%2)v[i+1][j]++;\n x=x/2;\n }\n \n }\n \n int diff=1e9;\n for(int i=0;i<n;i++){\n int l=i;int r=n-1;int mid;\n while(l<=r){\n mid=(l+r)/2;\n int val=0;\n for(int j=0;j<31;j++){\n int a1=v[mid+1][j]-v[i][j];\n if(a1)val=val|(1<<j);\n }\n if(val>m){\n r=mid-1;\n }else l=mid+1;\n diff=min(diff,abs(val-m));\n }\n if(diff==0)break;\n\n }\n return diff;\n }\n};", "memory": "257679" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int len = nums.size();\n vector<vector<int>> digitCnt(len + 1, vector<int>(32, 0));\n for (int i = 1; i <= len; ++i) {\n for (int j = 0; j <= 31; ++j) {\n if ((nums[i - 1] >> j) & 1) {\n digitCnt[i][j] = digitCnt[i - 1][j] + 1;\n }\n else {\n\t\t\t\t\tdigitCnt[i][j] = digitCnt[i - 1][j];\n\t\t\t\t}\n }\n }\n int l = 0, r = 1;\n int minDiff = 1e9, res = 0;\n\n while (r <= len) {\n res = 0;\n if (l == r) {\n ++r;\n continue;\n }\n for (int j = 0; j <= 31; ++j) {\n if (digitCnt[r][j] - digitCnt[l][j]) {\n res += 1 << j;\n }\n }\n\n if (res == k) return 0;\n if (res > k) {\n minDiff = min(res - k, minDiff);\n l++;\n }\n else {\n minDiff = min(k - res, minDiff);\n r++;\n }\n }\n return minDiff;\n }\n};\n", "memory": "261430" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n \n vector<vector<int>> arr( nums.size(), vector<int>(32, 0) ); \n\n for( int j = 0; j < nums.size(); j++ )\n {\n for( int i = 0; i < 32; i++ )\n {\n if( nums[j] & (1<<i) )\n arr[j][i] = 1; \n }\n }\n\n int ans = INT_MAX; \n int prev = 0; \n\n vector<int> current(32, 0);\n\n for( int i = 0; i < nums.size(); i++ )\n {\n int num = 0; \n for( int j = 0; j < 32; j++ )\n {\n current[j] += arr[i][j]; \n if( current[j] > 0)\n num += (1<<j); \n }\n\n // cout << \"1 . Num: \" << num << \"Ans: \" << ans << endl; \n ans = min( ans, abs(num-k)); \n\n while( prev < i && num > k )\n {\n for( int i = 0; i < 32; i++ )\n {\n if( current[i] != 0 && current[i] == arr[prev][i])\n num -= (1<<i); \n current[i] -= arr[prev][i]; \n }\n\n ans = min( ans, abs(num-k)); \n prev++; \n }\n\n ans = min( ans, abs(num-k)); \n // cout << \"2 . Num: \" << num << \"Ans: \" << ans << endl; \n \n } \n\n return ans; \n }\n};", "memory": "265181" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n// using namespace __gnu_pbds;\n #define vb vector<bool>\n #define ff first\n #define ss second\n #define pb push_back\n #define gout(tno) cout << \"Case #\" << tno++ <<\": \"\n #define ld long double\n #define ll long long\n #define f(i, a, b) for (int(i) = int(a); (i) < int(b); ++(i))\n #define vi vector<int>\n #define vb vector<bool>\n #define pb push_back\n #define ub upper_bound\n #define lb lower_bound\n #define rall(x) x.rbegin(), x.rend()\n #define uniq(v) v.resize(unique(v.begin(), v.end()) - v.begin())\n #define scanv(v) for (int i = 0; i < v.size(); ++i) cin >> v[i];\n // #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>\n //order_of_key (k) //find_by_order(k) \n #define prDouble(x) cout<<fixed<<setprecision(10)<<x\n #define pii pair<int, int>\n #define vpii vector<pair<int, int> >\n #define w(x) int x; cin >> x; while(x--)\n #define FIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);\n #define setbits(x) __builtin_popcountll(x) //count number of setbits in a number\n #define max3(a, b, c) max((a), max((b), (c)))\n #define min3(a, b, c) min((a), min((b), (c)))\n #define mx_all(c) *max_element((c).begin(), (c).end())\n #define mn_all(c) *min_element((c).begin(), (c).end())\n #define all(x) x.begin(),x.end()\n #define siz(x) ((int)(x).size())\n #define yes cout<<\"Yes\"<<endl\n #define no cout<<\"No\"<<endl\n #define alice cout<<\"Alice\"<<endl\n #define bob cout<<\"Bob\"<<endl\n #define takahashi cout<<\"Takahashi\"<<endl\n #define aoki cout<<\"Aoki\"<<endl\n #define pb push_back\n #define vi vector<int>\n #define vb vector<bool>\n #define vs vector<string>\n #define vvi vector<vector<int>>\n #define djikstra priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>\n #define lld long double\n #define show(a) for (auto& (i) : (a)) cout << i<<\" \" ;\n #define itos to_string\n #define STOI stoi\n \n const lld pi = 3.1415926535897932;\n ll mul1(ll a,ll b,ll m){ll ans=0;while(b>0){if(b&1)ans=(ans+a)%m;b=b/2;a=(a+a)%m;}return ans;}\n ll mul(ll a,ll b,ll m){ return (a*b)%m;}\n ll accurateFloor(ll a, ll b) {ll val = a / b; while (val * b > a)val--;return val; }\n void yesno(bool xxx) {if(xxx) cout<<\"Yes\\n\"; else cout<<\"No\\n\";}\n ll nCr(ll n, ll r){if (n < r)return 0; if (r > n - r) r = n - r; ll ans = 1;ll i; for (i = 1; i <= r; i++) { ans *= n - r + i; ans /= i; } return ans;}\n ll binaryexp(ll a,ll b){ll ans=0;if(b<0)return 0; if(b==0)return 1;else if(b==1)return a; else if(b%2){ans=(a*(binaryexp((a*a),b/2)));}else ans=binaryexp((a*a),b/2);return ans;}\n int gcd(int x,int y){if(y==0)return x;else return gcd(y,x%y);}\n \n long long gcd(long long int a, long long int b) {if (b == 0) return a; return gcd(b, a % b);}\n \n // Function to return LCM of two numbers \n long long lcm(ll a, ll b){ return (a / gcd(a, b)) * b;}\n ll mod_add(ll a, ll b, ll m=1e9+7) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}\n ll expo(ll a, ll b, ll m) {ll res = 1; while (b > 0) {if (b & 1)res = mul(res , a,m) % m; a = mul(a , a,m) % m; b = b >> 1;} return res%m ;}\n ll modinv(ll a , ll m ) {return expo(a , m-2 , m)%m;} // m is prime /**\n// for questions involving segments, think of sweep line algorithm and binary search,\n// if segment tree with lazy prop^ does not seem to work .\n// think greedy wisely and not rush it over the algorithm\n// Use DSU for dynamically varying graphs,expanding compressing tree and cycles\n// for(int s=m;s;s=(s-1)&m) iterating through all subsets of mask m\n// Think about topological sortings whenever you see some sort of order or maybe independency\n// Think of Rolling Hash if Your solution is giving space or time limit exceeded for string matching\n// KMP algo for single pattern searching in text and Corasick algo for multiple pattern searching in Text.\n\nconst ll mod= 1e9+7;\n //1e9+7 //998244353\nconst ll mx=5e4+1,N=1e3+10;\nconst ll inf=1e9;\nclass Solution {\npublic:\n\n int chk(vector<vector<int>> &pf,int st,int ed,int k){\n int ans=0;\n f(bt,0,31){\n int cur=pf[ed][bt];\n if(st-1>=0) cur-=pf[st-1][bt];\n if(st==1 && ed==1)\n cout<<ed<<\" \"<<cur<<\" \"<<bt<<endl;\n if(cur) ans+=(1<<bt);\n }\n return (ans);\n }\n int minimumDifference(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<int>> pf(n,vector<int>(31));\n for(int i=0;i<31;i++){\n if((nums[0]>>i)&1ll) pf[0][i]=1;\n }\n\n for(int i=1;i<n;i++){\n f(bt,0,31){\n if((nums[i]>>bt)&1ll) pf[i][bt]=1;\n pf[i][bt]+=pf[i-1][bt];\n }\n }\n\n // f(i,0,n){\n // f(bt,0,3){\n // cout<<pf[i][bt]<<\" \";\n // }\n // cout<<endl;\n // }\n \n int ans=inf;\n vector<int> nxt(n);\n f(i,0,n){\n int l=i,r=n-1;\n nxt[i]=n-1;\n int res=-1;\n while(l<=r){\n int md=(l+r)/2;\n if(chk(pf,i,md,k)>=k){\n r=md-1;\n nxt[i]=md;\n res=md;\n }\n else{\n l=md+1;\n }\n \n }\n // cout<<res<<endl;\n }\n // show(nxt);\n // cout<<endl;\n\n f(i,0,n){\n int st=i;\n int ed=nxt[i];\n ans=min(ans,abs(chk(pf,st,ed,k)-k));\n if(ed-1>=st) ans=min(ans,abs(chk(pf,st,ed-1,k)-k));\n }\n return ans;\n\n\n }\n};\n\n\n// int main(){\n// int n,k;\n// cin>>n>>k;\n// vector<int> v(n);\n// f(i,0,n) cin>>v[i];\n// Solution s;\n// cout<<s.minimumDifference(v,k)<<endl;;\n\n// }", "memory": "265181" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int left = 0, n = nums.size(), right = 0, ans=INT_MAX;\n vector<int> cur(32);\n while(right<n){\n for(int i=0;i<32;i++){\n cur[i]+=(nums[right]&(1<<i))>0;\n }\n int temp = calc(cur);\n if(temp>k){\n ans = min(ans,temp-k);\n for(int i=0;i<32;i++){\n cur[i]-=(nums[right]&(1<<i))>0;\n }\n if(left<right){\n for(int i=0;i<32;i++){\n cur[i]-=(nums[left]&(1<<i))>0;\n }\n left++;\n }\n else left = right=right+1;\n }\n else{\n right++;\n ans = min(ans,k-temp);\n }\n }\n return ans;\n }\n int calc(vector<int>cur){\n int val=0;\n for(int i=0;i<32;i++){\n if(cur[i]>0) val^=1<<i;\n }\n return val;\n }\n};", "memory": "268933" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n // find all subarrays --> 30*N atmost 30 bits \n int n=nums.size(),ans=1e9;\n unordered_set<int>res,cur,cur1;\n\n for(auto i=0;i<n;i++){\n\n cur={nums[i]};\n \n for(auto j:cur1) cur.insert(j|nums[i]);\n\n cur1=cur; //cur1 as all possible \n\n for(auto j:cur1) res.insert(j);\n\n \n }\n\n for(auto i:res) ans=min(ans,abs(i-k));\n\n return ans;\n\n }\n};", "memory": "268933" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n // OR more only makes it larger\n int mx = 0;\n for (auto& x: nums) mx |= x;\n if (mx <= k) return k - mx;\n int ans = mx - k;\n \n unordered_set<int> S{0};\n \n for (int i = 0; i < nums.size(); i ++) {\n unordered_set<int> S2{0};\n for (auto& x: S) {\n ans = min(ans, abs((x | nums[i]) - k));\n S2.insert(x | nums[i]);\n }\n swap(S, S2);\n }\n return ans;\n \n }\n};", "memory": "272684" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n // for every index store the last time 1 has occured\n vector<int> v(31,-1);\n int minn = 1e9;\n for(int i = 0; i < n; i++){\n int val = nums[i];\n minn = min(minn,abs(k-val));\n map<int,int> p;\n for(int j = 0; j < 31; j++){\n if(v[j] != -1 && !(nums[i]&(1<<j))){\n p[-v[j]] += pow(2,j);\n }\n }\n for(auto l:p){\n val += l.second;\n minn = min(minn,abs(k-val));\n }\n for(int j = 0; j < 31; j++){\n if(nums[i]&(1<<j)){v[j] = i;}\n }\n }\n return minn;\n }\n};", "memory": "272684" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> bitsPrefix;\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n bitsPrefix.resize(n+1, vector<int>(33, 0));\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < 32; j++) {\n bitsPrefix[i+1][j] = bitsPrefix[i][j];\n }\n int pos = 31;\n while (nums[i]) {\n int cur = nums[i] % 2;\n nums[i] /= 2;\n if (cur) bitsPrefix[i+1][pos]++;\n pos--;\n }\n }\n\n\n int ans = INT_MAX;\n for (int i = 0; i < n; i++) {\n \n int l = i, r = n+1; // proper bounds, adjust later\n while (r - l > 1) {\n int m = l + (r-l)/2;\n\n // extract OR value from prefix sums\n int weight = 1;\n int orVal = 0;\n for (int j = 31; j >= 2; j--) {\n if (bitsPrefix[m][j] - bitsPrefix[i][j]) {\n orVal += weight;\n }\n weight *= 2;\n }\n // cout << orVal << \" \";\n if (orVal >= k) r = m;\n else l = m;\n }\n // cout << endl;\n\n if (l != i) {\n int weight = 1;\n int orVal = 0;\n for (int j = 31; j >= 2; j--) {\n if (bitsPrefix[l][j] - bitsPrefix[i][j]) {\n orVal += weight;\n }\n weight *= 2;\n }\n ans = min(ans, abs(orVal - k));\n }\n\n if (r != n+1) {\n int weight = 1;\n int orVal = 0;\n for (int j = 31; j >= 2; j--) {\n if (bitsPrefix[r][j] - bitsPrefix[i][j]) {\n orVal += weight;\n }\n weight *= 2;\n }\n ans = min(ans, abs(orVal - k));\n }\n }\n return ans;\n\n /*\n for (int i = 0; i < n+1; i++) {\n for (int j = 0; j < 32; j++) {\n cout << bitsPrefix[i][j] << \" \";\n }\n cout << endl;\n }\n return 0;\n */\n } \n};\n\n/*\nint bitsPrefix[n][32];\n\nExample 1: \n\nnums = [1,2,4,5]\nnums bits: 001, 010, 100, 101\n\n\nbitsPrefix: 000 -> 001 -> 011 -> 111 -> 212\n\n\nExample 2:\n\nnums = [1,3,1,3]\nnums bits: 01, 11, 01, 11\n\nbitsPrefix: 01 -> 12 -> 13 -> 24\n\n*/", "memory": "276435" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) \n {\n unordered_set<int> prev, curr;\n set<int> res;\n for(auto i:nums)\n {\n curr={i};\n for(auto j:prev) curr.insert(j|i);\n for(auto j:curr) res.insert(j);\n prev=curr;\n }\n int mini = INT_MAX;\n auto lb = res.lower_bound(k);\n\n if(lb!=res.end())\n mini=min(mini,abs( k - (*lb) ));\n\n if(lb!=res.begin())\n {\n --lb;\n mini=min(mini,abs( k - (*lb) ));\n }\n return mini;\n\n\n }\n};", "memory": "276435" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) \n {\n unordered_set<int> prev, curr;\n set<int> res;\n for(auto i:nums)\n {\n curr={i};\n for(auto j:prev) curr.insert(j|i);\n for(auto j:curr) res.insert(j);\n prev=curr;\n }\n // int lb=lower_bound(res.begin(),res.end());\n int mini = INT_MAX;\n for(auto i:res) mini=min(mini,abs(k-i));\n return mini;\n\n\n }\n};", "memory": "280186" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(0);\n cin.tie(0); cout.tie(0);\n unordered_set<int> freqset, set, temp;\n set.insert(nums[0]), freqset.insert(nums[0]);\n for(int i=1; i<nums.size(); i++) {\n temp.clear();\n for(auto ele: set) {\n temp.insert(ele|nums[i]);\n }\n temp.insert(nums[i]);\n for(auto ele: temp) {\n freqset.insert(ele);\n }\n set = temp;\n }\n int minimum = INT_MAX;\n for(auto ele: freqset) {\n minimum = min(minimum, abs(k-ele));\n }\n return minimum;\n }\n};", "memory": "283938" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n unordered_set<int> freqset, set, temp;\n set.insert(nums[0]), freqset.insert(nums[0]);\n for(int i=1; i<nums.size(); i++) {\n temp.clear();\n for(auto ele: set) {\n temp.insert(ele|nums[i]);\n }\n temp.insert(nums[i]);\n for(auto ele: temp) {\n freqset.insert(ele);\n }\n set = temp;\n }\n int minimum = INT_MAX;\n for(auto ele: freqset) {\n minimum = min(minimum, abs(k-ele));\n }\n return minimum;\n }\n};", "memory": "287689" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_set<int> ans ; \n unordered_set<int> till_now;\n unordered_set<int> farji; \n for(int i = 0 ; i < n ; i++){\n farji.insert(nums[i]);\n for(auto &j:till_now){\n farji.insert(nums[i]|j);\n }\n till_now = farji; \n farji.clear(); \n for(auto &j: till_now){\n ans.insert(j); \n }\n }\n int an_s = INT_MAX; \n for(auto &i:ans){\n an_s = min(an_s,abs(i-k)); \n }\n return an_s ; \n }\n};", "memory": "291440" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#define ll long long\nclass Solution {\npublic:\n int minimumDifference(vector<int>& arr, int k) {\n int ans = INT_MAX;\n unordered_set<int> curr, prev, res;\n prev.insert(0);\n for(int i = 0; i < arr.size(); i++){\n int x = arr[i];\n for(auto y : prev){\n curr.insert(x | y);\n }\n curr.insert(x);\n for(auto y : curr){\n ans = min(ans, abs(k-y));\n res.insert(y);\n }\n prev = curr;\n curr.clear();\n }\n return ans;\n }\n};", "memory": "295191" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#define ll long long\nclass Solution {\npublic:\n int minimumDifference(vector<int>& arr, int k) {\n unordered_set<ll> curr, prev, res;\n prev.insert(0);\n for(int i = 0; i < arr.size(); i++){\n int x = arr[i];\n for(auto y : prev){\n curr.insert(x | y);\n }\n curr.insert(x);\n for(auto y : curr){\n res.insert(y);\n }\n prev = curr;\n curr.clear();\n }\n int ans = INT_MAX;\n for(auto i : res){\n ans = min(ans, abs(k-(int)i));\n }\n return ans;\n }\n};", "memory": "298943" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nclass Solution {\npublic:\n int minimumDifference(vector<int>& v, int k) {\n ll n=v.size();\n ll head=-1,tail=0,ans=1e17;\n vvi pref(33,vi(n+1));\n REP(i,0,32){\n REP(j,0,n-1){\n pref[i][j+1]=pref[i][j];\n if(v[j]&(1LL<<i)){\n pref[i][j+1]++;\n }\n }\n }\n ll tem=0;\n while(tail<n){\n while(head+1<n){\n ll nwtem=tem|v[head+1];\n if(nwtem>k)break;\n tem|=v[head+1];\n head++;\n }\n if(head>=tail)ans=min(ans,abs(tem-k));\n if(head+1<n){\n ll nwtem=tem|v[head+1];\n ans=min(ans,abs(nwtem-k));\n }\n // cout<<ans<<'\\n';\n if(head<tail){\n tem=0;\n head++;\n tail++;\n }else if(head==tail){\n tail++;\n tem=0;\n }\n else{\n tem=0;\n REP(i,0,32){\n tem|=((ll)((pref[i][head+1]- pref[i][tail+1])>=1)<<i);\n }\n tail++;\n }\n }\n return ans;\n }\n};", "memory": "302694" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nclass Solution {\npublic:\n int minimumDifference(vector<int>& v, int k) {\n ll n=v.size();\n ll head=-1,tail=0,ans=1e17;\n vvi pref(33,vi(n+1));\n REP(i,0,32){\n REP(j,0,n-1){\n pref[i][j+1]=pref[i][j];\n if(v[j]&(1LL<<i)){\n pref[i][j+1]++;\n }\n }\n }\n ll tem=0;\n while(tail<n){\n while(head+1<n){\n ll nwtem=tem|v[head+1];\n if(nwtem>k)break;\n tem|=v[head+1];\n head++;\n }\n if(head>=tail)ans=min(ans,abs(tem-k));\n if(head+1<n){\n ll nwtem=tem|v[head+1];\n ans=min(ans,abs(nwtem-k));\n }\n // cout<<ans<<'\\n';\n if(head<tail){\n tem=0;\n head++;\n tail++;\n }\n else{\n tem=0;\n REP(i,0,32){\n tem|=((ll)((pref[i][head+1]- pref[i][tail+1])>=1)<<i);\n }\n tail++;\n }\n }\n return ans;\n }\n};", "memory": "302694" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nclass Solution {\npublic:\n int minimumDifference(vector<int>& v, int k) {\n ll n=v.size();\n ll head=-1,tail=0,ans=1e17;\n vvi pref(35,vi(n+1));\n REP(i,0,35-1){\n REP(j,0,n-1){\n pref[i][j+1]=pref[i][j];\n if(v[j]&(1LL<<i)){\n pref[i][j+1]++;\n }\n }\n }\n ll tem=0;\n while(tail<n){\n while(head+1<n){\n ll nwtem=tem|v[head+1];\n if(nwtem>k)break;\n tem|=v[head+1];\n head++;\n }\n if(head>=tail)ans=min(ans,abs(tem-k));\n if(head+1<n){\n ll nwtem=tem|v[head+1];\n ans=min(ans,abs(nwtem-k));\n }\n // cout<<ans<<'\\n';\n if(head<tail){\n tem=0;\n head++;\n tail++;\n }else if(head==tail){\n tail++;\n tem=0;\n }\n else{\n tem=0;\n REP(i,0,34){\n tem|=((ll)((pref[i][head+1]- pref[i][tail+1])>=1)<<i);\n }\n tail++;\n }\n }\n return ans;\n }\n};", "memory": "306445" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nclass Solution {\npublic:\n int minimumDifference(vector<int>& v, int k) {\n ll n=v.size();\n ll head=-1,tail=0,ans=1e17;\n vvi pref(35,vi(n+1));\n REP(i,0,35-1){\n REP(j,0,n-1){\n pref[i][j+1]=pref[i][j];\n if(v[j]&(1LL<<i)){\n pref[i][j+1]++;\n }\n }\n }\n ll tem=0;\n while(tail<n){\n while(head+1<n){\n ll nwtem=tem|v[head+1];\n if(nwtem>k)break;\n tem|=v[head+1];\n head++;\n }\n if(head>=tail)ans=min(ans,abs(tem-k));\n if(head+1<n){\n ll nwtem=tem|v[head+1];\n ans=min(ans,abs(nwtem-k));\n }\n cout<<ans<<'\\n';\n if(head<tail){\n tem=0;\n head++;\n tail++;\n }else if(head==tail){\n tail++;\n tem=0;\n }\n else{\n tem=0;\n REP(i,0,34){\n tem|=((ll)((pref[i][head+1]- pref[i][tail+1])>=1)<<i);\n }\n tail++;\n }\n }\n return ans;\n }\n};", "memory": "310196" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\n\n \nprivate:\n \n vector<int> convToBinary(int num) {\n \n vector<int> binaryRep(32, 0);\n \n for (int i=0; i<32; i++) {\n binaryRep[i] = num % 2;\n num = num / 2;\n }\n \n return binaryRep;\n \n }\n \n int findMinAbsDiff(vector<int>& nums, int k) {\n \n int smallestPossible = INT_MAX;\n \n int i=0;\n int j=0;\n \n int currOr = 0;\n \n vector<int> cntBits(32, 0);\n \n int numElem = 0;\n \n while ((j < nums.size() && currOr <= k) || (i < nums.size() && currOr > k) ) {\n \n j = max(i, j);\n \n if (currOr <= k) {\n \n numElem++;\n \n currOr = currOr | nums[j];\n \n vector<int> binaryRep = convToBinary(nums[j]);\n \n for (int i=0; i<32; i++) {\n cntBits[i] = cntBits[i] + binaryRep[i];\n }\n \n j++;\n \n }\n else {\n \n if (numElem > 0)\n smallestPossible = min(smallestPossible, std::abs(k-currOr));\n \n vector<int> binaryRep = convToBinary(nums[i]);\n \n \n \n for (int i=0; i<32; i++) {\n cntBits[i] = cntBits[i] - binaryRep[i];\n if (cntBits[i] == 0 && binaryRep[i] == 1) {\n currOr = currOr - (1 << i);\n }\n }\n \n // std::cout<<i<<\" \"<<currOr<<\"\\n\";\n \n numElem--;\n \n i++;\n \n }\n\n // std::cout<<currOr<<\"\\n\";\n \n if (numElem > 0)\n smallestPossible = min(smallestPossible, std::abs(k-currOr));\n \n }\n \n return smallestPossible;\n \n }\n \npublic:\n \n int minimumDifference(vector<int>& nums, int k) {\n return findMinAbsDiff(nums, k);\n }\n};", "memory": "313948" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n set<int> lasts;\n int res = INT_MAX;\n for (const int n : nums) {\n set<int> nexts;\n nexts.insert(n);\n for (const int last : lasts) {\n nexts.insert(n | last);\n }\n lasts.swap(nexts);\n for (const int last : lasts) {\n res = min(res, abs(k - last));\n }\n }\n return res;\n }\n};", "memory": "317699" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "int v[35], d[35];\n\nclass Solution {\npublic:\n \n inline void cal(int a) {\n for(int i = 0; i < 31; i++) {\n if((1 << i) & a) {\n d[i] = 1;\n } else {\n d[i] = 0;\n }\n }\n }\n \n int minimumDifference(vector<int>& a, int k) {\n int n = a.size(), res = 1e9, c = 0;\n for(int i = 0; i < 32; i++) {\n v[i] = -1;\n }\n // map<int, int> m;\n for(int i = 0; i < n; i++) {\n cal(a[i]);\n c |= a[i];\n for(int j = 0; j < 32; j++) {\n if(d[j]) {\n v[j] = i;\n }\n }\n map<int, int> m;\n for(int j = 0; j < 32; j++) {\n if(v[j] >= 0) {\n m[v[j]] += (1 << j);\n }\n }\n int tc = c;\n for(auto &j : m) {\n res = min(res, abs(k - tc));\n tc -= j.second;\n }\n }\n return res;\n }\n};", "memory": "321450" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#define ll long long\nclass Solution {\npublic:\n int minimumDifference(vector<int>& arr, int k) {\n unordered_set<ll> curr, prev, res;\n prev.insert((1LL<<31)-1);\n for(int i = 0; i < arr.size(); i++){\n int x = arr[i];\n for(auto y : prev){\n curr.insert(x | y);\n }\n curr.insert(x);\n for(auto y : curr){\n res.insert(y);\n }\n prev = curr;\n curr.clear();\n }\n int ans = INT_MAX;\n for(auto i : res){\n ans = min(ans, abs(k-(int)i));\n }\n return ans;\n }\n};", "memory": "325201" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n set<int> dp[n];\n dp[0].insert(nums[0]);\n int res=abs(k-nums[0]);\n for(int i=1;i<n;i++){\n dp[i].insert(nums[i]);\n for(auto x:dp[i-1]){\n dp[i].insert(nums[i]|x);\n }\n for(auto x:dp[i]){\n res = min(res,abs(k-x));\n }\n }\n return res;\n }\n};", "memory": "328953" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n set<int> st[n + 1];\n for (int i = n - 1; i >= 0; i--) {\n st[i].insert(nums[i]);\n for (auto& sti : st[i + 1]) {\n st[i].insert(nums[i] | sti);\n }\n }\n int mn = INT_MAX;\n for (int i = 0; i < n; i++) {\n for (auto& sti : st[i]) {\n mn = min(mn, abs(k - sti));\n }\n }\n return mn;\n }\n};", "memory": "332704" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& arr, int target) {\n int n=arr.size();\n set<int> st[n];\n st[n-1].insert(arr[n-1]);\n for(int i=n-2;i>=0;i--)\n {\n st[i].insert(arr[i]);\n for(auto it:st[i+1])\n {\n int val=(arr[i]|it);\n st[i].insert(val);\n }\n }\n int mn=INT_MAX;\n for(int i=0;i<n;i++)\n {\n for(auto it:st[i])\n mn=min(mn,abs(it-target));\n }\n return mn;\n }\n};", "memory": "336455" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n\n void add(int val, vector<int> &count)\n {\n for(int i = 0; i<32; i++)\n {\n if(((val>>i)&1)!=0)//if ith bit is zero\n {\n count[i]++;\n }\n }\n }\n\n void remove(int val, vector<int> &count)\n {\n for(int i = 0; i<32; i++)\n {\n if(((val>>i)&1)!=0)\n {\n count[i]--;\n }\n }\n }\n\n ll get(vector<int> &count)\n { \n ll res = 0;\n\n for(int i = 0; i<32; i++)\n {\n if(count[i]>0)//so this bit is set(i.g. 1)\n {\n res += (1ll<<i);//ll required otherwise overflow\n }\n } \n\n return res;\n }\n\n int minimumDifference(vector<int>& nums, int k) {\n \n //Approch 1 TC O(n*log(max(nums[i]))), SC O(32)\n //Appply subarray(&, |) technique\n\n set<int> values;//at max only 32 posible values\n\n int n = nums.size();\n int ans = INT_MAX;\n\n for(int i = 0; i<n; i++)\n {\n set<int> newValues;\n\n for(auto it: values)\n newValues.insert(it|nums[i]);\n \n newValues.insert(nums[i]);\n\n for(auto it: newValues)\n {\n ans = min(ans, abs(k-it));\n\n }\n\n values = newValues;\n \n }\n\n return ans;\n\n //Approch 2: Sliding window TC O(32*n), SC O(32)\n/*\n int n = nums.size();\n vector<int> count(32, 0);\n\n ll cur = nums[0], ans = INT_MAX;\n\n int i = 0, j = 1;\n \n add(nums[0], count);\n ans = min(ans, abs(cur-k));\n\n while(j<n)\n { \n\n while(cur>k && i<j)//if cur is small make it large by decrese the window\n {\n remove(nums[i], count);//remove ele\n cur = get(count);//take AND of new subarr\n //ans = min(ans, abs(k-cur));\n i++;\n }\n \n \n while(cur<k && j<n)//if cur is big make it small by increse the window\n { \n add(nums[j], count);//add ele\n cur = get(count);//take and AND of subarray [i j]\n //ans = min(ans, abs(k-cur));//store min\n j++;\n }\n\n \n //ans = min(ans, abs(k-cur));\n\n \n\n ans = min(ans, abs(k-cur));\n\n if(cur==k) return 0;//optimal ans\n }\n\n return ans;\n \n\n //Approch 3: Seg tree + BS\n */\n }\n};", "memory": "340206" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& arr, int k) {\n int n=arr.size();\n set<int>st;\n int mini=INT_MAX;\n for (int i=0;i<arr.size();i++)\n {\n set<int>curr;\n for (auto it:st)\n {\n int x=it|arr[i];\n mini=min(mini,abs(x-k));\n curr.insert(x);\n }\n curr.insert(arr[i]);\n mini=min(mini,abs(arr[i]-k));\n st=curr;\n }\n \n return mini;\n }\n};", "memory": "343958" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n set<int> prev;\n int ans = INT_MAX;\n int n = nums.size();\n for(auto x:nums){\n set<int> next;\n for(auto y:prev){\n next.insert(x | y);\n }\n next.insert(x);\n for(auto y:next){\n ans = min(ans,abs(y-k));\n }\n prev = next;\n }\n return ans;\n }\n};", "memory": "343958" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n long long n=nums.size(),ans=1e9;\n map<int,int>mp;\n for(int i=0;i<n;i++){\n map<int,int>mp1;\n for(auto [val,cnt]:mp){\n mp1[val|nums[i]]+=cnt;\n } \n mp1[nums[i]]++;\n for(auto [val,cnt]:mp1){\n ans=min(ans,(long long)abs(k-val));\n }\n mp=mp1;\n }\n return ans;\n }\n};", "memory": "347709" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n map<int, int> prev;\n long long res = 0;\n int ans=0, diff=INT_MAX;\n\n for(int i = 0; i <n; i++) {\n map<int, int> curr;\n\n for(auto &it: prev) {\n int andSum = it.first;\n diff = min(diff, abs(k-it.first));\n curr[andSum | nums[i]] ++;\n }\n\n curr[nums[i]]++;\n prev = curr;\n \n }\n for(auto &it:prev)\n {\n diff = min(diff, abs(k-it.first));\n }\n\n\n\n return diff;\n }\n};", "memory": "347709" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // prev or cur store total unique value of and of subaaray possible starting from a index\n // particularly only log(nums[i]) values of and possible because value of and will change only when a bit turns into 0 from 1 in prev and\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n set<int> prev;\n int ans = INT_MAX;\n for(int i = n-1;i >= 0;i--)\n {\n set<int> cur;\n for(auto it:prev)\n {\n cur.insert(it | nums[i]);\n }\n cur.insert(nums[i]);\n\n for(auto it:cur)\n ans = min(ans,abs(k - it));\n\n prev = cur;\n }\n\n return ans;\n }\n};", "memory": "351460" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // for closest &\n // prev or cur store total unique value of and of subaaray possible starting from a index\n // particularly only log(nums[i]) values of and possible because value of and will change only when a bit turns into 0 from 1 in prev and\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n set<int> prev;\n int ans = INT_MAX;\n for(int i = n-1;i >= 0;i--)\n {\n set<int> cur;\n for(auto it:prev)\n {\n cur.insert(it | nums[i]);\n }\n cur.insert(nums[i]);\n\n for(auto it:cur)\n ans = min(ans,abs(k - it));\n\n prev = cur;\n }\n\n return ans;\n }\n};", "memory": "351460" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int ans=0,diff=1e9,n=nums.size();\n map<int,int>next_map;\n for(int i=n-1;i>=0;i--){\n int curEle=nums[i];\n map<int,int>cur_map;\n for(auto it : next_map){\n int val=(curEle|it.first);\n if(abs(val-k)<diff){\n diff=abs(val-k);\n }\n cur_map[val]++;\n }\n if(abs(curEle-k)<diff){\n diff=abs(curEle-k);\n }\n cur_map[curEle]++;\n next_map=cur_map;\n }\n return diff;\n }\n};", "memory": "355211" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<int>> psum;\n for (int i=0; i<32; i++) {\n vector<int> ps = {0};\n for (int x : nums) {\n ps.push_back(ps.back() + ((x>>i)&1));\n }\n psum.push_back(ps);\n }\n auto OR = [&](int l, int r) {\n int ret = 0;\n for (int i=0; i<32; i++) {\n if (psum[i][r+1]-psum[i][l] > 0) {\n ret |= (1<<i);\n }\n }\n return ret;\n };\n \n int ret = INT_MAX;\n int l = 0;\n for (int r=0; r<nums.size(); r++) {\n ret = min(ret, abs(OR(l, r)-k));\n while (l+1 <= r && OR(l, r) >= k) {\n l++;\n ret = min(ret, abs(OR(l, r)-k));\n }\n }\n return ret;\n }\n};", "memory": "355211" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<int>> psum;\n for (int i=0; i<32; i++) {\n vector<int> ps = {0};\n for (int x : nums) {\n ps.push_back(ps.back() + ((x>>i)&1));\n }\n psum.push_back(ps);\n }\n auto OR = [&](int l, int r) {\n int ret = 0;\n for (int i=0; i<32; i++) {\n if (psum[i][r+1]-psum[i][l] > 0) {\n ret |= (1<<i);\n }\n }\n return ret;\n };\n \n int ret = INT_MAX;\n int l = 0;\n for (int r=0; r<nums.size(); r++) {\n while (l <= r && OR(l, r) >= k) {\n ret = min(ret, abs(OR(l, r)-k));\n l++;\n }\n if (l <= r) ret = min(ret, abs(OR(l, r)-k));\n }\n return ret;\n }\n};", "memory": "358963" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define ll long long\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<ll>> dp(n, vector<ll>(32, 0));\n\n for (int i = 0; i < 32; i++) {\n if (nums[0] & (1 << i)) {\n dp[0][i]++;\n }\n }\n\n for (int j = 1; j < n; j++) {\n for (int i = 0; i < 32; i++) { // Updated loop to check all 32 bits\n dp[j][i] = dp[j - 1][i];\n if (nums[j] & (1 << i)) {\n dp[j][i]++;\n }\n }\n }\n\n int l = 0, r = 0;\n ll diff = abs(k - nums[0]);\n ll x = nums[r];\n ll ans = LLONG_MAX; // Use LLONG_MAX for long long values\n r++;\n\n while (l < n && r < n) {\n x |= nums[r]; \n ans = abs(k - x);\n\n while (l < r && x > k) {\n for (int i = 0; i < 32; i++) {\n if (dp[l][i] > 0 && dp[r][i] > 0) {\n int set=dp[r][i]-dp[l][i];\n if(set==0){ // Check if the bit was set by nums[l]\n if (x & (1 << i)) {\n x ^= (1 << i); // Unset the bit\n }}\n }\n }\n l++;\n ans = min(ans, abs(k - x));\n }\n\n diff = min(diff, ans);\n r++;\n }\n\n return diff;\n }\n};\n", "memory": "358963" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define ll long long\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<ll>> dp(n, vector<ll>(32, 0));\n\n for (int i = 0; i < 32; i++) {\n if (nums[0] & (1 << i)) {\n dp[0][i]++;\n }\n }\n\n for (int j = 1; j < n; j++) {\n for (int i = 0; i < 32; i++) { // Updated loop to check all 32 bits\n dp[j][i] = dp[j - 1][i];\n if (nums[j] & (1 << i)) {\n dp[j][i]++;\n }\n }\n }\n\n int l = 0, r = 0;\n ll diff = abs(k - nums[0]);\n ll x = nums[r];\n ll ans = LLONG_MAX; // Use LLONG_MAX for long long values\n r++;\n\n while (l < n && r < n) {\n x |= nums[r]; \n ans = abs(k - x);\n\n while (l < r && x > k) {\n for (int i = 0; i < 32; i++) {\n int set=dp[r][i]-dp[l][i];\n if (dp[l][i] > 0 && dp[r][i] > 0&&set==0) {\n \n if(set==0){ // Check if the bit was set by nums[l]\n if (x & (1 << i)) {\n x ^= (1 << i); // Unset the bit\n }}\n }\n }\n l++;\n ans = min(ans, abs(k - x));\n }\n\n diff = min(diff, ans);\n r++;\n }\n\n return diff;\n }\n};\n", "memory": "362714" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "// class Solution {\n// public:\n// int minimumDifference(vector<int>& nums, int k) {\n// int n = nums.size();\n// int min_diff = abs(k - nums[0]);\n// int or_value = 0;\n \n// for (int l = 0, r = 0; r < n; ++r) {\n// or_value |= nums[r];\n \n// // Calculate the current difference\n// min_diff = min(min_diff, abs(k - or_value));\n \n// // Check if the OR value matches k (can't get better than 0)\n// if (min_diff == 0) return 0;\n \n// // If OR value is already larger than k, no need to continue adding elements\n// // because OR operation is cumulative (it only grows or remains same)\n// while (l < r && or_value > k) {\n// // Move left pointer and recalculate OR value\n// or_value = 0;\n// for (int i = l + 1; i <= r; ++i) {\n// or_value |= nums[i];\n// }\n// ++l;\n// min_diff = min(min_diff, abs(k - or_value));\n// }\n// }\n \n// return min_diff;\n// }\n// };\n\nclass Solution {\npublic:\n typedef long long ll;\n\n int minimumDifference(vector<int>& nums, int k) {\n int n = size(nums);\n int l=0, r=0, op=0, num=0;\n int ans = 1e9;\n vector<vector<ll>> dp(n+1, vector<ll> (32,0));\n for(int i=0; i<n; i++){\n for(int j=0;j<32;j++){\n dp[i+1][j] = dp[i][j] + (nums[i] & (1<<j));\n }\n }\n for(int l=0 , r=0; r<n; r++){\n op = op | nums[r];\n ans = min(ans, abs(k - op));\n if (ans == 0) return 0;\n while(l<r && op>k){\n op = 0;\n l++;\n for(int j=0;j<32;j++){\n int x = dp[r+1][j] - dp[l][j];\n if(x) x = 1;\n op += (x<<j);\n }\n ans = min(ans, abs(k - op));\n }\n }\n return ans;\n }\n};", "memory": "362714" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int ans = INT_MAX;\n unordered_set<int> curr_or_set;\n for (int num : nums) {\n unordered_set<int> next_or_set;\n next_or_set.insert(num);\n ans = min(ans, abs(k - num));\n for (int prev_or : curr_or_set) {\n int new_or = prev_or | num; // 비트 OR 연산\n if (next_or_set.find(new_or) == next_or_set.end()) {\n next_or_set.insert(new_or);\n ans = min(ans, abs(k - new_or));\n }\n }\n curr_or_set = move(next_or_set);\n }\n return ans;\n }\n};\n", "memory": "366465" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution \n{\npublic:\n int minimumDifference(vector<int>& nums, int k) \n {\n std::unordered_set<int> new_set;\n int result = INT_MAX;\n\n for (auto x : nums)\n {\n std::unordered_set<int> old_set = std::move(new_set);\n new_set.clear();\n\n for (auto&& val : old_set)\n new_set.insert(val | x);\n new_set.insert(x);\n\n for (auto&& val : new_set)\n result = std::min(result, std::abs(val - k));\n }\n\n return result;\n }\n};", "memory": "366465" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // O(30*N) - Time complexity\n int minimumDifference(vector<int>&nums, int k){\n int n=nums.size();\n unordered_set<int>prev;\n int ans=INT_MAX;\n\n for(int i=0; i<n; i++){\n unordered_set<int>curr;\n for(auto it: prev){\n curr.insert(it|nums[i]);\n }\n curr.insert(nums[i]);\n for(auto it: curr){\n ans = min(ans, abs(it-k));\n }\n prev=curr;\n }\n return ans;\n }\n};", "memory": "370216" }
3,436
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p> <p>Return the <strong>minimum</strong> possible value of the absolute difference.</p> <p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n unordered_set<int> ors;\n int best = INT_MAX;\n\n for(int i = 0; i<nums.size(); i++)\n {\n unordered_set<int> ors_ending_here;\n ors_ending_here.insert(nums[i]);\n\n for(auto& ele : ors)\n {\n ors_ending_here.insert(nums[i] | ele);\n }\n\n for(auto& ele : ors_ending_here)\n {\n best = min(best, abs(k - ele));\n }\n\n ors = ors_ending_here;\n }\n\n return best;\n }\n};", "memory": "373968" }