id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int neg_index=1;\n int pos_index=0;\n int n = nums.size();\n int ans[n];\n for (int i=0;i<n;i++){\n if (nums[i]<0){\n ans[neg_index] = nums[i];\n neg_...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> op;\n int p=0;\n for(int i=0;i<nums.size();i++)\n {\n \n if(nums[i]>0)\n {\n \n op.push_back(nums[i]);\n // p++;\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>ans;\n int n = nums.size() ;\n int temp1[n];\n int temp2[n];\n int a = 0,b=0,c=0;\n while(a<n){\n if(nums[a] >0){\n temp1[b] = nums[a];\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "\nauto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n \n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> ans;\n int i=0,j=0;\n int n=nums.size();\n while(i<n && j<n){\n while(nums[i]<0){\n...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int p1=0,p2=0;\n if (nums.size()==0 || nums.size()==2)\n {\n if (nums[0]>0)\n return nums;\n else\n return {nums[1],nums[0]};\n }\n while (...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx2\")\nstatic auto _ = [](){\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return nullptr;\n}();\n\nclass Solution {\npublic:\n inline int sign(int n) const {\n return (n > 0) - (n < 0);...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "int speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n = nums.size();\n int res[n];\n vector<int> vres;\n int pos = 0, neg = 1;\n for(int i=0;...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int size=nums.size();\n vector<int> pos(size);\n vector<int> neg(size);\n int p=0,n=0;\n for(int i=0;i<size;i++){\n if(nums[i]>=0){\n pos[p]=nums[i];\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> nums1(nums.size());\n vector<int> nums2(nums.size());\n int k=0;\n int l=0;\n for(int i=0;i<nums.size();i++) {\n if(nums[i]<0) {\n nums1[k] = nums[i];\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n auto neg = stable_partition(nums.begin(), nums.end(), [](int n){ return n > 0; });\n auto pos = nums.begin();\n vector<int> res;\n while (neg != nums.end()) {\n res.push_back(*pos++);\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> p(n,0);\n vector<int> n_(n,0);\n int n_c=0,p_c=0;\n for(int i=0;i<n;i++){\n if(nums[i]<0){\n n_[n_c]=nums[i];\n n_c++...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\n // void helper(vector<int>& nums, vector<int> & ans){\n // int size = nums.size();\n // vector<int> visited(size, 0);\n \n // for(int k = 0 ; k < size; k++){\n \n // if(k % 2 == 0){\n // int j = 0;\n // for(int i = 0; i < size; i++){\...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int neg = 0;\n int pos = 0;\n int i = 0;\n int n = nums.size();\n\n // Find the first negative and positive indices\n while (nums[neg] >= 0) neg++;\n while (nums[pos] < 0) pos++...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n vector<int> pos;\n vector<int> neg;\n int n = nums.size();\n for(int i = 0; i < n ;i++)\n {\n if(nums[i] < 0)\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios_base::sync_with_stdio(0);\n vector<int>p;\n vector<int>n;\n for(int i=0;i<nums.size();i++){\n if(nums[i]<0) n.push_back(nums[i]);\n else if(nums[i]>0) p.push_back(nums[i]); \n } \n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution{\npublic:\n vector<int> rearrangeArray(vector<int>& nums){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n int itr = nums.size()/2;\n vector<int> p_vec, n_vec;\n for(int &num:nums){\n if(num<0){\n n_vec.push_back(num);...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n vector<int>vcp,vcn;\n int n=nums.size();\n for(int i=0;i<nums.size();i++){\n if(nums[i]<0)\n vcn.push_back(nums[i]);\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> pos;\n vector<int> neg;\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] > 0) {\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> pos;\n vector<int> neg;\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n\n // Separate the positive and negative numbers\n for (int i = 0; i < nums...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> vp;\n vector<int> vn;\n for( auto x: nums){\n if(x>0) vp.push_back(x);\n else vn.push_back(x);\n }\n for(int i=0;i<n/2 ;i++){\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>positive;\n vector<int>negative;\n for(int i =0; i< nums.size(); i++)\n {\n if(nums[i]>0)\n {\n positive.push_back(nums[i]);\n }\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> positive;\n vector<int> negative;\n for(int i=0; i<nums.size(); i++){\n if(nums[i] >= 0)\n positive.push_back(nums[i]);\n else\n negative.pus...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>pos;\n vector<int>neg;\n for(int x = 0; x < nums.size(); x++)\n {\n if(nums[x] > 0)\n {\n pos.push_back(nums[x]);\n }\n else\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> positive;\n vector<int> negative;\n for(int i=0; i<nums.size(); i++){\n if(nums[i] >= 0)\n positive.push_back(nums[i]);\n else\n negative.pus...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
2
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> pos;\n vector<int> neg;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]<0)\n {\n neg.push_back(nums[i]);\n }\n else{\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& a) {\n\n int n = a.size();\n queue<int> pos, neg; // max 2 element will there at anytime in these queues\n queue<int> posIdx, negIdx; // max 2 element will there at anytime in these queues\n // Space Complexi...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& a) {\n\n int n = a.size();\n queue<int> pos, neg; // max 2 element will there at anytime in these queues\n queue<int> posIdx, negIdx; // max 2 element will there at anytime in these queues\n // Space Complexi...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> v;\n int j= 0;\n vector<int> negative;\n for(int i = 0;i<nums.size();i++){\n if(nums[i]<0) negative.push_back(nums[i]);\n }\n \n for(int i = 0;i<nums.size();i...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios_base::sync_with_stdio(0); cin.tie(0);\n queue<int> q1 , q2;\n vector<int> ans;\n for(auto &x: nums){\n if(x>0) q1.push(x);\n if(x<0) q2.push(x);\n }\n ans.pus...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>neg;\n int k=0;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]<0)\n {\n neg.push_back(nums[i]);\n k++;\n }\n }\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> pos;\n vector<int> neg;\n int n = nums.size();\n vector<int> res(n);\n for(const int& it : nums){\n if(it >= 0)\n pos.push_back(it);\n else\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector <int> ans;\n stack <int> pos;\n stack <int> neg;\n for(int i= nums.size()-1; i>=0; --i) {\n if(nums[i]>0) pos.push(nums[i]);\n else neg.push(nums[i]);\n }\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector <int> ans;\n stack <int> pos;\n stack <int> neg;\n for(int i= nums.size()-1; i>=0; --i) {\n if(nums[i]>0) pos.push(nums[i]);\n else neg.push(nums[i]);\n }\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n queue<int> positives;\n queue<int> negatives;\n \n \n for (int num : nums) {\n if (num > 0) {\n positives.push(num); \n } else {\n negatives.push(num); \n }\n }\n \n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector <int> ans;\n stack <int> pos;\n stack <int> neg;\n for(int i= nums.size()-1; i>=0; --i) {\n if(nums[i]>0) pos.push(nums[i]);\n else neg.push(nums[i]);\n }\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>v;\n vector<int>n;\n int pos = 1;\n for(int i = 0; i < nums.size(); i++)\n {\n if(nums[i] > 0)\n {\n v.push_back(nums[i]);\n }\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>p,n;\n for(int x : nums){\n if(x>0) p.push_back(x);\n else n.push_back(x);\n }\n int pos=1;\n for(int x : n){\n p.insert(p.begin()+pos,x);\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> p, n;\n for(auto i:nums)\n if(i>=0)\n p.push_back(i);\n else\n n.push_back(i);\n int idx=1;\n for(auto i:n){\n p.insert(p.b...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "#include <chrono>\n\nauto init = []()\n{\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\n\nclass Solution\n{\npublic:\n vector<int> rearrangeArray(vector<int> &nums)\n {\n queue<int> pos, neg;\n for (int i = 0; i < nums.size(); i++)\n {\...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "#include <chrono>\n\nauto init = []()\n{\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\n\nclass Solution\n{\npublic:\n vector<int> rearrangeArray(vector<int> &nums)\n {\n queue<int> pos, neg;\n for (int i = 0; i < nums.size(); i++)\n {\...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> v1;\n vector<int> v2;\n\n for(int i = 0;i<nums.size();++i){\n\n if(nums[i]<0) v1.push_back(nums[i]);\n else v2.push_back(nums[i]);\n\n }\n\n vector<int> ans(...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n = nums.size();\n vector<int> neg;\n vector<int> pos;\n vector<int> ans(n);\n for(int i = 0; i < n; i++){\n if(nums[i] > 0){\n pos.push_back(nums[i]);\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n \n vector<int> pos;\n vector<int> neg;\n\n for(int i=0 ; i<nums.size() ; i++){\n\n if(nums[i]<0){\n neg.push_back(nums[i]);\n }\n else{\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n \n vector<int> pos;\n vector<int> neg;\n\n for(int i=0 ; i<nums.size() ; i++){\n\n if(nums[i]<0){\n neg.push_back(nums[i]);\n }\n else{\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\n\n void merge(vector<int> &nums , vector<int> pos , vector<int> neg){\n int l1 = 0 ;\n int l2 = 0 ;\n\n bool val = false ;\n int i = 0 ;\n while(i<nums.size()){\n if(!val){\n nums[i] = pos[l1] ;\n i++ ;\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> ans(n);\n int pos=0, neg=1;\n // equal no.of pos and neg\n // for(int i=0;i<n;i++)\n // {\n // if(nums[i]>0)\n // {\n // ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n vector<int>res1;\n vector<int>res2;\n vector<int>res;\n for(auto num:nums){\n if(num<0)res1.push_back(num);\n else res2.push_back(num...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n // brute force\n\n vector<int> p;\n vector<int> n;\n\n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] < 0) {\n n.push_back(nums[i]);\n } else {\n ...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int> x={};\n vector<int> y={};\n for (int i=0;i<nums.size();i++){\n if (nums[i]>0){\n x.push_back(nums[i]);\n }\n else {\n y.push_back(...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n vector<int>pos;\n vector<int>neg;\n int n=nums.size();\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]>0)\n {\n pos.push_back(nums[i]);\n }\n...
2,271
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should return the array of nums such that the the array follows the given conditions:</p> <ol> <li>Every <strong>c...
3
{ "code": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n int n=nums.size();\n vector<int> p;\n vector<int> ne;\n for(int i=0;i<n;i++){\n if(nums[i]<0){\n ne.push_back(nums[i]);\n }\n else{\n p...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
0
{ "code": "#pragma GCC optimize(\"Ofast\", \"inline\", \"unroll-loops\")\nclass Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n=pref.size();\n if(n==1) return pref;\n vector<int> arr(n);\n arr[0] = pref[0];\n for(int i=1;i<n;i++){\n arr[i] = pr...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n=pref.size();\n vector<int>result(n);\n result[0] = pref[0];\n for(int i=1;i<n;i++){\n result[i] = pref[i-1]^pref[i];\n } \n return result;\n }\n};\n/*\npref array se result nikalna ...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n = pref.size(), xxor = pref[0];\n vector<int> ans(n, pref[0]);\n for(int i = 1; i < n; ++i){\n ans[i] = pref[i]^xxor;\n xxor ^= ans[i];\n }\n return ans;\n }\n};", ...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int size = pref.size();\n vector<int> ans(size);\n\n ans[0] = pref[0];\n for (int i = 1; i < size; i++) {\n ans[i] = pref[i] ^ pref[i - 1];\n }\n return ans;\n }\n};", "...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int> ans(pref.size(), 0);\n\n for (int i = 0; i < pref.size(); i++) {\n if (i == 0)\n ans[i] = pref[i];\n else {\n ans[i] = pref[i - 1] ^ pref[i];\n ...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n = pref.size();\n if(pref.size() == 1) return pref;\n vector<int> ans(n);\n ans[0] = pref[0];\n for(int i=1;i<n;i++)\n {\n ans[i] = pref[i-1]^pref[i];\n }\n re...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
0
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int xr=pref[0];\n vector<int> a;\n int s=0;\n \n \n vector<int> ps(pref.size(),xr); \n for(int i=1;i<pref.size();i++){ \n ps[i]=xr^pref[i];\n xr=pref[i]...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
1
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n = pref.size();\n for (int i = n - 1; i > 0; --i) {\n pref[i] ^= pref[i - 1];\n }\n return pref;\n }\n};\n ", "memory": "78700" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
1
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int temp = -1;\n int n = pref.size();\n vector<int> ans;\n for (int i = 0; i < n; i++) {\n if (i == 0) {\n temp = pref[i];\n pref[i]=temp;\n\n cont...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int> res(pref.size());\n res[0] = pref[0];\n for (int i = 1; i < pref.size(); i++) {\n res[i] = pref[i] ^ pref[i - 1];\n }\n return res;\n }\n};", "memory": "78800" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n = pref.size(), curr = pref[0];\n vector<int> ans(n);\n ans[0] = pref[0];\n\n for(int i = 1; i < n; i++){\n int temp = pref[i] ^ curr;\n ans[i] = temp;\n curr = curr...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n=pref.size();\n vector<int>ans(n);\n vector<int>sum(n);\n ans[0]=pref[0];\n sum[0]=pref[0];\n for(int i=1;i<n;i++)\n {\n ans[i]=sum[i-1]^pref[i];\n sum[i]=...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int a =0 ; vector<int>arr(pref.size()) ; \n arr[0]= pref[0] ; \n a= a^ pref[0] ; \n for(int i=1;i<pref.size();i++){\n \n arr[i]=pref[i]^pref[i-1] ;\n\n } vec...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n ios::sync_with_stdio(0);\n vector<int> v;\n if(pref.empty())\n return {};\n \n v.push_back(pref[0]);\n for(int i=1; i<pref.size(); i++){\n v.push_back(pref[i-1]^pref[i...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int>arr;\n arr.push_back(pref[0]);\n for(int i=1;i<pref.size();i++){\n arr.push_back(pref[i-1]^pref[i]);\n }\n return arr;\n }\n};", "memory": "81700" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n int n = pref.size();\n\n vector<int> res;\n res.push_back(pref[0]);\n\n for (int i=1; i<n; i++)\n res.push_back(pref[i] ^ pref[i-1]);\n\n return res;\n }\n};", "memory": "81800" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int> arr;\n arr.push_back(pref[0]);\n for(int i=1;i<pref.size();i++){\n arr.push_back(pref[i]^pref[i-1]);\n }\n\n return arr;\n }\n};", "memory": "81800" }
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int>ans;\n ans.push_back(pref[0]);\n for(int i=0;i<pref.size()-1;i++){\n int bit=(pref[i]^pref[i+1]);\n ans.push_back(bit);\n }\n return ans;\n }\n};", "memory": ...
2,519
<p>You are given an <strong>integer</strong> array <code>pref</code> of size <code>n</code>. Find and return <em>the array </em><code>arr</code><em> of size </em><code>n</code><em> that satisfies</em>:</p> <ul> <li><code>pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]</code>.</li> </ul> <p>Note that <code>^</code> denotes ...
2
{ "code": "class Solution {\npublic:\n vector<int> findArray(vector<int>& pref) {\n vector<int> ret;\n ret.push_back(pref[0]);\n\n for (int i = 1; i < pref.size(); i++) {\n ret.push_back(pref[i-1] ^ pref[i]);\n }\n\n return ret;\n }\n};", "memory": "81900" }
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
0
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n for(int i = 1; i < size(stones); ++i) stones[i] += stones[i-1];\n stones.insert(stones.begin(), 0);\n\n int n = size(stones);\n int dp[n-1];\n memset(dp, 0, sizeof(dp));\n\n // base case:\n ...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
0
{ "code": "#include <vector>\n\nclass Solution {\n public:\n int stoneGameVII(std::vector<int>& stones) {\n for (std::size_t i = 1; i < stones.size(); ++i) {\n stones[i] += stones[i - 1];\n }\n\n std::vector<int> diffs(stones.size(), 0);\n for (std::size_t i = 1; i < diffs.size(); ++i) {\n for ...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
0
{ "code": "class Solution {\npublic:\n int n;\n int dp[1001][1001][2];\n vector<int> pre;\n int solve(int i, int j, bool flag, vector<int>& stones)\n {\n if(i>j)\n {\n return 0;\n }\n\n if(dp[i][j][flag]!=-1)\n {\n return dp[i][j][flag];\n ...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
0
{ "code": "class Solution {\npublic:\n \n int dp[1002][1002][2] ; \n\n int rec(int l , int r , int ch , vector <int> &arr){\n if(l >= r){\n return 0 ; \n }\n if(dp[l][r][ch] != -1){\n return dp[l][r][ch] ; \n }\n int ans = -1e9 ; \n int ansx = 1...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
1
{ "code": "using ll = long long;\nusing pl = pair<ll, ll>;\nusing vl = vector<ll>;\nusing vvl = vector<vector<ll>>;\nusing vll = vector<pair<ll, ll>>;\n\n#define all(a) a.begin(), a.end()\n#define rall(a) a.rbegin(), a.rend()\n\n\nclass Solution {\npublic:\n\nint stoneGameVII(vector<int>& a) {\n\n ll n = a.size();...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
1
{ "code": "class Solution {\n static int solve(const std::vector<int>& stones, const int N, std::vector<int>& cache, const int l, const int r) {\n // ok, so the way the DP works, is that we are always entering as alice.\n const int SN = r - l + 1;\n\n // We pick the last and it's done\n ...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
1
{ "code": "inline const auto optimize = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\n static constexpr int solve(const std::vector<int>& stones, const int N,\n std::vector<int>& cache, const int ...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
1
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>> dp(n, vector<int>(n, 0));\n for(int len = 1;len<n;len++)\n {\n for(int i=0;i+len<n;i++)\n {\n int j = i+len;\n ...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
1
{ "code": "class Solution {\npublic:\n int solve(vector<int>& stones, int start, int end, int totalSum,vector<vector<int>>&dp) {\n if (start >= end || totalSum<=0) return 0; \n if(dp[start][end]!=-1)return dp[start][end]; \n int scoreIfStart = totalSum - stones[start] - solve(stones, start + 1...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int helper(int i,int j,vector<int>& stones,int sum){\n \n if(i>j)return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n int left=sum-stones[i] -helper(i+1,j,stones,sum-stones[i]);\n int right=sum-stones[j]-helper(...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int helper(int i,int j,vector<int>& stones,int sum){\n \n if(i>j)return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n int left=sum-stones[i] -helper(i+1,j,stones,sum-stones[i]);\n int right=sum-stones[j]-helper(...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>> dp_alice ;\n vector<vector<int>> dp_bob ;\n\n int minimise(vector<int>& stones, int l, int r, int sum) {\n if ( l >= r ) return 0 ;\n if ( dp_bob[l][r] != -1 ) return dp_bob[l][r] ;\n if ( r-l == 1 ) return max(stones[l],stones...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>> array(n, vector<int>(n, 0));\n vector<vector<int>> sum(n, vector<int>(n, 0));\n for (int i=0; i<n; i++) {\n //array[i][i] = stones[i];\n su...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n pair<int,int> f(int i,int j,vector<vector<pair<int,int>>>&dp,vector<int>&pre){\n if(i>=j)return {0,0};\n if(dp[i][j].first!=-1)return dp[i][j];\n int first=INT_MIN;\n int second;\n first=f(i+1,j,dp,pre).second+pre[j]-pre[i];\n second=...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n const int n = stones.size();\n\n vector<vector<int>> dp(n, vector<int>(n, 0));\n vector<vector<int>> sums(n, vector<int>(n, 0));\n\n int sum = 0;\n for (int i = 0; i < n; ++i) {\n for (int...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "#include <vector>\n#include <algorithm>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<vector<int>> sum;\n vector<vector<int>> memo;\n\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n sum.resize(n, vector<int>(n, 0));\n memo.resize(n, vector<in...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "typedef long long int ll;\n\nclass Solution {\npublic:\n int stoneGameVII(vector<int>& arr) {\n \n ll n = arr.size();\n\n const ll INF = 1e15; \n\n vector<ll> csum(n, 0);\n\n vector< vector<ll> > dp( n, vector<ll>( n, INF));\n\n for( ll i=0, tot=0; i<n; i++) {\n...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n vector<vector<int>> sum;\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n for(int i = 0; i < n; i++){\n dp.push_back(vector<int>(n, INT_MAX));\n sum.push_back(vector<int>(n));\n }\n...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "// Idea: DP:\n// let f[p][i][j] be the result of subproblem A[i...j] when the active player is\n// p, p in [0,1].\n// then the DP/Bellman equation is:\n// f[p][i][j] = max of: sum(A[i+1 ... j]) - f[1-p][i+1][j],\n// sum(A[i ... j-1]) - f[1-p][i][j-1].\nclass Solution {\n constexpr s...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "// Idea: DP:\n// let f[p][i][j] be the result of subproblem A[i...j] when the active player is\n// p, p in [0,1].\n// then the DP/Bellman equation is:\n// f[p][i][j] = max of: sum(A[i+1 ... j]) - f[1-p][i+1][j],\n// sum(A[i ... j-1]) - f[1-p][i][j-1].\n// complexity: O(2 * 2 n^2) = O(n...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n int dp(vector<int>& stones,int i,int j,int isbob,vector<vector<vector<int>>>&memo,int sum){\n if(i>j){\n return 0;\n }\n if(memo[isbob][i][j]!=-1){\n return memo[isbob][i][j];\n }\n int ans=(isbob?INT_MAX:INT_MIN);\n ...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n int n;\n int solve(vector<int>& stones,int start,int end,int player,vector<int>&presum,vector<vector<vector<int>>>&dp){\n if(start>=end)return 0;\n if(dp[player][start][end]!=-1)return dp[player][start][end];\n if(player==0){\n int p1=solve(st...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int> &a, int i, int j, int sum, bool isAliceTurn, vector<vector<int>> &dp) {\n if (i > j) {\n return 0;\n }\n if (dp[i][j] != -1) {\n return dp[i][j];\n }\n if (isAliceTurn) {\n int op1 = sol...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int solve(vector<int>& stones, int i, int j, int sum){\n if(i>=j){\n return 0;\n }\n if(sum<=0){\n return 0;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n int fron...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n vector<vector<int>> dp(1005, vector<int>(1005, 0));\n for(int i = stones.size() - 1; i >= 0; i--) {\n int sum = stones[i];\n for(int j = i + 1; j < stones.size(); j++) {\n sum += ston...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\n unordered_map<int, int> dp;\n\n int solver(int left, int right, bool player, int total, vector<int>& stones) {\n if (left == right) {\n return 0;\n } else if (dp.contains(left * 10000 + right * 10 + player)) {\n return dp[left * 10000 + right * 1...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\n unordered_map<int, int> dp;\n\n int solver(int left, int right, int total, vector<int>& stones) {\n if (left == right) {\n return 0;\n } else if (dp.contains(left * 1000 + right)) {\n return dp[left * 1000 + right];\n }\n\n int take...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n pair<int, int> dynamicthink(vector<vector<vector<pair<int,int>>>>& dp, int curr, int start, int end, int total, vector<int>& stones) {\n if (start >= end) {\n return {0, 0};\n }\n if (dp[start][end][curr].first != -1) {\n return dp[s...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n int stoneGameVII(vector<int>& stones) {\n int n = stones.size();\n vector<vector<vector<pair<int,int>>>> dp(n, vector<vector<pair<int,int>>>(n, vector<pair<int,int>>(2, {0, 0})));\n vector<int> prefixSum(n + 1, 0);\n \n // Compute prefix sum...
1,808
<p>Alice and Bob take turns playing a game, with <strong>Alice starting first</strong>.</p> <p>There are <code>n</code> stones arranged in a row. On each player&#39;s turn, they can <strong>remove</strong> either the leftmost stone or the rightmost stone from the row and receive points equal to the <strong>sum</strong...
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<pair<int, int>>>> memo; \n\n int getSum(vector<int>& sums, int l, int r) {\n return sums[r] - ((l > 0) ? sums[l - 1] : 0);\n };\n\n pair<int, int> dfs(int l, int r, int turn, pair<int, int> score, vector<int>& sums) { \n if (l == r)...