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<s...
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...
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<s...
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...
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<s...
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<...
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<s...
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...
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<s...
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...
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<s...
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...
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<s...
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 < ...
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<s...
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 f...
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<s...
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...
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<s...
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] = (an...
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<s...
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 :...
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<s...
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 ...
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<s...
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 ...
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<s...
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] ...
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<s...
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();...
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<s...
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] ^ ar...
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<s...
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] ...
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<s...
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] ...
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<s...
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 ...
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<s...
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 i...
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<s...
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 ...
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<s...
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,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<s...
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 ...
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<s...
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 ...
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<s...
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;\...
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<s...
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 ...
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<s...
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 = queri...
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<s...
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 ...
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<s...
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...
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<s...
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 ...
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<s...
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 ...
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<s...
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 ...
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<s...
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 ...
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<s...
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 ...
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<s...
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...
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<s...
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 o...
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<s...
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 o...
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<s...
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);...
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<s...
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...
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<s...
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 ...
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<s...
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 segTre...
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<s...
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 ...
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<s...
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 ...
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<s...
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 ...
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<s...
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()...
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<s...
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 ...
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<s...
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 ...
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<s...
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>>...
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<s...
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 retur...
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<s...
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<...
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<s...
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] ...
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<s...
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 ret...
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<s...
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 term...
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<s...
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 ...
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<s...
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 SegmentTre...
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<s...
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=sta...
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<s...
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 ...
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<s...
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 ...
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<s...
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 re...
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<s...
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 = st...
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<s...
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(an...
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<s...
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 ...
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<s...
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 ...
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<s...
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 ...
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<s...
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 ...
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<s...
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 ...
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<s...
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 ...
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<s...
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...
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<s...
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:q...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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++)...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 in...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 ...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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; ...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 ...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 ...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 ...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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++) {\...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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] ==...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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+...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 ...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 ...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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(in...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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] ==...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 ...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 ...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 // els...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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] =...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 {\npub...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 ...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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,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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 = m...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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(...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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+...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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 fo...
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>&nbsp;palindrome.</p> <p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p> <p>&nbsp;</p> <p><s...
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)...