id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n return {};\n }\n};\nbool init = ([]() -> char {\n ios::sync_with_stdio(false);\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n int ARR[30001]; ARR[0] = 0;\n ofstream out(\"user.out\");\n string s;\n while(getline(cin, s)) {\n for(int i = 1, l = s.length(), c = 1, x = 0; i < l; i++) {\n if(s[i] == ',') {\n ARR[c++] = x ^ ARR[c-1];\n x = 0;\n }else if(s[i] == ']') {\n ARR[c] = x ^ ARR[c-1];\n break;\n }else {\n x = 10*x + (s[i] - '0');\n }\n }\n out << '['; getline(cin, s);\n bool first = true;\n int i = 2, l = s.length();\n while(i < l) {\n int x = 0, y = 0;\n while(s[i] != ',') {\n x = 10*x + (s[i++] - '0');\n }\n i++;\n while(s[i] != ']') {\n y = 10*y + (s[i++] - '0');\n }\n if(first) {\n first = false;\n }else out << ',';\n out << (ARR[y+1] ^ ARR[x]);\n\n i += 3;\n }\n out << \"]\\n\";\n }\n out.flush();\n exit(0);\n return 1;\n})();",
"memory": "8500"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n return {};\n }\n};\nbool init = ([]() -> char {\n ios::sync_with_stdio(false);\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n int ARR[30001]; ARR[0] = 0;\n ofstream out(\"user.out\");\n string s;\n while(getline(cin, s)) {\n for(int i = 1, l = s.length(), c = 1, x = 0; i < l; i++) {\n if(s[i] == ',') {\n ARR[c++] = x ^ ARR[c-1];\n x = 0;\n }else if(s[i] == ']') {\n ARR[c] = x ^ ARR[c-1];\n break;\n }else {\n x = 10*x + (s[i] - '0');\n }\n }\n out << '['; getline(cin, s);\n bool first = true;\n int i = 2, l = s.length();\n while(i < l) {\n int x = 0, y = 0;\n while(s[i] != ',') {\n x = 10*x + (s[i++] - '0');\n }\n i++;\n while(s[i] != ']') {\n y = 10*y + (s[i++] - '0');\n }\n if(first) {\n first = false;\n }else out << ',';\n out << (ARR[y+1] ^ ARR[x]);\n\n i += 3;\n }\n out << \"]\\n\";\n }\n out.flush();\n exit(0);\n return 1;\n})();\n\n ",
"memory": "8600"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n \n std::vector<int> prefixXOR(arr.size() + 1, 0);\n for (int i = 0; i < arr.size(); ++i) {\n prefixXOR[i + 1] = prefixXOR[i] ^ arr[i];\n }\n\n std::vector<int> res;\n for (const auto& quer : queries) {\n int left = quer[0];\n int right = quer[1];\n\n res.push_back(prefixXOR[right + 1] ^ prefixXOR[left]);\n }\n return res;\n }\n};\n\nbool init = ([]() -> char {\n ios::sync_with_stdio(false);\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n int ARR[30001]; ARR[0] = 0;\n ofstream out(\"user.out\");\n string s;\n while(getline(cin, s)) {\n for(int i = 1, l = s.length(), c = 1, x = 0; i < l; i++) {\n if(s[i] == ',') {\n ARR[c++] = x ^ ARR[c-1];\n x = 0;\n }else if(s[i] == ']') {\n ARR[c] = x ^ ARR[c-1];\n break;\n }else {\n x = 10*x + (s[i] - '0');\n }\n }\n out << '['; getline(cin, s);\n bool first = true;\n int i = 2, l = s.length();\n while(i < l) {\n int x = 0, y = 0;\n while(s[i] != ',') {\n x = 10*x + (s[i++] - '0');\n }\n i++;\n while(s[i] != ']') {\n y = 10*y + (s[i++] - '0');\n }\n if(first) {\n first = false;\n }else out << ',';\n out << (ARR[y+1] ^ ARR[x]);\n\n i += 3;\n }\n out << \"]\\n\";\n }\n out.flush();\n exit(0);\n return 1;\n})();\n",
"memory": "8600"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n return {};\n }\n};\nbool init = ([]() -> char {\n ios::sync_with_stdio(false);\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n int ARR[30001]; ARR[0] = 0;\n ofstream out(\"user.out\");\n string s;\n while(getline(cin, s)) {\n for(int i = 1, l = s.length(), c = 1, x = 0; i < l; i++) {\n if(s[i] == ',') {\n ARR[c++] = x ^ ARR[c-1];\n x = 0;\n }else if(s[i] == ']') {\n ARR[c] = x ^ ARR[c-1];\n break;\n }else {\n x = 10*x + (s[i] - '0');\n }\n }\n out << '['; getline(cin, s);\n bool first = true;\n int i = 2, l = s.length();\n while(i < l) {\n int x = 0, y = 0;\n while(s[i] != ',') {\n x = 10*x + (s[i++] - '0');\n }\n i++;\n while(s[i] != ']') {\n y = 10*y + (s[i++] - '0');\n }\n if(first) {\n first = false;\n }else out << ',';\n out << (ARR[y+1] ^ ARR[x]);\n\n i += 3;\n }\n out << \"]\\n\";\n }\n out.flush();\n exit(0);\n return 1;\n})();",
"memory": "8700"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n return {};\n }\n};\nbool init = ([]() -> char {\n ios::sync_with_stdio(false);\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n int ARR[30001]; ARR[0] = 0;\n ofstream out(\"user.out\");\n string s;\n while(getline(cin, s)) {\n for(int i = 1, l = s.length(), c = 1, x = 0; i < l; i++) {\n if(s[i] == ',') {\n ARR[c++] = x ^ ARR[c-1];\n x = 0;\n }else if(s[i] == ']') {\n ARR[c] = x ^ ARR[c-1];\n break;\n }else {\n x = 10*x + (s[i] - '0');\n }\n }\n out << '['; getline(cin, s);\n bool first = true;\n int i = 2, l = s.length();\n while(i < l) {\n int x = 0, y = 0;\n while(s[i] != ',') {\n x = 10*x + (s[i++] - '0');\n }\n i++;\n while(s[i] != ']') {\n y = 10*y + (s[i++] - '0');\n }\n if(first) {\n first = false;\n }else out << ',';\n out << (ARR[y+1] ^ ARR[x]);\n\n i += 3;\n }\n out << \"]\\n\";\n }\n out.flush();\n exit(0);\n return 1;\n})();",
"memory": "8800"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n return {};\n }\n};\nbool init = ([]() -> char {\n ios::sync_with_stdio(false);\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n int ARR[30001]; ARR[0] = 0;\n ofstream out(\"user.out\");\n string s;\n while(getline(cin, s)) {\n for(int i = 1, l = s.length(), c = 1, x = 0; i < l; i++) {\n if(s[i] == ',') {\n ARR[c++] = x ^ ARR[c-1];\n x = 0;\n }else if(s[i] == ']') {\n ARR[c] = x ^ ARR[c-1];\n break;\n }else {\n x = 10*x + (s[i] - '0');\n }\n }\n out << '['; getline(cin, s);\n bool first = true;\n int i = 2, l = s.length();\n while(i < l) {\n int x = 0, y = 0;\n while(s[i] != ',') {\n x = 10*x + (s[i++] - '0');\n }\n i++;\n while(s[i] != ']') {\n y = 10*y + (s[i++] - '0');\n }\n if(first) {\n first = false;\n }else out << ',';\n out << (ARR[y+1] ^ ARR[x]);\n\n i += 3;\n }\n out << \"]\\n\";\n }\n out.flush();\n exit(0);\n return 1;\n})();",
"memory": "8800"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n for (int i = 1; i < n; i++) {\n arr[i] ^= arr[i-1];\n }\n int q = queries.size();\n vector<int> ans(q);\n for (int i = 0; i < q; i++) {\n int l = queries[i][0], r = queries[i][1];\n ans[i] = arr[r]^(l > 0 ? arr[l-1] : 0);\n }\n return ans;\n }\n};\n",
"memory": "40900"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = queries.size();\n vector<int> answer(n, 0);\n int i = 0, m = 0, b = 0;\n for(vector<int>& a: queries){\n m = a[1];\n b = arr[m];\n for(int j=a[0]; j<m; j++){\n b = b^arr[j];\n }\n answer[i] = b;\n i++;\n }\n return answer;\n }\n};",
"memory": "41200"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n for (int i = 1; i < arr.size(); i++) arr[i] ^= arr[i-1];\n vector<int> ans(queries.size());\n for (int i = 0; i < queries.size(); i++) {\n ans[i] = arr[queries[i][1]];\n if (queries[i][0]) ans[i] ^= arr[queries[i][0]-1];\n }\n return ans;\n }\n};",
"memory": "41200"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = queries.size();\n vector<int> answer(n, 0);\n int i = 0;\n for(vector<int>& a: queries){\n for(int j=a[0]; j<=a[1]; j++){\n answer[i] = (answer[i]^arr[j]);\n }\n i++;\n }\n return answer;\n }\n};",
"memory": "41300"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n std::vector<int> ans;\n partial_sum(arr.begin(), arr.end(), arr.begin(), bit_xor<int>());\n\n ans.reserve(queries.size());\n\n //populate xor dict\n for(auto& query : queries)\n {\n int query_ans = arr[query.back()];\n if(query.front() > 0)\n {\n query_ans ^= arr[query.front()-1];\n }\n ans.emplace_back(std::move(query_ans));\n }\n\n return ans;\n }\n\n};",
"memory": "41300"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n partial_sum(arr.begin(), arr.end(), arr.begin(), bit_xor<int>());\n int n=queries.size();\n vector<int> ans(n);\n int i=0;\n for(auto& q: queries){\n int q0=q[0], q1=q[1];\n ans[i++]=(q0==0)?arr[q1]:(arr[q1]^arr[q0-1]);\n }\n return ans;\n }\n};\n\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "41400"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int>values;\n int val = arr[0];\n for(int i=1;i<arr.size();i++)\n {\n if(i%100==0)\n {\n values.push_back(val);\n val = 0;\n }\n val ^= arr[i];\n }\n vector<int>ans(queries.size(), 0);\n for(int i=0;i<queries.size();i++)\n {\n int j = queries[i][0];\n for( ; j%100!=0 && j<=queries[i][1]; j++)\n {\n ans[i]^=arr[j];\n }\n for( ; j+99<=queries[i][1]; j+=100)\n {\n ans[i]^=values[j/100];\n }\n for( ; j<=queries[i][1]; j++)\n {\n ans[i]^=arr[j];\n }\n }\n return ans;\n }\n};",
"memory": "41400"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n vector<int> pre(n);\n pre[0] = arr[0];\n \n // Compute prefix XOR array\n for (int i = 1; i < n; ++i) {\n pre[i] = pre[i - 1] ^ arr[i];\n }\n \n std::vector<int> res(queries.size());\n \n // Answer each query\n for (int k = 0; k < queries.size(); ++k) {\n int i = queries[k][0];\n int j = queries[k][1];\n if (i == 0) {\n res[k] = pre[j];\n } else {\n res[k] = pre[j] ^ pre[i - 1];\n }\n }\n \n return res;\n }\n};",
"memory": "41500"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n=arr.size();\n vector<int> prefix(n,0);\n prefix[0]=arr[0];\n for(int i=1;i<n;i++){\n prefix[i]=arr[i]^prefix[i-1];\n }\n int m=queries.size();\n vector<int> vec(m,0);\n for(int i=0;i<m;i++){\n int f=queries[i][0];\n int l=queries[i][1];\n if(f==0){\n vec[i]=prefix[l];\n }\n else{\n vec[i]=arr[f]^prefix[f]^prefix[l];\n }\n }\n return vec;\n }\n};",
"memory": "41600"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = queries.size();\n vector<int> ans(n);\n vector<int> pre(arr.size());\n pre[0] = arr[0];\n for(int i=1; i<arr.size(); i++){\n pre[i] = pre[i-1] ^ arr[i];\n }\n for(int i=0; i<n; i++){\n int startIdx = queries[i][0];\n int endIdx = queries[i][1];\n ans[i] = startIdx == 0 ? pre[endIdx] : pre[endIdx] ^ pre[startIdx-1];\n // int res = arr[startIdx];\n // for(int j=startIdx+1; j<=endIdx; j++){\n // res = res ^ arr[j];\n // }\n // ans[i] = res; \n }\n return ans;\n }\n};",
"memory": "41600"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n vector<int> pre(n);\n pre[0] = arr[0];\n \n // Compute prefix XOR array\n for (int i = 1; i < n; ++i) {\n pre[i] = pre[i - 1] ^ arr[i];\n }\n \n std::vector<int> res(queries.size());\n \n // Answer each query\n for (int k = 0; k < queries.size(); ++k) {\n int i = queries[k][0];\n int j = queries[k][1];\n if (i == 0) {\n res[k] = pre[j];\n } else {\n res[k] = pre[j] ^ pre[i - 1];\n }\n }\n \n return res;\n }\n};",
"memory": "41700"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n vector<int> pre(n);\n pre[0] = arr[0];\n \n // Compute prefix XOR array\n for (int i = 1; i < n; ++i) {\n pre[i] = pre[i - 1] ^ arr[i];\n }\n \n std::vector<int> res(queries.size());\n \n // Answer each query\n for (int k = 0; k < queries.size(); ++k) {\n int i = queries[k][0];\n int j = queries[k][1];\n if (i == 0) {\n res[k] = pre[j];\n } else {\n res[k] = pre[j] ^ pre[i - 1];\n }\n }\n \n return res;\n }\n};",
"memory": "41700"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int>ans;\n for(auto &it : queries){\n int start = it[0];\n int end = it[1];\n int xors =0;\n for(int i=start ;i<=end;i++){\n xors = xors^arr[i];\n }\n ans.push_back(xors);\n\n }\n return ans;\n\n \n }\n};",
"memory": "41800"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int>ans;\n for(int i=1;i<arr.size();i++){\n arr[i] ^= arr[i-1];\n }\n for(int i=0;i<queries.size();i++){\n int s = queries[i][0];\n int e = queries[i][1];\n if(s > 0){\n ans.push_back(arr[s-1]^arr[e]);\n }\n else{\n ans.push_back(arr[e]);\n }\n }\n return ans;\n }\n};",
"memory": "41800"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n vector<int> xorprefix(n);\n xorprefix[0] = arr[0];\n for(int i = 1 ; i < n ;i++)\n {\n xorprefix[i] = xorprefix[i-1] ^ arr[i];\n }\n vector<int> r;\n for(int i = 0 ; i < queries.size() ; i++)\n {\n int start = queries[i][0];\n int end = queries[i][1];\n if(start == 0)\n {\n r.push_back(xorprefix[end]);\n }\n else\n {\n r.push_back(xorprefix[end]^xorprefix[start-1]);\n }\n }\n return r;\n }\n};",
"memory": "41900"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n vector<int>commulative_array(n , 0);\n commulative_array[0] = arr[0];\n for(int i=1;i<n;i++){\n commulative_array[i] = commulative_array[i-1] ^ arr[i];\n \n }\n vector<int>result;\n \n for(auto &it : queries){\n int l =it[0];\n int r = it[1];\n int xor_val = commulative_array[r] ^ (l==0 ? 0 : commulative_array[l-1]);\n result.push_back(xor_val);\n }\n return result;\n \n }\n};",
"memory": "41900"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> prefix_xor(arr.size()+1,0);\n for(int i=0;i<arr.size();i++){\n prefix_xor[i+1]=prefix_xor[i]^arr[i];\n // cout<<prefix_xor[i+1]<<endl;\n }\n vector<int> ans;\n for(int i=0;i<queries.size();i++){\n int l = queries[i][0];\n int r = queries[i][1];\n int tmp = prefix_xor[l]^prefix_xor[r+1];\n ans.push_back(tmp);\n }\n return ans;\n }\n};",
"memory": "42000"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = queries.size(), m = arr.size(), end = 0;\n vector<int> answer;\n vector<int> xors(m+1, 0);\n for(int i=1; i<=m; i++){\n xors[i] = arr[i-1]^xors[i-1];\n }\n for(vector<int>& a: queries){\n end = a[1];\n answer.push_back(xors[end+1]^xors[a[0]]);\n }\n return answer;\n }\n};",
"memory": "42000"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& q) {\n int n = arr.size();\n vector<int>prefix(n);\n\n prefix[0] = arr[0];\n for(int i=1; i<n; i++){\n prefix[i] = prefix[i-1] ^ arr[i];\n }\n\n vector<int>ans;\n\n for(int i=0; i<q.size(); i++){\n int l = q[i][0];\n int r = q[i][1];\n\n if(l == 0) ans.push_back(prefix[r]);\n else if(l == r && r!=0) ans.push_back(arr[l]);\n else{ \n ans.push_back(prefix[l-1] ^ prefix[r]);\n }\n }\n\n return ans;\n }\n};",
"memory": "42100"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int>prefix(arr.size());\n prefix[0]=arr[0];\n for(int i=1;i<arr.size();i++)\n {\n prefix[i]=prefix[i-1]^arr[i];\n }\n vector<int>res;\n for(int i=0;i<queries.size();i++){\n int start=queries[i][0];\n int end=queries[i][1];\n if(start==0){\n res.push_back(prefix[end]);\n }\n else{\n res.push_back(prefix[end]^prefix[start-1]);\n }\n }\n return res;\n }\n};",
"memory": "42100"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n vector<int> pref(n);\n pref[0] = arr[0];\n for (int i = 1; i < n; i++) {\n pref[i] = pref[i - 1] ^ arr[i];\n }\n int q = queries.size();\n vector<int> ans;\n while (q--) {\n int left = queries[q][0];\n int right = queries[q][1];\n if (left == 0) {\n ans.push_back(pref[right]);\n } else {\n ans.push_back(pref[right] ^ pref[left - 1]);\n }\n }\n reverse(ans.begin(), ans.end());\n return ans;\n }\n};\n",
"memory": "42200"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> res,pre(arr.size(),0);\n pre[0]=arr[0];\n for (int i=1;i<arr.size();i++)\n pre[i]=pre[i-1]^arr[i];\n for (int i=0;i<queries.size();i++){\n int l=queries[i][0],r=queries[i][1];\n if( l==0)\n res.push_back(pre[r]);\n else\n res.push_back(pre[r]^pre[l-1]);\n }\n return res;\n }\n};",
"memory": "42200"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "\nclass SegmentTreeV1 {\n public:\n size_t depth; // depth >= 1\n vector<int> buffer; // 1-based buffer\n\n SegmentTreeV1() {}\n void build(vector<int>& arr) {\n depth = ceil(log2(arr.size())) + 1;\n buffer = vector<int>(1 << depth, 0);\n // Fill leaf\n int offset = 1 << (depth - 1);\n for (int i = 0; i < (int)arr.size(); i++) {\n buffer[offset + i] = arr[i];\n }\n for (int i = (int)arr.size(); i < (1 << (depth - 1)); i++) {\n buffer[offset + i] = 0;\n }\n // Bottom up building\n for (int i = offset - 1; i >= 1; i--) {\n buffer[i] = buffer[i * 2] ^ buffer[i * 2 + 1];\n }\n }\n\n int query(int l, int r) {\n return recursive_query(1, 1, l, r);\n }\n\n int recursive_query(int i, int d, int l, int r) {\n int offset = 1 << (d - 1);\n int range = 1 << (depth - d);\n int il = (i - offset) * range;\n int ir = il + range - 1;\n if (r < il || l > ir)\n return 0;\n if (max(l, il) == il && min(r, ir) == ir) {\n // Matched\n return buffer[i];\n }\n return recursive_query(i * 2, d + 1, l, r) ^ recursive_query(i * 2 + 1, d + 1, l, r);\n }\n};\n\n/*\n Builds the segment tree in a mathematic method\n */\nclass Solution {\n public:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n SegmentTreeV1 segment_tree;\n segment_tree.build(arr);\n vector<int> result(queries.size());\n for (int i = 0; i < (int)queries.size(); i++) {\n result[i] = segment_tree.query(queries[i][0], queries[i][1]);\n }\n return result;\n }\n};\n\nauto _ = []() noexcept {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0; }();\n",
"memory": "42300"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> ans;\n vector<int> xorSum;\n int n = queries.size();\n\n int initXor=0;\n for(int i=0; i<arr.size(); i++){\n initXor=initXor^arr[i];\n cout<<initXor<<endl;\n xorSum.push_back(initXor);\n }\n\n\n for(auto& query:queries){\n if(query[0]==query[1]){\n ans.push_back(arr[query[0]]);\n }else{\n if(query[0]-1<0){\n ans.push_back(xorSum[query[1]]);\n }else{\n ans.push_back(xorSum[query[0]-1]^xorSum[query[1]]);\n }\n }\n }\n return ans;\n }\n};",
"memory": "42300"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n std::vector<int> ans;\n std::vector<int> xor_dict;\n\n\n //populate xor dict\n xor_dict.emplace_back(arr.front());\n for(int i = 1; i < arr.size(); i++)\n {\n xor_dict.push_back(xor_dict.back() ^ arr[i]);\n }\n\n for(auto& query : queries)\n {\n int query_ans = xor_dict[query.back()];\n if(query.front() > 0)\n {\n query_ans ^= xor_dict[query.front()-1];\n }\n ans.emplace_back(query_ans);\n }\n\n return ans;\n }\n\n};",
"memory": "42400"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> ans;\n vector<int> XOR;\n int val = 0;\n for(int num : arr){\n val ^= num;\n XOR.push_back(val);\n }\n\n for(const auto& val : queries){\n if(val[0] == val[1]){\n ans.push_back(arr[val[0]]);\n }\n else{\n int start = val[0] - 1;\n if(start >= 0){\n ans.push_back(XOR[val[1]] ^ XOR[start]);\n }\n else{\n ans.push_back(XOR[val[1]]);\n }\n }\n }\n return ans;\n }\n};",
"memory": "42500"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int x = 0;\n int n = arr.size();\n vector<int> XOR(1, 0);\n\n for(int i = 0; i < n; i++){\n x ^= arr[i];\n XOR.push_back(x);\n }\n\n int m = queries.size();\n vector<int> out;\n for(int i = 0; i < m; i++){\n out.push_back(XOR[queries[i][1]+1]^XOR[queries[i][0]]);\n }\n return out;\n }\n};",
"memory": "42500"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> ans;\n vector<int> XOR;\n int val = 0;\n for(int num : arr){\n val ^= num;\n XOR.push_back(val);\n }\n\n for(const auto& val : queries){\n if(val[0] == val[1]){\n ans.push_back(arr[val[0]]);\n }\n else{\n int start = val[0] - 1;\n if(start >= 0){\n ans.push_back(XOR[val[1]] ^ XOR[start]);\n }\n else{\n ans.push_back(XOR[val[1]]);\n }\n }\n }\n return ans;\n }\n};",
"memory": "42600"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = queries.size();\n vector<int> ans;\n vector<int> pre;\n int x = 0;\n for(int i = 0;i<arr.size();i++){\n x^=arr[i];\n pre.push_back(x);\n }\n\n\n for(int i = 0;i<n;i++){\n int l = queries[i][0],r = queries[i][1];\n int curr_ans = pre[r];\n if(l==0)ans.push_back(curr_ans);\n else{\n curr_ans^=pre[l-1];\n ans.push_back(curr_ans);\n }\n \n } \n\n return ans;\n }\n};",
"memory": "42600"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "\nclass SegmentTree {\n public:\n size_t depth; // depth >= 1\n vector<int> buffer; // 1-based buffer\n\n SegmentTree() {}\n void build(vector<int>& arr) {\n depth = ceil(log2(arr.size())) + 1;\n buffer = vector<int>(1 << depth, 0);\n\n // Fill leaf\n int offset = 1 << (depth - 1);\n for (int i = 0; i < (int)arr.size(); i++) {\n buffer[offset + i] = arr[i];\n }\n for (int i = (int)arr.size(); i < (1 << (depth - 1)); i++) {\n buffer[offset + i] = 0;\n }\n // Bottom up building\n for (int i = offset - 1; i >= 1; i--) {\n buffer[i] = buffer[i * 2] ^ buffer[i * 2 + 1];\n }\n }\n int query(int l, int r) {\n return recursive_query(1, 1, l, r);\n }\n int recursive_query(int i, int d, int l, int r) {\n int offset = 1 << (d - 1);\n int range = 1 << (depth - d);\n int bl = (i - offset) * range;\n int br = (i - offset + 1) * range - 1;\n if (r < bl || l > br)\n return 0;\n l = max(l, bl);\n r = min(r, br);\n if (l == bl && r == br) {\n // Matched\n return buffer[i];\n }\n int lval = recursive_query(i * 2, d + 1, l, r);\n int rval = recursive_query(i * 2 + 1, d + 1, l, r);\n return lval ^ rval;\n }\n};\n\nclass Solution {\n public:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n SegmentTree segment_tree;\n segment_tree.build(arr);\n vector<int> result;\n for (auto& q : queries) {\n result.push_back(segment_tree.query(q[0], q[1]));\n }\n return result;\n }\n};",
"memory": "42700"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "\nclass SegmentTree {\n public:\n size_t depth; // depth >= 1\n vector<int> buffer; // 1-based buffer\n\n SegmentTree() {}\n void build(vector<int>& arr) {\n depth = ceil(log2(arr.size())) + 1;\n buffer = vector<int>(1 << depth, 0);\n\n // Fill leaf\n int offset = 1 << (depth - 1);\n for (int i = 0; i < (int)arr.size(); i++) {\n buffer[offset + i] = arr[i];\n }\n for (int i = (int)arr.size(); i < (1 << (depth - 1)); i++) {\n buffer[offset + i] = 0;\n }\n // Bottom up building\n for (int i = offset - 1; i >= 1; i--) {\n buffer[i] = buffer[i * 2] ^ buffer[i * 2 + 1];\n }\n }\n int query(int l, int r) {\n return recursive_query(1, 1, l, r);\n }\n int recursive_query(int i, int d, int l, int r) {\n int offset = 1 << (d - 1);\n int range = 1 << (depth - d);\n int il = (i - offset) * range;\n int ir = il + range - 1;\n if (r < il || l > ir)\n return 0;\n if (max(l, il) == il && min(r, ir) == ir) {\n // Matched\n return buffer[i];\n }\n return recursive_query(i * 2, d + 1, l, r) ^ recursive_query(i * 2 + 1, d + 1, l, r);\n }\n};\n\nclass Solution {\n public:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n SegmentTree segment_tree;\n segment_tree.build(arr);\n vector<int> result;\n for (auto& q : queries) {\n result.push_back(segment_tree.query(q[0], q[1]));\n }\n return result;\n }\n};\n\nauto _ = []() noexcept {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0; }();\n",
"memory": "42700"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void buildTree(vector<int> & arr, vector<int> &segmentTree, int idx , int l , int r){\n if(l == r) {\n segmentTree[idx] = arr[l];\n return;\n }\n int mid = l + (r - l) / 2;\n\n buildTree(arr, segmentTree, idx * 2 + 1, l, mid);\n buildTree(arr, segmentTree, idx * 2 + 2, mid + 1, r);\n \n segmentTree[idx] = segmentTree[idx * 2 + 1] ^ segmentTree[idx * 2 + 2];\n }\n\n int rangeQ(vector<int>& arr, vector<int>& segmentTree, int idx, int l, int r, int left, int right) {\n \n if(right < l || left > r) return 0;\n\n if(left <= l && r <= right) return segmentTree[idx];\n\n int mid = l + (r - l) / 2;\n int lans = rangeQ(arr, segmentTree, idx * 2 + 1, l, mid, left, right);\n int rans = rangeQ(arr, segmentTree, idx * 2 + 2, mid + 1, r, left, right);\n\n return lans ^ rans;\n }\n\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n vector<int> segmentTree(4 * n, 0);\n \n buildTree(arr, segmentTree, 0, 0, n - 1);\n\n vector<int> res;\n for(auto &q : queries) {\n int left = q[0];\n int right = q[1];\n\n int temp = rangeQ(arr, segmentTree, 0, 0, n - 1, left, right);\n res.push_back(temp);\n }\n \n return res;\n }\n};\n",
"memory": "42800"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "\nstruct SegementTree {\n int n;\n vector<int> tree;\n\n SegementTree(int n) {\n this->n = n;\n tree.resize(4 * n);\n }\n\n SegementTree(vector<int>& arr) : SegementTree(arr.size()) {\n build(arr, 0, 0, n - 1);\n }\n\n void build(vector<int>& arr, int x, int l, int r) {\n if (l == r) {\n tree[x] = arr[l];\n } else {\n int m = l + ((r - l) >> 1);\n build(arr, 2 * x + 1, l, m);\n build(arr, 2 * x + 2, m + 1, r);\n tree[x] = tree[2 * x + 1] ^ tree[2 * x + 2];\n }\n }\n\n int query(int x, int l, int r, int ql, int qr) {\n if (l > qr || r < ql) {\n return 0;\n }\n if (l >= ql && r <= qr) {\n return tree[x];\n }\n int m = l + ((r - l) >> 1);\n return query(2 * x + 1, l, m, ql, qr) ^ query(2 * x + 2, m + 1, r, ql, qr);\n }\n};\n\nclass Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n SegementTree st(arr);\n vector<int> ans;\n for (auto& q : queries) {\n ans.push_back(st.query(0, 0, st.n - 1, q[0], q[1]));\n }\n return ans;\n }\n};",
"memory": "43100"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class SegmentTree {\npublic:\n vector<int> seg;\n SegmentTree(int n) {\n seg.resize(4*n);\n }\n\n int query(int segInd, int l, int r, int queryL, int queryR) {\n if(l>=queryL && r<=queryR) {\n return seg[segInd];\n }\n if(r<queryL || l>queryR) {\n return 0;\n }\n int mid = (l+r)/2;\n int leftXor = query(2*segInd+1, l, mid, queryL, queryR);\n int rightXor = query(2*segInd+2, mid+1, r, queryL, queryR);\n return leftXor ^ rightXor;\n }\n\n void buildUtil(int l, int r, int segInd, vector<int> &arr) {\n if(l==r) {\n seg[segInd] = arr[l];\n return;\n }\n int mid = (l+r)/2;\n buildUtil(l, mid, 2*segInd+1, arr);\n buildUtil(mid+1, r, 2*segInd+2, arr);\n seg[segInd] = seg[2*segInd+1] ^ seg[2*segInd+2];\n }\n\n void build(vector<int> &arr) {\n int n = arr.size();\n buildUtil(0, n-1, 0, arr);\n }\n\n};\nclass Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n SegmentTree segmentTree = SegmentTree(n);\n segmentTree.build(arr);\n vector<int> ans;\n for(int i=0;i<queries.size();i++) {\n ans.push_back(segmentTree.query(0, 0, n-1, queries[i][0], queries[i][1]));\n }\n\n return ans;\n }\n};",
"memory": "43100"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> segTree;\n\nvoid Build(int ind, int s, int e, vector<int>& arr) {\n if (s == e) {\n segTree[ind] = arr[s];\n return;\n }\n\n int mid = (s + e) >> 1;\n Build(2 * ind + 1, s, mid, arr);\n Build(2 * ind + 2, mid + 1, e, arr);\n\n segTree[ind] = segTree[2 * ind + 1] ^ segTree[2 * ind + 2];\n}\n\nint query(int s, int e, int l, int r, int ind) {\n if (l > e || r < s) {\n return 0;\n }\n\n if (s >= l && e <= r) {\n return segTree[ind];\n }\n\n int mid = (s + e) >> 1;\n int left = query(s, mid, l, r, 2 * ind + 1);\n int right = query(mid + 1, e, l, r, 2 * ind + 2);\n\n return left ^ right;\n}\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n segTree.resize(n*4);\n\n Build(0 ,0 ,n-1 , arr);\n vector<int> ans;\n\n for(int i =0 ; i < queries.size(); i++){\n int val = query(0 , n-1 ,queries[i][0], queries[i][1], 0);\n ans.push_back(val);\n\n }\n return ans;\n\n }\n};",
"memory": "43200"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n long long v=0;\n vector<long long>a;\n vector<int>ans;\n for(int i=0;i<arr.size();i++)\n {\n v^=(long long)arr[i];\n a.push_back(v);\n }\n for(int i=0;i<queries.size();i++)\n {\n ans.push_back((int)(a[queries[i][0]]^a[queries[i][1]]^arr[queries[i][0]]));\n }\n return ans;\n }\n};",
"memory": "43200"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> pXor, sXor;\n\n // Created prefix and suffix xor arrays\n int result = 0;\n for (int i = 0; i < arr.size(); i++) {\n result = result ^ arr[i];\n pXor.push_back(result);\n }\n\n // cout << \"Pxor\" << endl;\n\n // for (int x: pXor) {\n // cout << x << \" \";\n // } \n // cout << endl;\n\n result = 0;\n for (int i = arr.size()-1; i >= 0; i--) {\n result = result ^ arr[i];\n sXor.push_back(result);\n }\n\n reverse(sXor.begin(), sXor.end());\n\n cout << \"sXor\" << endl;\n\n // for (int x: sXor) {\n // cout << x << \" \";\n // } \n // cout << endl;\n\n int queryLen = queries.size();\n vector<int> ans;\n for (int i = 0; i < queries.size(); i++) {\n int startIdx = queries[i][0],\n endIdx = queries[i][1];\n\n int p = sXor[startIdx],\n q = pXor[endIdx];\n\n ans.push_back((p^q) ^ sXor[0]);\n }\n\n return ans;\n\n }\n};\n",
"memory": "43300"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> pXor, sXor;\n\n // Created prefix and suffix xor arrays\n int result = 0;\n for (int i = 0; i < arr.size(); i++) {\n result = result ^ arr[i];\n pXor.push_back(result);\n }\n\n // cout << \"Pxor\" << endl;\n\n // for (int x: pXor) {\n // cout << x << \" \";\n // } \n // cout << endl;\n\n result = 0;\n for (int i = arr.size()-1; i >= 0; i--) {\n result = result ^ arr[i];\n sXor.push_back(result);\n }\n\n reverse(sXor.begin(), sXor.end());\n\n // cout << \"sXor\" << endl;\n\n // for (int x: sXor) {\n // cout << x << \" \";\n // } \n // cout << endl;\n\n int queryLen = queries.size();\n vector<int> ans;\n for (int i = 0; i < queries.size(); i++) {\n int startIdx = queries[i][0],\n endIdx = queries[i][1];\n\n int p = sXor[startIdx],\n q = pXor[endIdx];\n\n ans.push_back((p^q) ^ sXor[0]);\n }\n\n return ans;\n\n }\n};\n",
"memory": "43300"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> pre, suf;\n pre.push_back(arr[0]);\n for(int i = 1; i < arr.size(); i++) {\n pre.push_back(pre.back() ^ arr[i]);\n }\n suf.push_back(arr.back());\n for(int i = arr.size() - 2; i >= 0; i--) {\n suf.push_back(suf.back() ^ arr[i]);\n }\n reverse(suf.begin(), suf.end());\n int total = pre.back();\n vector<int> ans;\n for(int i = 0; i < queries.size(); i++) {\n int x = queries[i][0], y = queries[i][1];\n int left = 0, right = 0;\n if(x != 0) left = pre[x - 1];\n if(y + 1 < suf.size()) right = suf[y + 1];\n int temp = left ^ right;\n temp ^= total;\n ans.push_back(temp);\n }\n return ans;\n }\n};",
"memory": "43400"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n #define mx 100001\n int arr[mx];\n int tree[mx * 3];\n void init(int node, int b, int e)\n {\n if (b == e) {\n tree[node] = arr[b];\n return;\n }\n int Left = node * 2;\n int Right = node * 2 + 1;\n int mid = (b + e) / 2;\n init(Left, b, mid);\n init(Right, mid + 1, e);\n tree[node] = tree[Left] ^ tree[Right];\n }\n int query(int node, int b, int e, int i, int j)\n {\n if (i > e || j < b)\n return 0; //বাইরে চলে গিয়েছে\n if (b >= i && e <= j)\n return tree[node]; //রিলেভেন্ট সেগমেন্ট\n int Left = node * 2; //আরো ভাঙতে হবে\n int Right = node * 2 + 1;\n int mid = (b + e) / 2;\n int p1 = query(Left, b, mid, i, j);\n int p2 = query(Right, mid + 1, e, i, j);\n return p1 ^ p2; //বাম এবং ডান পাশের যোগফল\n }\n\n /*int main()\n {\n READ(\"in\");\n int n;\n cin >> n;\n repl(i, n)\n cin\n >> arr[i];\n init(1, 1, n);\n\n update(1, 1, n, 2, 0);\n cout << query(1, 1, n, 1, 3) << endl;\n update(1, 1, n, 2, 2);\n cout << query(1, 1, n, 2, 2) << endl;\n return 0;\n }*/\n vector<int> xorQueries(vector<int>& ar, vector<vector<int>>& queries) {\n vector<int>ans;\n\n for(int i = 0; i<ar.size(); i++)\n {\n arr[i+1] = ar[i];\n }\n int n = ar.size();\n init(1,1,n);\n\n for(int i = 0; i<queries.size(); i++)\n {\n int x = query(1,1,n,queries[i][0]+1, queries[i][1]+1);\n ans.push_back(x);\n }\n return ans;\n }\n};",
"memory": "43500"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int seg[4*100000];\n \n void helper(vector<int>& arr,int index,int start,int end){\n if(start==end){\n seg[index]=arr[start];\n return;\n }\n \n int mid=(start+end)/2;\n helper(arr,2*index+1,start,mid);\n helper(arr,2*index+2,mid+1,end);\n \n seg[index]=seg[2*index+1]^seg[2*index+2];\n }\n \n int getAns(vector<int>& arr,int l,int r,int index,int start,int end){\n if(start>=l && r>=end){\n return seg[index];\n }\n if(start>r || l>end){\n return 0;\n }\n int mid = (start+end)/2;\n int ans1 = getAns(arr,l,r,2*index+1,start,mid);\n int ans2 = getAns(arr,l,r,2*index+2,mid+1,end);\n return ans1^ans2;\n }\n \n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n=arr.size();\n helper(arr,0,0,n-1);\n vector<int> out;\n for(int i=0;i<queries.size();i++){\n int l=queries[i][0];\n int r=queries[i][1];\n \n int ans = getAns(arr,l,r,0,0,n-1);\n out.push_back(ans);\n }\n return out;\n }\n};",
"memory": "43500"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> dp;\n int get_xor(vector<int>& arr, int i) {\n // get xor from first i elements\n if (dp[i] < 0)\n dp[i] = get_xor(arr, i - 1) ^ arr[i - 1];\n return dp[i];\n }\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n dp.resize(arr.size() + 1, -1);\n dp[0] = 0;\n vector<int> res(queries.size());\n for (int i = 0; i < queries.size(); i++) {\n int left = get_xor(arr, queries[i][0]);\n int right = get_xor(arr, queries[i][1] + 1);\n res[i] = left ^ right;\n }\n return res;\n }\n};",
"memory": "43600"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "#define ll int\n\nstruct segmenttree{\n vector<ll>v;\n vector<ll>tree;\n ll n;\n segmenttree(ll nn,vector<ll>vec)\n {\n v=vec;\n n=nn;\n tree.resize(4*n);\n build(1,0,n-1);\n }\n \n ll combine(ll a,ll b)\n {\n ll c;\n c=(a^b);\n return c;\n }\n\n void build(ll node,ll start,ll end)\n {\n if(start==end)\n {\n tree[node]=v[start];\n return;\n }\n ll mid=(start+end) >> 1;\n build(2*node,start,mid);\n build(2*node+1,mid+1,end);\n tree[node]=combine(tree[2*node],tree[2*node+1]);\n }\n\n void update(ll node,ll start,ll end,ll ind,ll val)\n {\n if(start==end)\n {\n v[start]=val;\n tree[node]=v[start];\n return;\n }\n ll mid=(start+end) >> 1;\n if(ind<=mid) update(2*node,start,mid,ind,val);\n else update(2*node+1,mid+1,end,ind,val);\n tree[node]=combine(tree[2*node],tree[2*node+1]);\n }\n\n void update(ll ind,ll val)\n {\n update(1,0,n-1,ind,val);\n }\n \n ll query(ll node,ll start,ll end,ll l,ll r)\n {\n if(start>r || end<l) return 0;\n else if(start>=l && end<=r) return tree[node];\n ll mid=(start+end) >> 1;\n ll left=query(2*node,start,mid,l,r);\n ll right=query(2*node+1,mid+1,end,l,r);\n return combine(left,right);\n }\n\n ll query(ll l,ll r)\n {\n return query(1,0,n-1,l,r);\n }\n};\n\nclass Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n segmenttree seg(arr.size(),arr);\n vector<int>ans;\n for(auto& it:queries) ans.emplace_back(seg.query(it[0],it[1]));\n return ans;\n }\n};",
"memory": "43600"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> accuXOR(arr.size());\n accuXOR[0] = arr[0];\n for (int i = 1; i < arr.size(); i++) {\n accuXOR[i] = accuXOR[i-1] ^ arr[i];\n }\n\n vector<pair<int,pair<int,int>>> q;\n for (int i = 0; i < queries.size(); i++) {\n q.push_back({queries[i][0], {queries[i][1], i}});\n }\n sort(q.begin(), q.end());\n\n vector<int> ans(queries.size());\n for (int i = 0; i < q.size(); i++) {\n int start = q[i].first;\n int end = q[i].second.first;\n int index = q[i].second.second;\n\n int accu = 0;\n if (start > 0) {\n accu = accuXOR[start-1];\n }\n ans[index] = accuXOR[end] - (accu & accuXOR[end]) + ((accu ^ accuXOR[end]) & accu);\n }\n return ans;\n }\n};",
"memory": "43700"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> ans(queries.size());\n for(int _ = 1; _ < arr.size(); _++) arr[_] ^= arr[_-1];\n\n int pos = 0;\n for(vector<int> _q : queries){\n if(_q[0]) ans[pos] = arr[_q[1]] ^ arr[_q[0]-1];\n else ans[pos] = arr[_q[1]];\n pos++;\n }\n\n return ans;\n }\n};",
"memory": "43800"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "#define ll long long\n\nclass solve{\n\n vector<ll> tree;\n ll sz;\n public:\n\n solve( ll n)\n {\n sz = 4*n;\n tree.resize(sz);\n }\n\n void build( ll l, ll r, ll i, vector<int> &arr)\n {\n if( l == r)\n {\n tree[i] = arr[l];\n return;\n }\n\n ll mid = (l + r)/2;\n\n build( l, mid, i*2 + 1, arr);\n build( mid+1, r, i*2 + 2, arr);\n\n\n tree[i] = tree[i*2 + 1] ^ tree[i*2 + 2];\n }\n\n ll res(ll l, ll r, ll x, ll y, ll i)\n {\n if( r < x || l > y)\n {\n return 0ll;\n }\n\n else if( l >= x && r <= y){\n\n return tree[i];\n\n }\n\n ll mid = (l + r)/2;\n\n return res( l, mid , x, y, i*2 + 1) ^ res(mid + 1, r, x, y, i*2 + 2);\n }\n\n\n};\nclass Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n\n ll n = arr.size();\n solve tree1(n);\n\n tree1.build( 0ll, n-1, 0ll, arr);\n ll sz = queries.size();\n\n vector<int> ans(sz);\n\n for(ll i = 0; i< queries.size(); i++)\n {\n ll x = queries[i][0];\n ll y = queries[i][1];\n ll sol = tree1.res(0ll, n-1, x, y, 0ll);\n\n ans[i] = sol;\n\n }\n\n return ans;\n \n }\n};",
"memory": "43800"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>&q) {\npartial_sum(arr.begin(),arr.end(),arr.begin(),bit_xor<int>());\nint i=0;vector<int>ans(q.size());\nfor(auto qe:q){\n int q0=qe[0],q1=qe[1];\nans[i++]=(q0==0)?arr[q1]:(arr[q1]^arr[q0-1]);//nullifying the terms that came before\n//take care of operator precedence\n\n}\nreturn ans;\n }\n};",
"memory": "43900"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> result_arr(queries.size(),0);\n for(int i = 1; i < arr.size();i++){\n arr[i] ^= arr[i-1];\n }\n int indx=0;\n for(auto query: queries){\n if (query[0] == 0)\n result_arr[indx++] = arr[query[1]];\n else result_arr[indx++] = arr[query[1]] ^ arr[query[0]-1];\n }\n return result_arr;\n }\n};",
"memory": "44000"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "\nstruct Node {\n int l;\n int r;\n int val;\n Node() {}\n Node(int i, int val) : l(i), r(i), val(val) {}\n Node(int l, int r, int val) : l(l), r(r), val(val) {}\n Node add(Node rval) {\n return Node(min(l, rval.l), max(r, rval.r), val ^ rval.val);\n }\n};\n\nclass SegmentTree {\n public:\n size_t depth; // depth >= 1\n vector<Node> buffer; // 1-based buffer\n\n SegmentTree() {}\n void build(vector<int>& arr) {\n depth = ceil(log2(arr.size())) + 1;\n buffer = vector<Node>(1 << depth, Node());\n\n // Fill leaf\n int offset = 1 << (depth - 1);\n for (int i = 0; i < (int)arr.size(); i++) {\n buffer[offset + i] = Node(i, arr[i]);\n }\n for (int i = (int)arr.size(); i < (1 << (depth - 1)); i++) {\n buffer[offset + i] = Node(i, 0);\n }\n // Bottom up building\n for (int i = offset - 1; i >= 1; i--) {\n buffer[i] = buffer[i * 2].add(buffer[i * 2 + 1]);\n }\n }\n int query(int l, int r) {\n return recursive_query(1, l, r);\n }\n int recursive_query(int i, int l, int r) {\n l = max(l, buffer[i].l);\n r = min(r, buffer[i].r);\n if (l == buffer[i].l && r == buffer[i].r) {\n // Matched\n return buffer[i].val;\n }\n int lval = 0;\n int rval = 0;\n if (l <= buffer[i * 2].r)\n lval = recursive_query(i * 2, l, r);\n if (r >= buffer[i * 2 + 1].l)\n rval = recursive_query(i * 2 + 1, l, r);\n return lval ^ rval;\n }\n};\n\nclass Solution {\n public:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n SegmentTree segment_tree;\n segment_tree.build(arr);\n vector<int> result;\n for (auto& q : queries) {\n result.push_back(segment_tree.query(q[0], q[1]));\n }\n return result;\n }\n};",
"memory": "44100"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> result(queries.size());\n int index=0;\n for(auto i:queries){\n int start=i[0];\n int end=i[1];\n int sum=0;\n for(int j=start;j<=end;j++){\n sum^=arr[j];\n }\n result[index]=sum;\n index++;\n }\n return result;\n }\n};",
"memory": "44100"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) \n {\n vector<int> res(queries.size(), 0);\n\n for (int i = 1; i < arr.size(); i++)\n arr[i] = arr[i] ^ arr[i-1];\n\n int i = 0;\n for (vector<int> q: queries)\n { \n int left = q[0];\n int right = q[1];\n\n if (left == 0)\n res[i] = arr[right];\n else\n res[i] = arr[right] ^ arr[left - 1];\n \n i++;\n } \n\n return res;\n }\n};",
"memory": "44200"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> res;\n for(int i=1;i<arr.size();i++)\n arr[i]^=arr[i-1];\n for(vector<int> q: queries)\n res.push_back(arr[q[1]]^(q[0]>0?arr[q[0]-1]:0));\n return res;\n }\n};",
"memory": "44300"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector< int > res;\n for(auto it : queries) {\n int temp = 0;\n for(int i = it[0]; i <= it[1]; ++i) {\n temp ^= arr[i];\n }\n res.push_back(temp);\n }\n return res;\n }\n};",
"memory": "44300"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n vector<int> ans;\n int tempval = 0;\n for(auto it : queries){\n int start = it[0];\n int end = it[1];\n for(int i = start ; i <= end; i++){\n tempval = tempval^arr[i];\n }\n ans.push_back(tempval);\n tempval = 0;\n }\n return ans;\n }\n};",
"memory": "44400"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& q) {\n vector<int> v;\n for(auto it:q)\n {\n int l=it[0];\n int r=it[1];\n int ans=0;\n for(int i=l;i<=r;++i)ans^=arr[i];\n v.push_back(ans);\n }\n return v;\n\n \n }\n};",
"memory": "44500"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n // unordered_map<int,int>mp;\n // for(int i=0; i<arr.size(); i++) mp[i] = arr[i];\n\n vector<int>ans;\n for(int i=0; i<queries.size(); i++){\n int XOR = 0;\n vector<int> v= queries[i];\n for(int i=v[0]; i<=v[1]; i++){\n XOR ^= arr[i]; \n }\n ans.emplace_back(XOR);\n }\n\n return ans;\n \n }\n};",
"memory": "44500"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> finans;\n // first approach\n\n // for(auto it:queries){\n // int left = it[0];\n // int right = it[1];\n // int ans = arr[left];\n // for(int i = left + 1 ; i <= right ; i++){\n // ans = ans ^ arr[i];\n // }\n // finans.push_back(ans);\n // }\n // return finans;\n\n // 2nd approach\n\n int n = arr.size();\n for(int i = 1 ; i < n ; i++){\n arr[i] = arr[i] ^ arr[i-1];\n }\n for(auto it:queries){\n int left = it[0];\n int right = it[1];\n if(left == 0){\n finans.push_back(arr[right]);\n }\n else{\n finans.push_back(arr[right] ^ arr[left - 1]);\n }\n }\n return finans;\n\n\n\n \n }\n};",
"memory": "44600"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> finans;\n // first approach\n\n // for(auto it:queries){\n // int left = it[0];\n // int right = it[1];\n // int ans = arr[left];\n // for(int i = left + 1 ; i <= right ; i++){\n // ans = ans ^ arr[i];\n // }\n // finans.push_back(ans);\n // }\n // return finans;\n\n // 2nd approach\n\n int n = arr.size();\n for(int i = 1 ; i < n ; i++){\n arr[i] = arr[i] ^ arr[i-1];\n }\n for(auto it:queries){\n int left = it[0];\n int right = it[1];\n if(left == 0){\n finans.push_back(arr[right]);\n }\n else{\n finans.push_back(arr[right] ^ arr[left - 1]);\n }\n }\n return finans;\n\n\n\n \n }\n};",
"memory": "44600"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n for(int i=1;i<arr.size();i++){\n arr[i] ^= arr[i-1];\n }\n vector<int> rt;\n for(auto i: queries){\n if(i[0]) rt.push_back(arr[i[1]] ^ arr[i[0]-1]);\n else rt.push_back(arr[i[1]]);\n }\n return rt;\n }\n};",
"memory": "44700"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int xorTillNow = 0;\n for(int i=0; i<arr.size(); i++)\n {\n xorTillNow = xorTillNow^arr[i];\n arr[i] = xorTillNow;\n }\n\n vector<int> ans;\n for(auto query : queries)\n {\n int left = query[0];\n int right = query[1];\n int leftXOR = 0;\n int rightXOR = arr[right];\n --left;\n if(left >= 0)\n leftXOR = arr[left];\n \n ans.push_back(leftXOR ^ rightXOR); \n }\n return ans;\n }\n};",
"memory": "44700"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n int n = arr.size();\n vector<int> pre(n+1);\n int x = 0;\n pre[0] = 0;\n for(int i=0;i<n;i++){\n x = x^arr[i];\n pre[i+1] = x;\n }\n vector<int> ans;\n for(auto q : queries){\n int a = pre[q[1]+1]^pre[q[0]];\n ans.push_back(a);\n }\n return ans;\n }\n};",
"memory": "44800"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> answer;\n vector<int> prefixXor(arr.size());\n prefixXor[0] = arr[0];\n for (int i = 1; i < arr.size(); i++)\n prefixXor[i] = prefixXor[i - 1] ^ arr[i];\n\n for (auto query : queries) {\n int start = query[0];\n int end = query[1];\n int result = prefixXor[end];\n if (start > 0)\n result = result ^ prefixXor[start - 1];\n answer.push_back(result);\n }\n return answer;\n }\n};",
"memory": "44800"
} |
1,435 | <p>You are given an array <code>arr</code> of positive integers. You are also given the array <code>queries</code> where <code>queries[i] = [left<sub>i, </sub>right<sub>i</sub>]</code>.</p>
<p>For each query <code>i</code> compute the <strong>XOR</strong> of elements from <code>left<sub>i</sub></code> to <code>right<sub>i</sub></code> (that is, <code>arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>]</code> ).</p>
<p>Return an array <code>answer</code> where <code>answer[i]</code> is the answer to the <code>i<sup>th</sup></code> query.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>Output:</strong> [8,0,4,4]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length, queries.length <= 3 * 10<sup>4</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 <= left<sub>i</sub> <= right<sub>i</sub> < arr.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {\n vector<int> prefXOR;\n int curr = 0;\n for(auto x:arr){\n curr = x^curr;\n prefXOR.push_back(curr);\n }\n vector<int> res;\n for(auto x:queries){\n if(x[0]-1>=0)\n {\n res.push_back(prefXOR[x[1]] ^ prefXOR[x[0]-1] );\n }\n else{\n res.push_back(prefXOR[x[1]]);\n }\n }\n return res;\n \n }\n};",
"memory": "45200"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int minInsertions(string s) {\n int n = s.size();\n int dp[n], prev, ans;\n for(int l = n - 1; l >= 0; l--) {\n dp[l] = 0;\n if(l + 1 < n)prev = dp[l + 1], dp[l + 1] = (s[l] != s[l + 1]);\n for(int r = l + 2; r < n; r++) {\n ans = min(dp[r - 1], dp[r]) + 1;\n if(s[l] == s[r]) ans = min(ans, prev);\n prev = dp[r];\n dp[r] = ans;\n }\n }\n return dp[n - 1];\n }\n};",
"memory": "7600"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\nint lcs(string &text1, string &text2) {\n int n1 = text1.size();\n int n2 = text2.size();\n int dp[n2+1];\n memset(dp,0,sizeof(dp));\n for (int i = n1 - 1; i >= 0; i--) {\n int last_diagonal = 0;\n for (int j = n2 - 1; j >= 0; j--) {\n int temp = dp[j];\n if (text1[i] == text2[j]) dp[j] = 1 + last_diagonal;\n else dp[j] = max(dp[j], dp[j + 1]);\n last_diagonal = temp;\n }\n }\n\n return dp[0];\n}\n\n// bd\n// db\n\nint minInsertions(string s) {\n string str = s;\n reverse(str.begin(),str.end());\n int x = lcs(s,str);\n int n = s.size();\n return n - x;\n}\n};",
"memory": "7700"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\nint lcs(string &text1, string &text2) {\n int n1 = text1.length();\n int n2 = text2.length();\n int dp[n2+1];\n memset(dp,0,sizeof(dp));\n for (int i = n1 - 1; i >= 0; i--) {\n int last_diagonal = 0;\n for (int j = n2 - 1; j >= 0; j--) {\n int temp = dp[j];\n if (text1[i] == text2[j]) dp[j] = 1 + last_diagonal;\n else dp[j] = max(dp[j], dp[j + 1]);\n last_diagonal = temp;\n }\n }\n\n return dp[0];\n}\n\n// bd\n// db\n\nint minInsertions(string s) {\n string str = s;\n reverse(str.begin(),str.end());\n int x = lcs(s,str);\n int n = s.length();\n return n - x;\n}\n};",
"memory": "7700"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n return s.length() - longestPalindromeSubseq(s);\n }\nprivate:\n int longestPalindromeSubseq(string s) {\n int n = s.length();\n \n int dp[n];\n fill(&dp[0], &dp[0] + n, 0);\n for(int i=n-1; i>=0; --i){\n int prev = dp[i];\n dp[i] = 1;\n for(int j=i+1; j<n; ++j){\n int temp = dp[j];\n dp[j] = s[i]==s[j] ? prev + 2 : max(dp[j], dp[j-1]);\n prev = temp;\n }\n }\n \n return dp[n-1];\n }\n};",
"memory": "7800"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n std::vector<int> dp(s.size() + 1);\n std::iota(std::begin(dp), std::end(dp), 0);\n\n for(int i = 1; i <= s.size(); i ++) {\n int temp = i - 1;\n dp[0] = i;\n for(int j = 1; j <= s.size(); j ++) {\n int next_temp = dp[j];\n int bi = s.size() - j;\n if(s[i - 1] == s[bi]) dp[j] = temp;\n else dp[j] = 1 + std::min(dp[j - 1], dp[j]);\n temp = next_temp; \n }\n }\n\n return dp[s.size()] / 2; \n }\n};",
"memory": "7900"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int lcs(string &text1,string &text2){\n int n = text1.size(), m = text2.size();\n vector<int>curr(m+1,0),next(m+1,0);\n for(int i=n-1;i>=0;i--){\n for(int j=m-1;j>=0;j--){\n if(text1[i] == text2[j])curr[j] = 1 + next[j+1];\n else curr[j] = max(next[j],curr[j+1]);\n }\n next = curr;\n }\n return curr[0];\n }\n int minInsertions(string s) {\n string t = s;\n reverse(s.begin(),s.end());\n return s.length()-lcs(s,t);\n }\n};",
"memory": "8000"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n int longestCommonSubsequence(string s1, string s2) {\n int n = s1.size();\n int m = s2.size();\n // vector<vector<int>> dp(n+1 , vector<int>(m+1 , 0));\n // return func(n, m , s1 , s2 , dp);\n vector<int> prev(m + 1, 0), curr(m + 1, 0);\n for (int ind2 = 0; ind2 <= m; ind2++)\n prev[ind2] = 0;\n // for(int ind1 = 0 ; ind1<= n ; ind1++) dp[ind1][0] = 0;\n for (int ind1 = 1; ind1 <= n; ind1++) {\n for (int ind2 = 1; ind2 <= m; ind2++) {\n if (s1[ind1 - 1] == s2[ind2 - 1])\n curr[ind2] = 1 + prev[ind2 - 1];\n else\n curr[ind2] = max(prev[ind2], curr[ind2 - 1]);\n }\n prev = curr;\n }\n return prev[m];\n }\n\npublic:\n int minInsertions(string s) {\n string t = s;\n reverse(s.begin(), s.end());\n int lps = longestCommonSubsequence(s, t);\n return s.size() - lps;\n }\n};",
"memory": "8100"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int lcs(string s, string t) {\n vector<int> prev(t.length() + 1, 0), curr(t.length() + 1, 0);\n for (int i = 0; i <= s.length(); i++)\n prev[i] = 0;\n for (int i = 1; i <= s.length(); i++) {\n for (int j = 1; j <= t.length(); j++) {\n if (s[i - 1] == t[j - 1])\n curr[j] = 1 + prev[j - 1];\n else\n curr[j] = max(prev[j], curr[j - 1]);\n }\n prev = curr;\n }\n return prev[t.length()];\n }\n int minInsertions(string s) {\n string t = s;\n reverse(t.begin(), t.end());\n return s.length() - lcs(s, t);\n }\n};",
"memory": "8400"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\nint f(string s, string t) {\n int n = s.size();\n int m = t.size();\n \n vector<int> prev(m+1, 0), cur(m+1, 0); // Only two 1D arrays for space optimization\n \n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i-1] == t[j-1]) {\n cur[j] = 1 + prev[j-1];\n } else {\n cur[j] = max(prev[j], cur[j-1]);\n }\n }\n prev = cur; // Move current row to previous row for the next iteration\n }\n \n return prev[m];\n}\nint lcs(string s, string t){\n int n=s.size();\n int m=t.size();\n vector<int> cur(m+1);\n vector<int> prev(m+1);\n return f(s,t);\n}\n \n int minInsertions(string s) {\n int size=s.length();\n string n= s;\n reverse(n.begin(),n.end());\n int staticLen= lcs(s,n);\n return size - staticLen; \n }\n};",
"memory": "8400"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "// using tabulation with space optimization\nclass Solution {\nprivate:\n int solve(string s) {\n\n int n = s.length();\n string t(s.rbegin(), s.rend());\n vector<int> prev(n+1, 0), curr(n+1, 0);\n \n for(int i = 1; i <= n; i++) {\n for(int j = 1; j <= n; j++) {\n if(s[i-1] == t[j-1]) curr[j] = 1 + prev[j-1];\n else curr[j] = max(prev[j] , curr[j-1]);\n }\n prev = curr;\n }\n return prev[n];\n }\npublic:\n int minInsertions(string s) {\n \n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int n = s.length();\n return n - solve(s);\n\n }\n};",
"memory": "8500"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int spaceopt(string text1,string text2)\n {\n vector<int>curr(text2.size()+1,0);\n vector<int>next(text2.size()+1,0);\n\n for(int i=text1.size()-1;i>=0;i--)\n {\n for(int j=text2.size()-1;j>=0;j--)\n {\n int ans=0;\n if(text1[i]==text2[j])\n {\n ans= 1+next[j+1];\n \n }\n else {\n ans= max(curr[j+1],next[j]);\n }\n curr[j]=ans;\n }\n next=curr;\n }\n return next[0];\n }\n int minInsertions(string s) {\n string reverses=s;\n reverse(reverses.begin(),reverses.end());\n cout<<spaceopt(s,reverses)<<endl;\n return s.size()-spaceopt(s,reverses);\n }\n};",
"memory": "8600"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n string r(s.rbegin(), s.rend());\n int n = s.size();\n int t[n+1][n+1];\n\n for (int i=0; i<n+1; i++)\n {\n for (int j=0; j<n+1; j++)\n {\n if (i==0||j==0)\n t[i][j] = 0;\n }\n }\n\n for (int i=1; i<n+1; i++)\n {\n for (int j=1; j<n+1; j++)\n {\n if (s[i-1]==r[j-1])\n {\n t[i][j] = 1 + t[i-1][j-1];\n }\n else\n {\n t[i][j] = max(t[i-1][j], t[i][j-1]);\n }\n }\n }\n\n return n - t[n][n];\n }\n};",
"memory": "8700"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string a) {\n int n = a.length();\n string b = a;\n reverse(b.begin(), b.end());\n \n int t[n+1][n+1];\n for(int i=0; i<=n; i++){\n t[i][0] = 0;\n t[0][i] = 0;\n }\n \n for(int i=1; i<=n; i++){\n for(int j=1; j<=n; j++){\n if(a[i-1] == b[j-1]){\n t[i][j] = 1 + t[i-1][j-1];\n }\n else{\n t[i][j] = max(t[i-1][j], t[i][j-1]);\n }\n }\n }\n \n return n - t[n][n];\n }\n};",
"memory": "8800"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[501][501];\n int solve(int i, int j, string& s){\n\n if(i>=j){\n return 0;\n }\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n\n if(s[i] == s[j]){\n return dp[i][j] = solve(i+1, j-1, s);\n }\n\n int insert_i = 1 + solve(i, j-1, s);\n int insert_j = 1 + solve(i+1, j, s);\n\n return dp[i][j] = min(insert_i, insert_j);\n }\n int minInsertions(string s) {\n memset(dp, -1, sizeof(dp));\n int n = s.size();\n\n return solve(0, n-1, s);\n }\n};",
"memory": "8900"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int dp[501][501];\n\n int f(int i, int j, string &s){\n if(i<0 || j>=s.size()){\n return 0;\n }\n if(dp[i][j]!=-1) return dp[i][j];\n if(s[i] == s[j]){\n return dp[i][j] = 1+f(i-1, j+1, s);\n }\n return dp[i][j] = max(f(i-1, j, s), f(i, j+1, s));\n }\n\n int minInsertions(string s) {\n memset(dp, -1, sizeof(dp));\n return s.size() - f(s.size()-1, 0, s);\n }\n};",
"memory": "9000"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\nstring a, b;\nint n;\nint dp[501][501];\n\nint rec(int i , int j){\n // basecase\n if( i == n) return n - j;\n if( j == n) return n - i;\n // cache check\n if(dp[i][j] !=-1){\n return dp[i][j];\n }\n\n // transition\n int ans = 1e9;\n if(a[i] == b[j]){\n ans = min(ans, rec(i+1,j+1));\n } else {\n ans = min(1 + rec(i,j+1), 1 + rec(i+1,j));\n }\n // save and return\n return dp[i][j] = ans;\n}\n\npublic:\n int minInsertions(string s) {\n a = s;\n reverse(s.begin(),s.end());\n b = s;\n n = s.size();\n memset(dp,-1,sizeof(dp));\n\n int ans = rec(0,0);\n return ans/2;\n }\n};",
"memory": "9000"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint n,m;\n int dp[501][501];\n int rec(int i,int j,string &text1,string &text2){\n // pruning\n if(i>=n || j>=m) return 0;\n // basecase\n\n // cache check\n if(dp[i][j]!=-1) return dp[i][j]; \n // compute\n int ans = 0;\n if(text1[i]==text2[j]) ans = max(ans,1+rec(i+1,j+1,text1,text2));\n else ans = max(rec(i+1,j,text1,text2),rec(i,j+1,text1,text2));\n \n // save and return\n dp[i][j] = ans;\n return ans;\n }\n int minInsertions(string text2) {\n string text1 = text2;\n reverse(text2.begin(),text2.end());\n this->n = text1.size();\n this->m = text2.size();\n memset(dp,-1,sizeof(dp));\n int ans = rec(0,0,text1,text2);\n return n-ans;\n }\n};",
"memory": "9100"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[501][501];\n int solve(int i,int j,string &s)\n {\n if(i>=j) return 0;\n if(dp[i][j]!=-1) return dp[i][j];\n if(s[i]==s[j]){\n return dp[i][j]=solve(i+1,j-1,s);\n }\n\n return dp[i][j]=1+min(solve(i+1,j,s),solve(i,j-1,s));\n }\n int minInsertions(string s) {\n memset(dp,-1,sizeof(dp));\n int n=s.size();\n return solve(0,n-1,s);\n }\n};",
"memory": "9100"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string str) {\n int n = str.size();\n int ans = n - LPS(str , n);\n return ans;\n }\n\n\nint LPS(string str1 , int n )\n{\n string str2 = str1;\n \n reverse(str2.begin() , str2.end());\n \n \n return LCS( str1 , str2);\n}\n\n\n\nint LCS ( string str1 , string str2)\n{\n int n = str1.size();\n int m = str2.size();\n \n int t[n+1][m+1];\n \n for(int i =0 ; i<=n ; i++)\n {\n for(int j =0 ; j<=m ; j++)\n {\n if(i==0 || j==0)\n {\n t[i][j]=0;\n }\n }\n }\n \n \n for(int i=1 ; i<=n ; i++)\n {\n for(int j =1 ; j<=m ; j++)\n {\n if(str1[i-1] == str2[j-1])\n {\n t[i][j] = 1+t[i-1][j-1];\n }\n else\n {\n t[i][j] = max(t[i-1][j] , t[i][j-1]);\n }\n }\n }\n return t[n][m];\n}\n\n\n};\n\n",
"memory": "9200"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[501][501];\n // int solve(int ind1,int ind2,string &s ,string &t){\n // if(ind1<0 || ind2<0) return 0;\n // if(dp[ind1][ind2]!=-1) return dp[ind1][ind2];\n // if(s[ind1]==t[ind2]) return dp[ind1][ind2]=1+solve(ind1-1,ind2-1,s,t);\n // else return dp[ind1][ind2]=max(solve(ind1-1,ind2,s,t),solve(ind1,ind2-1,s,t));\n // }\n int minInsertions(string s) {\n string t=s;\n reverse(t.begin(),t.end());\n int n=s.size();\n memset(dp,0,sizeof(dp));\n vector<int>prev(n+1,0);\n vector<int>curr(n+1,0);\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n if(s[i-1]==t[j-1]) dp[i][j]=1+dp[i-1][j-1];\n else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);\n }\n }\n int len=dp[n][n];\n\n return s.size()-len;\n }\n};",
"memory": "9300"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n int lcs(string& s, string& t) {\n\t int n = (int)s.size(), m = (int)t.size();\n\t vector<int> prev(m + 1), cur;\n\t for (int i = n - 1; i >= 0; i--) {\n\t\t cur.assign(m + 1, 0);\n\t\t for (int j = m - 1; j >= 0; j--) {\n\t\t \tif (s[i] == t[j])\n\t\t\t \tcur[j] = prev[j + 1] + 1;\n\t\t\t else\n\t\t\t \tcur[j] = max(prev[j], cur[j + 1]);\n\t\t }\n\t\t prev = cur;\n \t}\n\t return prev[0];\n }\npublic:\n int minInsertions(string s) {\n string t = s;\n reverse(t.begin(), t.end());\n return (int)s.size() - lcs(s, t);\n }\n};",
"memory": "9400"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int lcs(string s1, string s2) {\n int n=s1.size();\n int m=s2.size();\n\n // vector<vector<int>> dp(n+1,vector<int>(m+1,0));\n vector<int> prev(m+1,0);\n vector<int> cur(m+1,0);\n\n\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n if(s1[i-1]==s2[j-1]){\n cur[j]=1+prev[j-1];\n\n }\n else cur[j] = max(prev[j] , cur[j-1]);\n }\n prev=cur;\n \n }\n return prev[m];\n }\n\n int lps(string s) {\n stack <int> st;\n string t=\"\";\n for(auto it:s){\n st.push(it);\n\n }\n\n while(!st.empty()){\n t+=st.top();\n st.pop();\n }\n\n return lcs(s,t);\n \n }\n int minInsertions(string s) {\n return s.size()-lps(s);\n }\n};",
"memory": "9700"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "int n;\nstring s;\nint dp[501][501];\n\nint rec(int l,int r){\n\n\tif(l >= r) return 0;\n\n\tif(dp[l][r]!=-1) return dp[l][r];\n\n\tint ans = 1e9;\n\tif(s[l]==s[r]){\n\t\tans = min(ans,rec(l+1,r-1));\n\t}\t\n\n\tans = min({ans,1+rec(l+1,r),1+rec(l,r-1)});\n\treturn dp[l][r]=ans;\n}\n\nclass Solution {\npublic:\n int minInsertions(string s) {\n :: s = s;\n n = s.size();\n memset(dp,-1,sizeof(dp));\n return rec(0,n-1);\n }\n};",
"memory": "9900"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "#pragma GCC optimize(\"Ofast\")\nstatic auto _ = []() {ios::sync_with_stdio(false); cin.tie(nullptr); return nullptr; }();\n\n\nclass Solution {\nprivate:\n string s;\n long long memo[501][501];\n long long dp(long long i, long long j)\n {\n\n if (i >= j)\n return 0;\n \n long long& ret = memo[i ][j];\n if (ret != -1)\n return ret;\n \n ret = INT_MAX;\n \n\n if (s[i] == s[j])\n return ret = dp(i + 1, j - 1);\n\n // b a d\n\n long long c1 = dp(i + 1, j);\n\n long long c2 = dp(i, j - 1);\n\n\n ret = 1 + min(c1, c2);\n \n \n return ret;\n }\npublic:\n long long minInsertions(string s) {\n this->s = move(s);\n memset (memo , -1 , sizeof (memo));\n return dp(0, (long long)this->s.length() - 1);\n }\n};\n",
"memory": "10000"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minInsertions(string s) {\n int n = s.size();\n int f[n][n];\n memset(f, -1, sizeof(f));\n function<int(int, int)> dfs = [&](int i, int j) -> int {\n if (i >= j) {\n return 0;\n }\n if (f[i][j] != -1) {\n return f[i][j];\n }\n int ans = 1 << 30;\n if (s[i] == s[j]) {\n ans = dfs(i + 1, j - 1);\n } else {\n ans = min(dfs(i + 1, j), dfs(i, j - 1)) + 1;\n }\n return f[i][j] = ans;\n };\n return dfs(0, n - 1);\n }\n};",
"memory": "10100"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[501][501];\n int solve(string &s, int l, int r) {\n if (l >= r) return 0;\n if (dp[l][r] != -1) return dp[l][r];\n int ans = INT_MAX;\n if (s[l] == s[r])\n ans = min(ans, solve(s, l+1, r-1));\n else \n ans = min({ans, 1 + solve(s, l, r-1), 1 + solve(s, l+1, r)});\n return dp[l][r] = ans;\n }\n int minInsertions(string s) {\n int n = s.length();\n memset(dp, -1, sizeof(dp));\n return solve(s, 0, n-1);\n }\n};",
"memory": "10200"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[501][501];\n int n;\n int rec(int i, int j, string &s){\n if(i >= j) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n int ans = 1e9;\n if(s[i] != s[j]) ans = min({ans, 1 + rec(i+1, j, s), 1 + rec(i, j-1, s)});\n else ans = min(ans, rec(i+1, j-1, s));\n return dp[i][j] = ans;\n }\n int minInsertions(string s) {\n n = s.length();\n memset(dp, -1, sizeof(dp));\n return rec(0, n-1, s);\n }\n};",
"memory": "10300"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int LCS(string a,string b,int m,int n){\n int t[1001][1001];\n for(int i=0;i<=m;i++){\n for(int j=0;j<=n;j++){\n if(i==0 || j==0){\n t[i][j]=0;\n }\n }\n }\n for(int i=1;i<=m;i++){\n for(int j=1;j<=n;j++){\n if(a[i-1]==b[j-1]){\n t[i][j]=1+t[i-1][j-1];\n }\n else{\n t[i][j]=max(t[i-1][j],t[i][j-1]);\n }\n }\n }\n return t[m][n];\n }\n int LPS(string x){\n int size1=x.length();\n string y=x;\n int size2=y.length();\n reverse(y.begin(),y.end());\n return LCS(x,y,size1,size2);\n }\n int minInsertions(string s) {\n int ans=LPS(s);\n return (s.length()-ans);\n }\n};",
"memory": "10400"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int dp[600][600];\n int f(string& s, int i, int j){\n if(i>=j) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n int a,b,c; a=b=c=1e5;\n if(s[i] == s[j]){\n a = f(s, i+1, j-1);\n }\n else{\n b = 1 + f(s, i+1, j);\n c = 1 + f(s, i, j-1);\n }\n \n\n return dp[i][j] = min({a,b,c});\n }\n\n int minInsertions(string s) {\n memset(dp,-1, sizeof(dp));\n return f(s, 0,s.size()-1);\n }\n};",
"memory": "10700"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n int lcs(string s1,string s2,int m,int n){\n int static t[1001][1001];\n memset(t,-1,sizeof(t));\n for (int i = 0; i < m+1; i++)\n {\n for (int j = 0; j < n+1; j++)\n {\n if(i==0 || j==0) t[i][j]=0;\n }\n \n }\n \n for (int i = 1; i < m+1; i++)\n {\n for (int j = 1; j < n+1; j++)\n {\n if(s1[i-1]==s2[j-1]) t[i][j]=1+t[i-1][j-1];\n else t[i][j]=max(t[i-1][j],t[i][j-1]);\n }\n \n }\n return t[m][n];\n }\npublic:\n int minInsertions(string s1) {\n string s2=s1;\n reverse(s2.begin(),s2.end());\n int lps= lcs(s1,s2,s1.length(),s2.length());\n return s1.size()-lps;\n }\n};",
"memory": "11700"
} |
1,437 | <p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>
<p>Return <em>the minimum number of steps</em> to make <code>s</code> palindrome.</p>
<p>A <b>Palindrome String</b> is one that reads the same backward as well as forward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "zzazz"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string "zzazz" is already palindrome we do not need any insertions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "mbadm"
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be "mbdadbm" or "mdbabdm".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode"
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes "leetcodocteel".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 500</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "int dp[1001][1001];\nint solve(string &s,int i,int j){\n if(i==j){\n return 0;\n }\n\n if(i>j){\n return 0;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n int ans=0;\n if(s[i]==s[j]){\n ans=solve(s,i+1,j-1);\n }else{\n ans=1+min(solve(s,i+1,j),solve(s,i,j-1));\n }\n dp[i][j]=ans;\n return ans;\n}\n\nclass Solution {\npublic:\n int minInsertions(string s) {\n memset(dp,1001,sizeof(dp));\n //return solve(s,0,s.size()-1);\n for(int i=0;i<s.size();i++){\n for(int j=0;j+i<s.size();j++){\n if(i==0){\n dp[j][i+j]=0;\n }else if(i==1){\n dp[j][j+i]=1;\n if(s[j]==s[i+j]){\n dp[j][j+i]=0;\n }\n }else if(s[j]==s[j+i]){\n dp[j][j+i]=dp[j+1][j+i-1];\n }else{\n dp[j][i+j]=1+min(dp[j+1][j+i],dp[j][j+i-1]);\n }\n }\n }\n return dp[0][s.size()-1];\n }\n};",
"memory": "11800"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.