id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n vector<int> pos;\n vector<int> neg;\n\n for(int num : nums){\n num-=1;\n if(num>=0) pos.push_back(num);\n else neg.push_back(num);\n }\n\n sort(pos.begin(),pos.end());\n ... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class largest{\n public:\n int i,j; \n largest(){\n this->i = i; \n this->j = j; \n }\n}; \n\nclass Solution {\npublic:\n largest is_match(vector<int> vc){\n largest l = largest(); \n int mx = INT_MIN; \n int n = vc.size(); \n for(int i=0; i<n-1; i++... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<pair<int,int>>q;\n for(int i=0;i<nums.size();i++){\n q.push({nums[i],i});\n // if(q.size()>2){\n // q.pop\n // }\n }\n int place=0;\n if(q.s... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n make_heap(nums.begin(), nums.end());\n pop_heap(nums.begin(), nums.end()); \n int max1 = nums.back(); \n nums.pop_back();\n pop_heap(nums.begin(), nums.end()); \n int max2 = nums.back(); \n ... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n \n priority_queue<pair<int,int>>q;\n for(int i=0;i<nums.size();i++){\n q.push({nums[i],i});\n // if(q.size()>... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n std::make_heap(nums.begin(), nums.end());\n\n std::pop_heap(nums.begin(), nums.end());\n int x = nums.back();\n nums.pop_back();\n\n std::pop_heap(nums.begin(), nums.end());\n int y = nums.back();... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int k=2;\n int n=nums.size();\n priority_queue<int,vector<int>,greater<int>>minHeap;\n for(int i=0;i<n;i++){\n minHeap.push(nums[i]);\n if(minHeap.size()>k){\n minHeap.pop();\n }\n ... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<int > p(nums.begin(),nums.end());\n int x = p.top()-1;\n p.pop();\n int y = p.top()-1;\n return x*y;\n }\n};",
"memory": "13700"
} |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<int,vector<int>,greater<int>>mp;\n vector<int>ans;\n for(int i=0;i<nums.size();i++){\n mp.push(nums[i]);\n if(mp.size()>2){\n mp.pop(); }\n \n }\n ... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<int> pq;\n for(int num:nums){\n pq.push(num);\n }\n int firstMax=pq.top();\n pq.pop();\n int secondMax=pq.top();\n return ((firstMax-1)*(secondMax-1));\n }\n};"... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<int> pq;\n for(int i=0;i<nums.size();i++){\n pq.push(nums[i]);\n }\n int a=pq.top()-1;\n pq.pop();\n int b=pq.top()-1;\n return a*b;\n }\n};",
"memory": "1400... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<int> pq;\n for(int i=0;i<nums.size();i++){\n pq.push(nums[i]);\n }\n int a=pq.top()-1;\n pq.pop();\n int b=pq.top()-1;\n return a*b;\n }\n};",
"memory": "1400... |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<int> p;\n for(int n : nums){\n p.push(n);\n }\n int x = p.top()-1;\n p.pop();\n int y = p.top()-1;\n return x*y;\n }\n};",
"memory": "14100"
} |
1,574 | Given the array of integers <code>nums</code>, you will choose two different indices <code>i</code> and <code>j</code> of that array. <em>Return the maximum value of</em> <code>(nums[i]-1)*(nums[j]-1)</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,5,2... | 3 | {
"code": "class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<int>pq;\n for(int &it : nums){\n pq.push(it);\n }\n int x = pq.top();\n pq.pop();\n int y = pq.top();\n pq.pop();\n\n return ((x-1) * (y-1));\n \n ... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": "class Solution {\n public:\n int maxArea(int h, int w, vector<int>& horizontalCuts,\n vector<int>& verticalCuts) {\n constexpr int kMod = 1'000'000'007;\n ranges::sort(horizontalCuts);\n ranges::sort(verticalCuts);\n\n // the maximum gap of each direction\n int maxGapX = max(ve... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": "class Solution {\npublic:\n int maxArea(int h, int w, std::vector<int>& horizontalCuts, std::vector<int>& verticalCuts) {\n return (long long int)getMaxCut(h, horizontalCuts) * getMaxCut(w, verticalCuts) % 1000000007;\n }\n\n int getMaxCut(int w, std::vector<int>& cuts) {\n std::rang... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": "class Solution {\npublic:\n int maxArea(int h, int w, vector<int>& hCuts, vector<int>& vCuts) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n\n sort(hCuts.begin() , hCuts.end());\n sort(vCuts.begin() , vCuts.end());\n int maxH = max(hCuts[0] , h - hC... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": "class Solution {\npublic:\n int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {\n ios_base::sync_with_stdio(false); \n cin.tie(NULL); \n sort(horizontalCuts.begin(),horizontalCuts.end());\n horizontalCuts.push_back(h);\n ios_base::sync_w... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": "#define MOD 1000000007\nclass Solution {\npublic:\n Solution(){\n ios_base :: sync_with_stdio(false);\n }\n int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {\n horizontalCuts.push_back(0);\n horizontalCuts.push_back(h);\n verticalCuts.p... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": "class Solution {\npublic:\n int mod=1e9+7;\n int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {\n // order doesnot matter true na? \n sort(horizontalCuts.begin(),horizontalCuts.end());\n int maxi=INT_MIN;\n int start=0;\n for(auto it... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": " typedef unsigned long long ull;\nclass Solution {\npublic:\n \n\n int maxArea(int h1, int w, vector<int>& h, vector<int>& v) {\n sort(h.begin(),h.end());\n sort(v.begin(),v.end());\n int x=0;\n int y=0;\n int M=1e9+7;\n int tp=0;\n... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": "class Solution {\npublic:\n int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {\n // 5: 0,1,2,3,4,5\n // 4: 0,1,2,3,4\n // horizontalCuts = [1,2,4]\n // verticalCuts = [1,3]\n sort(horizontalCuts.begin(), horizontalCuts.end());\n ... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": "class Solution {\npublic:\n int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {\n int maxiRow=INT_MIN;\n int mod = 1000000007;\n sort(begin(horizontalCuts),end(horizontalCuts));\n sort(begin(verticalCuts),end(verticalCuts));\n int n=hor... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": "class Solution {\npublic:\ntypedef long long ll;\n int maxArea(int h, int w, vector<int>& H, vector<int>& V) {\n H.push_back(h);\n V.push_back(w);\n ll maxH=0, maxV=0;\n sort(H.begin(), H.end());\n sort(V.begin(), V.end());\n for(int i=0;i<H.size();i++){\n ... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 0 | {
"code": "class Solution {\npublic:\n int maxArea(int h, int w, vector<int>& hCuts, vector<int>& vCuts) \n {\n sort(hCuts.begin(),hCuts.end());\n sort(vCuts.begin(),vCuts.end());\n int mxH = 0, mxV = 0;\n\n for(int i=0;i<=hCuts.size();i++)\n {\n if(i==0)\n ... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 2 | {
"code": "class Solution {\npublic:\n int maxArea(int h, int w, vector<int>& a, vector<int>& b) {\n //1000000007\n a.push_back(0); b.push_back(0);\n a.push_back(h); b.push_back(w);\n sort(a.begin(), a.end()); sort(b.begin(), b.end());\n long long int maxA=0; long long maxB=0;\n ... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 2 | {
"code": "class Solution {\npublic:\n int maxArea(int h1, int w, vector<int>& h, vector<int>& v) {\n sort(h.begin(),h.end());\n sort(v.begin(),v.end());\n int x=h[0];\n int y=v[0];\n int mod=1e9+7;\n for(int i=1;i<h.size();i++){\n ... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 3 | {
"code": "class Solution {\npublic:\n int maxArea(int h, int w, vector<int>& horizontalCuts, vector<int>& verticalCuts) {\n horizontalCuts.push_back(h);\n horizontalCuts.push_back(0);\n\n const int mod = 1e9 + 7;\n\n verticalCuts.push_back(w);\n verticalCuts.push_back(0);\n\n ... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n#define MOD_NUM 1000000007\nclass Solution {\npublic:\n int maxArea(int h, int w, vector<int> &horizontalCuts,\n vector<int> &verticalCuts) {\n\t\thorizontalCuts.push_back(h);\n\t\tverticalCuts.push_back(w);\n\t\tsort(horizontalCuts.begin(), hor... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 3 | {
"code": "class Solution {\npublic:\n \n \nint maxDiff(vector<int> v, int length){\n sort(v.begin(),v.end());\n int max_diff=v[0];\n for(int i=1;i<v.size();i++){\n max_diff=max(max_diff,v[i]-v[i-1]);\n }\n \n max_diff=max(max_diff,length-v.back()... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 3 | {
"code": "class Solution {\npublic:\n int m=1e9+7;\n int find(int l,vector<int> v)\n {\n int n=v.size();\n int ans=v[0];\n ans=max(ans,l-v[n-1]);\n for(int i=0;i<n-1;i++)\n ans=max(ans,v[i+1]-v[i]);\n return ans%m;\n }\n int maxArea(int h, int w, vector<int>& ... |
1,575 | <p>You are given a rectangular cake of size <code>h x w</code> and two arrays of integers <code>horizontalCuts</code> and <code>verticalCuts</code> where:</p>
<ul>
<li><code>horizontalCuts[i]</code> is the distance from the top of the rectangular cake to the <code>i<sup>th</sup></code> horizontal cut and similarly, a... | 3 | {
"code": "class Solution {\npublic:\n int maxdiff(vector<int> v,int length){\n sort(v.begin(),v.end());\n int max_diff=v[0];\n for(int i=1;i<v.size();i++){\n max_diff=max(max_diff,v[i]-v[i-1]);\n }\n max_diff=max(max_diff,length-v.back());\n ... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 0 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n int n = arr.size();\n int c = 0;\n for(int i = 0 ; i < n ; i++) {\n if(!arr[i]) c++;\n }\n\n int j = n + c - 1;\n int i = n - 1;\n\n while(i >= 0) {\n if(!arr[i]) ... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 0 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n int n = arr.size();\n int zerosToDuplicate = 0;\n int i = 0;\n\n // Step 1: Find the number of zeros to duplicate.\n for (i = 0; i < n; ++i) {\n if (arr[i] == 0) {\n zerosTo... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 0 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n int i = 0;\n int n = arr.size();\n \n while (i < n) {\n if (arr[i] == 0) {\n for (int j = n - 1; j > i; j--) {\n arr[j] = arr[j - 1];\n }\n ... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 0 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n \n int len = arr.size();\n int x = len;\n int y = len;\n \n for(int t : arr)\n if(t == 0) y++;\n \n while(x && y) {\n \n x--; y--;\n \... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 0 | {
"code": "class Solution {\npublic:\n void insert0(vector<int>& a,int n,int i) {\n for(int j = n-1;j>i;j--) {\n a[j] = a[j-1];\n }\n a[i+1] = 0;\n}\n void duplicateZeros(vector<int>& a) {\n int n = a.size();\n for(int i = 0;i<n;i++) {\n if(a[i] ==0 && i+1 <n) {\n ins... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 0 | {
"code": "class Solution {\npublic:\nvoid moveRightAndInsert(std::vector<int>& arr, int index, int value) {\n // Shift elements to the right starting from 'index'\n for (int i = arr.size() - 1; i > index; --i) {\n arr[i] = arr[i - 1];\n }\n // Insert the value at the specified index\n arr[index... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 0 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n int newSize = arr.size() + std::count(arr.begin(), arr.end(), 0);\n for (int i = arr.size()-1, j = newSize-1; i >= 0; i--)\n {\n if (j < arr.size())\n {\n arr[j] = arr[i];\n ... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 1 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n int n = arr.size();\n int zeroCount = 0;\n\n for (int i = 0; i < n; ++i) {\n if (arr[i] == 0) zeroCount++; \n }\n\n int i = n - 1;\n int j = n - 1 + zeroCount;\n\n while (i >... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 1 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n for (int i = 0; i < arr.size(); ++i) {\n if (arr.at(i) == 0) {\n arr.insert(arr.begin()+i, 0);\n arr.pop_back();\n i++;\n }\n }\n }\n};",
"memory": ... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 2 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n int n=arr.size();\n vector<int> v(arr);\n arr.clear();\n for(int i=0;i<v.size();i++){\n if(arr.size()==n){\n break;\n }\n if(v[i]==0){\n arr.pu... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 2 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n\n int n=arr.size();\n vector<int>a;\n for(int i=0; i<n;i++){\n if(arr[i]==0){\n a.push_back(0);\n a.push_back(0);\n }\n else{\n a.push_... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 2 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n int n=arr.size();\n vector<int> v;\n // arr.clear();\n // for(int i=0;i<v.size();i++){\n // if(arr.size()==n){\n // break;\n // }\n // if(v[i]==0){\n /... |
1,168 | <p>Given a fixed-length integer array <code>arr</code>, duplicate each occurrence of zero, shifting the remaining elements to the right.</p>
<p><strong>Note</strong> that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.... | 2 | {
"code": "class Solution {\npublic:\n void duplicateZeros(vector<int>& arr) {\n vector<int>ans;\n int a=0;\n for(int i=0;a<arr.size();++i){\n if(arr[i]!=0){\n ans.push_back(arr[i]);\n ++a;\n }\n else if(arr[i]==0&& a+2<=arr.size()... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "class Solution {\n static constexpr int primes[]{\n 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,\n 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,\n 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,\n 173, 179, 181, 191, 193,... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "class Solution {\npublic:\n // vector<int> adj[100005];\n // int vis[100005];\n // void dfs(int u , int p){\n // vis[u] = 1;\n // for(int v : adj[u]){\n // if(v == p)continue;\n // if(vis[v] == 0)dfs(v , u);\n // }\n // }\n\n // // linear sieve\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "class DSU {\n public:\n unordered_map<int,int>parent , rank;\n\n DSU(){\n }\n\n void MakeSet(int x){\n parent[x] = x;\n rank[x] = 0;\n }\n\n int Find(int x){\n return parent[x] = parent[x] == x ? x : Find(parent[x]);\n }\n\n bool Union(int x , int y){\n\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "class DSU {\n public:\n unordered_map<int,int>parent , rank;\n\n DSU(){\n }\n\n void MakeSet(int x){\n parent[x] = x;\n rank[x] = 0;\n }\n\n int Find(int x){\n return parent[x] = parent[x] == x ? x : Find(parent[x]);\n }\n\n bool Union(int x , int y){\n\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "class Solution {\npublic:\n int par[100001] ;\n int siz[100001] ;\n void make(int i){\n par[i] = i ;\n siz[i] = 1 ;\n }\n int find(int i){\n if(par[i]==i)return i ;\n return par[i] = find(par[i]) ;\n }\n void Union(int i, int j){\n i = find(i) ;\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "class DSU {\n vector<int> e;\npublic:\n DSU(int n) { e = vector<int> (n, -1); }\n int leader(int x) { return e[x] < 0 ? x : e[x] = leader(e[x]); }\n void merge(int x, int y) {\n x = leader(x);\n y = leader(y);\n if(x == y) return;\n if(e[x] > e[y]) swap(x, y);\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "class Solution {\nprivate:\n vector<int> parent;\n vector<int> rank;\n\n int find(int x) {\n if (parent[x] != x) {\n parent[x] = find(parent[x]);\n }\n return parent[x];\n }\n\n void unite(int x, int y) {\n x = find(x);\n y = find(y);\n if... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "class Solution {\n // Function to find the root of a set using path compression\n int getf(vector<int> &f, int x) {\n return f[x] == x ? x : (f[x] = getf(f, f[x]));\n }\n \n // Function to merge two sets and update the frequency\n void merge(vector<int> &f, vector<int> &num, int x,... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "class DisjointSet {\n vector<int> parent, rank;\n\npublic:\n DisjointSet(int n) {\n parent.resize(n + 1);\n rank.resize(n + 1, 0);\n\n for(int i = 0; i <= n; i++) {\n parent[i] = i;\n }\n }\n\n int findUpar(int node) {\n if(node != parent[node]) {\n... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "class Solution {\n int parent[110001];\npublic:\n bool canTraverseAllPairs(vector<int>& nums) {\n int n = nums.size();\n int max_num = *max_element(nums.begin(), nums.end());\n vector<int> primes = get_primes(max_num);\n memset(parent, -1, sizeof(parent));\n\n unord... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "struct UnionFind {\n vector<int> parent, rank;\n UnionFind(int n) {\n parent.resize(n); rank.resize(n, 0);\n iota(parent.begin(), parent.end(), 0);\n }\n int find(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); }\n bool isConnected(int x, int y) { return find(... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 0 | {
"code": "struct UnionFind {\n vector<int> parent, rank;\n UnionFind(int n) {\n parent.resize(n); rank.resize(n, 0);\n iota(parent.begin(), parent.end(), 0);\n }\n int find(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); }\n bool isConnected(int x, int y) { return find(... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "// #pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass UnionFind { \npublic:\n vector<int> root, size;\n UnionFind(int n) : root(n), size(n) {\n size.assign(n, 1);\n iota(root.begin(), root.end(), 0);\n }\n\n int Find(int x) {\n if (x == root[x]) return x;\n els... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "///////////////////////////////////////////////////\n\n#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define INT(x) int x; cin >> x;\n#define VECTOR(v) vector<int> v(n); for (int i = 0; i < n; i ++) { cin >> v[i]; }\n#define EXISTS(s, x) s.find(x) != s.end()\n#define ALL(v) v.begin(),v.end()\n#define... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Disjoint{\n vector<int>par,size,rank;\npublic:\n Disjoint(int n)\n {\n par.resize(n+1);\n size.resize(n+1,1);\n rank.resize(n+1,0);\n for(int i=0 ; i<=n ; i++)par[i]=i;\n }\n int findPar(int a) \n {\n if(a==par[a])return a;\n return par[a]=... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\npublic:\n bool canTraverseAllPairs(vector<int>& nums) {\n int n = nums.size();\n std::vector<int> p(n);\n std::vector<int> sz(n, 1);\n std::iota(p.begin(), p.end(), 0);\n const auto find = [&](int x){\n while (p[x] != x) {\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> rank;\n\n int find(int x) {\n if (x == parent[x]) {\n return x;\n }\n return parent[x] = find(parent[x]);\n }\n\n void Union(int x, int y) {\n int x1 = find(x);\n int y1 = find(y);\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\npublic:\n vector<int>par,sz;\n int find(int x){\n if(x==par[x])return x;\n else return par[x]=find(par[x]);\n }\n void merge(int x,int y){\n x=par[x];\n y=par[y];\n if(sz[x]>sz[y]){\n sz[x]+=sz[y];\n par[y]=x;\n }... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\npublic: \n vector<int> parent;\n void Union(int u, int v) {\n u = Find(u);\n v = Find(v);\n if (u != v) {\n parent[v] = u;\n }\n }\n int Find(int u) {\n if (parent[u] == u) return u;\n return parent[u] = Find(parent[u]);\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\n class UnionFind {\n vector<int> reps, szs;\n public:\n UnionFind(int n) : reps(n), szs(n, 1) {\n iota(begin(reps), end(reps), 0);\n }\n\n int find(int i) {\n if (reps[i] != i) reps[i] = find(reps[i]);\n return reps[i];\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\npublic:\n\n struct DisjointSets {\n\n int n;\n vector<int> parent;\n vector<bool> touched;\n vector<int> rank;\n\n DisjointSets(int n) {\n this->n = n;\n parent.assign(n, 0);\n iota(parent.begin(), parent.end(), 0);\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\npublic:\n\n struct DisjointSets {\n\n int n;\n vector<int> parent;\n vector<bool> touched;\n vector<int> rank;\n\n DisjointSets(int n) {\n this->n = n;\n parent.assign(n, 0);\n iota(parent.begin(), parent.end(), 0);\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\npublic:\n int findpar(int node,unordered_map<int,int>&parent) {\n if (node == parent[node])\n return node;\n return parent[node] = findpar(parent[node],parent);\n }\n void uni(int u, int v,unordered_map<int,int>&parent,unordered_map<int,int>&rank) {\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\npublic:\n int findpar(int node,unordered_map<int,int>&parent) {\n if (node == parent[node])\n return node;\n return parent[node] = findpar(parent[node],parent);\n }\n void uni(int u, int v,unordered_map<int,int>&parent,unordered_map<int,int>&rank) {\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "const int N=1e5+7;\nvector<int>parent(N,0);\nvector<int>sz(N,0);\nclass Solution {\npublic:\n int find(int v){\n if(v==parent[v])return v;\n return parent[v]= find(parent[v]);\n }\n void union_2gr(int a,int b){\n a=find(a);\n b=find(b);\n \n if(a!=b){\n //un... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\npublic:\n map<int,int> f; //id of factors\n int m = 0; //count of factors\n\n //edges for numbers & factors\n //N: numbers, F: factors\n vector<int> N[100001], F[100001];\n\n void add_edge(int id_n, int factor)\n {\n int id_f = f[factor];\n N[id_n].push_... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\nprivate:\n int findParent(vector<int> &parent, int u){\n return parent[u]=(parent[u]==u)?u:findParent(parent, parent[u]);\n }\n void join(vector<int> &parent, vector<int> &size, int u, int v){\n int pu=findParent(parent, u), pv=findParent(parent, v);\n if(pu=... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class Solution {\npublic:\n void make(int node, vector<int>& parent, vector<int>& s){\n s[node]=1;\n parent[node]=node;\n }\n int find(int node,vector<int>& parent){\n if(node!=parent[node]){\n return parent[node]=find(parent[node],parent);\n }\n retur... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class disjointSet{\nprivate:\n int n;\n vector<int>rank;\n vector<int>parent;\npublic:\n disjointSet(int n){\n this -> n = n;\n parent.resize(n , 0);\n rank.resize(n , 1);\n for(int i = 0 ; i < n ; i++)\n parent[i] = i;\n }\n int find(int a){\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 1 | {
"code": "class DSU{\n public:\n vector<int>par,size;\n DSU(int n){\n par.resize(n+1);\n size.resize(n+1);\n for(int i=0;i<=n;i++){\n par[i] = i;\n size[i] = 1;\n }\n }\n int fpar(int node){\n if(par[node]!=node)return par[node] = fpar(p... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n bool canTraverseAllPairs(vector<int>& nums) {\n if(nums.size() == 1) return true;\n generate_primes(330);\n for(auto x:nums){\n if(x == 1) return false;\n auto factors = get_factors(x);\n connect(factors);\n }\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n bool canTraverseAllPairs(vector<int>& nums) {\n unordered_set<int> nums_set{};\n\n unordered_map<int, vector<int>> prime_to_idxs{};\n vector<vector<int>> idx_to_primes(ssize(nums));\n\n vector<bool> visited(ssize(nums), false);\n \n a... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n void bfs(int root, vector<bool> & vis, vector<vector<int>> & adj) {\n queue<int> q; \n q.push(root); \n vis[root] = true; \n\n while (!q.empty()) {\n auto u = q.front(); \n q.pop(); \n\n for (auto v : ad... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n using bitset = std::vector<std::int8_t>;\n\n template<int N>\n std::array<int, N> generatePossibleDivizors(int n) {\n n /= 2;\n auto visit = bitset(n);\n \n for (auto j = 4; j < n; j += 2) {\n visit[j] = true;\n }\n f... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class UnionFind {\npublic:\n UnionFind(int size) : parent(size), rank(size, 1) {\n for (int i = 0; i < size; ++i) {\n parent[i] = i;\n }\n }\n\n int find(int u) {\n if (parent[u] != u) {\n parent[u] = find(parent[u]);\n }\n return parent[u];... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class DSU{\nprivate:\n vector<int> parent;\n vector<int> rank;\npublic:\n DSU(int n){\n parent.resize(n,0);\n rank.resize(n,1);\n for(int i = 0;i < n;i++){\n parent[i] = i;\n }\n }\n\n int find(int x){\n if(parent[x] != x){\n return pa... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19};\nvoid generate_primes() {\n for (int i = 21; i <= 5e4; i += 2) {\n bool flag = true;\n for (const int& j : primes) {\n if (j * j > i)\n break;\n\n if (i % j == 0) {\n flag = false;\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class DisjointSet{\n public:\n vector<int>size ,parent;\n DisjointSet(int n){\n size.resize(n+1,1);\n parent.resize(n+1,0);\n for(int i=0;i<n+1;i++){\n parent[i] =i;\n }\n }\n\n int find_parent(int node){\n if(... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19};\n\n void generate_primes() {\n for (int i = 21; i <= 5e4; i += 2) {\n bool flag = true;\n for (const int& j : primes) {\n if (j * j > i)\n break;\n\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19};\n\n void generate_primes() {\n for (int i = 21; i <= 5e4; i += 2) {\n bool flag = true;\n for (const int& j : primes) {\n if (j * j > i)\n break;\n\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\n struct DSU {\n vector<int> par, rank;\n int count = 0;\n DSU(int n)\n {\n par.resize(n);\n iota(par.begin(), par.end(), 0);\n rank.assign(n, 1);\n count = n;\n }\n int find(int x)\n {\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n bool canTraverseAllPairs(vector<int>& nums) {\n int m = 0;\n int n = nums.size();\n queue<int> queue({nums[0]});\n unordered_set<int> visited({nums[0]});\n unordered_set<int> set(nums.begin(), nums.end());\n if (set.contains(1)) retur... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n bool canTraverseAllPairs(vector<int>& nums) {\n int m = 0;\n int n = nums.size();\n queue<int> queue({nums[0]});\n unordered_set<int> visited({nums[0]});\n unordered_set<int> set(nums.begin(), nums.end());\n if (set.contains(1)) retur... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "//class for union find\nclass DS\n{\n public:\n vector<int> parent, size;\n\n DS(int n)\n {\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++)\n {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int find(int node)\n {\n if (node == parent[node])\n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "typedef long long ll;\n\nclass Solution {\npublic:\n vector<int> prime_factor;\n void init(int MAX){\n prime_factor.resize(MAX+1, 1);\n for (int i=2;i<=MAX;i++){\n if (prime_factor[i] != 1)continue;\n // prime number\n prime_factor[i] = i;\n f... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int n,vector<vector<int>>&adj,vector<int>&vis){\n vis[n]=1;\n for(auto i:adj[n]){\n if(vis[i]==0){\n dfs(i,adj,vis);\n }\n }\n }\n \n \n bool canTraverseAllPairs(vector<int>& nums) {\n if(nu... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class UF {\n private:\n std::vector<int> parent;\n\n public:\n UF(int len) : parent(len, -1) {}\n\n int find(int a) {\n if (parent[a] >= 0) {\n return parent[a] = find(parent[a]);\n }\n return a;\n }\n\n bool unionSet(... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n\n int findparent(int node, vector<int>& parent){\n if(node==parent[node]) return node;\n return parent[node]=findparent(parent[node],parent);\n }\n void findUnion(int x, int y, vector<int>& parent, vector<int>& size){\n int px=findparent(x,parent);\... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\nprivate:\n vector<int> getPrime(int n) \n { \n vector<int> prime;\n if(n % 2 == 0){\n prime.push_back(2);\n while (n % 2 == 0) { \n n = n/2; \n } \n }\n // n must be odd at this point. So we can skip \n ... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n void bfs(int root, vector<bool> & vis, vector<vector<int>> & adj) {\n queue<int> q; \n q.push(root); \n vis[root] = true; \n\n while (!q.empty()) {\n auto u = q.front(); \n q.pop(); \n\n for (auto v : ad... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n using bitset = std::vector<std::int8_t>;\n\n std::vector<int> generatePossibleDivizors(int n) {\n n /= 2;\n auto visit = bitset(n);\n \n for (auto j = 4; j < n; j += 2) {\n visit[j] = true;\n }\n for (auto i = 3; i < n /... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class graph{\n private: int n;\n vector<vector<int>> edges;\n void traverse(int x, vector<bool>& vis)\n {\n vis[x] = true;\n for(auto y:edges[x])\n {\n if(!vis[y]) traverse(y, vis);\n }\n }\n\n public:\n graph(int n = 0): n(n){\n edges.resi... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Graph {\nprivate:\n int n;\n vector<vector<int>> edges;\n\n void traverse(int x, vector<bool>& visited) {\n visited[x] = true;\n for (int y: edges[x]) {\n if (!visited[y]) {\n traverse(y, visited);\n }\n }\n }\n\npublic:\n Graph... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Graph {\nprivate:\n int n;\n vector<vector<int>> edges;\n\n void traverse(int x, vector<bool>& visited) {\n visited[x] = true;\n for (int y: edges[x]) {\n if (!visited[y]) {\n traverse(y, visited);\n }\n }\n }\n\npublic:\n Graph... |
2,827 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</cod... | 2 | {
"code": "class Solution {\npublic:\n void getPrimeFactors(set<int>&s , int x) {\n vector<int> primeFactors;\n for (int i = 2; i * i <= x; i++) {\n if (x % i == 0) {\n s.insert(i);\n while (x % i == 0) {\n x /= i;\n }\n ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.