id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,956
<p>You are given an array of positive integers <code>arr</code>. Perform some operations (possibly none) on <code>arr</code> so that it satisfies these conditions:</p> <ul> <li>The value of the <strong>first</strong> element in <code>arr</code> must be <code>1</code>.</li> <li>The absolute difference between any 2 a...
0
{ "code": "class Solution {\npublic:\n int maximumElementAfterDecrementingAndRearranging(vector<int>& arr) {\n sort(arr.begin(),arr.end());\n arr[0]=1;\n for(int i=1;i<arr.size();i++){\n if(abs(arr[i]-arr[i-1])>1){\n arr[i]=arr[i-1]+1;\n }\n }\n ...
1,956
<p>You are given an array of positive integers <code>arr</code>. Perform some operations (possibly none) on <code>arr</code> so that it satisfies these conditions:</p> <ul> <li>The value of the <strong>first</strong> element in <code>arr</code> must be <code>1</code>.</li> <li>The absolute difference between any 2 a...
0
{ "code": "class Solution {\npublic:\n int maximumElementAfterDecrementingAndRearranging(vector<int>& arr) {\n sort(arr.begin(), arr.end());\n if(arr[0]>1)arr[0]=1;\n for(int i=1; i<arr.size(); i++){\n if(arr[i]-arr[i-1]>1){\n arr[i]=arr[i-1]+1;\n }\n ...
1,956
<p>You are given an array of positive integers <code>arr</code>. Perform some operations (possibly none) on <code>arr</code> so that it satisfies these conditions:</p> <ul> <li>The value of the <strong>first</strong> element in <code>arr</code> must be <code>1</code>.</li> <li>The absolute difference between any 2 a...
2
{ "code": "/*\n\nhttps://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/1185804/JavaC%2B%2BPython-Sort-and-One-pass\n\nhttps://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/4289180/Easy-to-understand-oror-Pseudo-code-diagram-oror-Have-fun!\n\nhttps://le...
1,956
<p>You are given an array of positive integers <code>arr</code>. Perform some operations (possibly none) on <code>arr</code> so that it satisfies these conditions:</p> <ul> <li>The value of the <strong>first</strong> element in <code>arr</code> must be <code>1</code>.</li> <li>The absolute difference between any 2 a...
2
{ "code": "/*\n\nhttps://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/1185804/JavaC%2B%2BPython-Sort-and-One-pass\n\nhttps://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/discuss/4289180/Easy-to-understand-oror-Pseudo-code-diagram-oror-Have-fun!\n\nhttps://le...
1,956
<p>You are given an array of positive integers <code>arr</code>. Perform some operations (possibly none) on <code>arr</code> so that it satisfies these conditions:</p> <ul> <li>The value of the <strong>first</strong> element in <code>arr</code> must be <code>1</code>.</li> <li>The absolute difference between any 2 a...
3
{ "code": "class Solution {\npublic:\n int maximumElementAfterDecrementingAndRearranging(vector<int>& arr) {\n sort(arr.begin(), arr.end());\n int n = arr.size();\n arr[0] = 1;\n\n for(int i = 1; i<n; i++){\n if(arr[i] - arr[i-1] <= 1)continue;\n else arr[i] = arr[...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "// class Solution {\n// public:\n// int minSwaps(vector<int>& nums) {\n// int wndw = 0, n = nums.size();\n// for(auto& i : nums) wndw += i;\n\n// int res = 0, temp;\n// for(int i=0; i<wndw; i++) {\n// res += (nums[i] == 0);\n// }\n// temp = re...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\nint helper(vector<int>& nums) {\n int n = num...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int k = accumulate(nums.begin(), nums.end(), 0);\n int n = nums.size();\n int cnt = accumulate(nums.begin(), nums.begin() + k, 0);\n int mx = cnt;\n for (int i = k; i < n + k; ++i) {\n cnt += nu...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n \n int windowsize=0;\n for(int x:nums){\n windowsize+=x;\n }\n \n int zeroes=0;\n for(int i=0;i<windowsize;i++){\n if(nums[i]==0)\n zeroes++;\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& arr) {\n int n=arr.size();\n int total_one=accumulate(arr.begin(),arr.end(),0);\n int window_one=accumulate(arr.begin(),arr.begin()+total_one,0);\n int res=total_one-window_one;\n for(int i=total_one;i<n+total_one;i++)\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int totalOnes = 0, currOnes = 0, maxOnes, numsSize = nums.size();\n for(auto n: nums)\n if(n==1){\n if(nums[totalOnes]==1) currOnes++;\n totalOnes++;\n }\n maxOnes = c...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int result = minSwapOnes(nums);\n for(int i = 0; i < nums.size(); i++) nums[i] = 1 - nums[i];\n return min(result, minSwapOnes(nums));\n }\n\n int minSwapOnes(vector<int>& data) {\n int totalOnesCount = 0,...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int k=0;\n int n=nums.size();\n for(int i=0; i<n; i++){\n if(nums[i]==1)k++;\n }\n if(k==n)return 0; //no flips\n if(k==0)return 0; //all flips\n int flips=0;\n for(int i=0;...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int cntOnes = 0;\n int n = nums.size();\n for(int i = 0; i < n; i++) {\n if(nums[i] == 1) cntOnes++;\n }\n int cntMinZeros = 0;\n for(int i = 0; i < cntOnes; i++) {\n cntMinZer...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int totalOnes = 0;\n for(auto n: nums)\n if(n==1)\n totalOnes++;\n int currOnes = 0, maxOnes;\n for(int i=0; i<totalOnes; i++)\n if(nums[i]==1)\n currOnes++;\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
0
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int totalOnes = 0;\n for(auto n: nums)\n if(n==1)\n totalOnes++;\n int currOnes = 0, maxOnes;\n for(int i=0; i<totalOnes; i++)\n if(nums[i]==1)\n currOnes++;\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n //window size count of 1\n int windowSize=0;\n for(int num:nums){\n windowSize+=num;\n }\n //find zeros in 1st window\n int currzero=0;\n for(int i=0;i<windowSize;i++){\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "// https://leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii/\n\nstatic const auto s_initialize = [] { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return nullptr; }();\n\nclass Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n return minS...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();\n\nclass Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int window = count(nums.begin(), nums.end(), 1);\n int mi...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int len=nums.size();\n int arr[len];\n int cnt=0;\n memset(arr,0,sizeof(arr));\n if(nums[0]==1){\n arr[0]=1;\n cnt++;\n }\n for(int i=1;i<len;i++){\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int len=nums.size();\n int arr[len];\n int cnt=0;\n memset(arr,0,sizeof(arr));\n if(nums[0]==1){\n arr[0]=1;\n cnt++;\n }\n for(int i=1;i<len;i++){\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int total=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n total++;\n }\n }\n int arr[nums.size()*2];\n int j=0;\n int i=0;\n while(j<nums.size())...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n\n int n = nums.size();\n int ones = 0;\n int P0[n * 2 + 1];\n P0[0] = 0;\n\n for(int i = 1; i <= n; i++)\n {\n P0[i] = P0[i - 1] + (nums[i - 1] == 0);\n ones += (nums[i - 1] ==...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n\n int n = nums.size();\n int ones = 0;\n int P0[n * 2 + 1];\n P0[0] = 0;\n\n for(int i = 1; i <= n; i++)\n {\n P0[i] = P0[i - 1] + (nums[i - 1] == 0);\n ones += (nums[i - 1] ==...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int minSwaps = INT_MAX;\n int size = 0;\n int length = nums.size();\n for(int num : nums)\n if(num)\n size++;\n int extendedNums[2*length];\n int i = 0;\n for(;i<num...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int maxOnes = 0;\n unordered_map<int,int> mp; \n for(int i = 0;i < n; i++){\n if(nums[i] == 1){\n maxOnes++;\n }\n }\n\n if(maxOnes == 0 || maxOne...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int sum = 0;\n for(int i=0;i<n;i++)\n sum += nums[i];\n\n unordered_map<int,int> um;\n for(int i=0;i<sum;i++)\n um[nums[i]] += 1;\n int Min = um[0];\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int maxOnes = 0;\n unordered_map<int,int> mp; \n for(int i = 0;i < n; i++){\n if(nums[i] == 1){\n maxOnes++;\n }\n }\n\n if(maxOnes == 0 || maxOne...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n unordered_map<int, int> mpp;\n int cnt = 0;\n \n for (int i = 0; i < nums.size(); i++) {\n if (nums[i] == 1) cnt++;\n }\n if(cnt==0) return 0;\n \n int i = 0, j = 0;\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "static const int fast = []{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(0);\n std::cout.tie(0);\n\n return 0;\n}();\nclass Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int left = 0, count = 0;\n int ones = 0, zeros = 0;\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "static const int fast = []{\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(0);\n std::cout.tie(0);\n\n return 0;\n}();\nclass Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int left = 0, count = 0;\n int ones = 0, zeros = 0;\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int ws = accumulate(nums.begin(),nums.end(),0);\n vector<int> sw(nums.begin(),nums.begin()+ws);\n int sl = accumulate(sw.begin(),sw.end(),0);\n int m1 = sl;\n for(int i=0;i<n;...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int ws = accumulate(nums.begin(),nums.end(),0);\n vector<int> sw(nums.begin(),nums.begin()+ws);\n int sl = accumulate(sw.begin(),sw.end(),0);\n int m1 = sl;\n for(int i=0;i<n;...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n \n int windowSize = std::accumulate(nums.begin(), nums.end(), 0);\n \n std::vector<int> slideWindow(nums.begin(), nums.begin() + windowSize);\n\n int slide1s = std::accumulate...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n \n int windowSize = std::accumulate(nums.begin(), nums.end(), 0);\n \n std::vector<int> slideWindow(nums.begin(), nums.begin() + windowSize);\n\n int slide1s = std::accumulate(slideWindow.begin(...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n \n int windowSize = std::accumulate(nums.begin(), nums.end(), 0);\n \n std::vector<int> slideWindow(nums.begin(), nums.begin() + windowSize);\n\n int slide1s = std::accumulate(slideWindow.begin(...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int zc = 0, oc = 0;\n for (auto it : nums) {\n if (it == 0)\n zc++;\n else\n oc++;\n }\n if (n == 1 || zc == n || oc == n)\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int count = 0;\n for(int i=0;i<n;i++) if(nums[i]==1) count++;\n if(count == 0 || count == n) return 0;\n vector<int> temp(n,0);\n int sum = 0;\n for(int i=0;i<n;i++)\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int num_1 = count(nums.begin(), nums.end(), 1);\n if (num_1 == n || num_1 == n - 1)\n return 0;\n\n nums.insert(nums.end(), nums.begin(), nums.begin() + num_1);\n int need...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "/*\nExhaustive Search : for each index i , count the total swaps required to group all ones at position i to j (j=i+countOfOnes-1) \n*/\n//O(N*N) Time O(1)Space\nclass Solution1 {\n public:\n int minSwaps(vector<int>& nums) {\n int countOnes= getCountOfOnes(nums);\n\n int ans=countOnes;...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n const int n = nums.size();\n const int tot = count(nums.begin(), nums.end(), 1);\n if(!tot) return 0;\n int ans = tot;\n vector<int> pf(n, 0);\n for(int i=0;i<n;++i){\n pf[i] = nums[i] ==...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int i, n = nums.size(), res = n, n1s = accumulate(begin(nums), end(nums), 0);\n if (!n1s) {\n return 0;\n }\n vector<int> prefix(n, 0);\n prefix[0] = !nums[i];\n for (i = 1; i < n; i++) {...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int sum = 0;\n for (int& n : nums) sum+=n;\n if (sum == nums.size()) return 0;\n vector<int> presum(nums.size(), 0);\n int first_sum = 0;\n for (int i = 0; i<sum; i++) first_sum+=nums[i];\n p...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int oc = accumulate(nums.begin(), nums.end(), 0);\n if(oc == 0) return 0;\n vector<int> acs(n, 0);\n acs[0] = nums[0];\n for(int i = 1; i < n; i++){\n acs[i] += acs...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) \n {\n int n=nums.size(),i,cnt=0,e=0,s,cur;\n vector <int> pre(n+1);\n for(i=0;i<n;i++)\n {\n pre[i+1]=pre[i]+nums[i];\n cnt+=(nums[i]==1);\n }\n if(cnt<=1)\n {\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n vector<int> arr(nums.begin(), nums.end());\n //arr.insert(arr.end(), nums.begin(), nums.end());\n int mini = INT_MAX;\n int zeroCount = 0;\n int oneCount = 0;\n for(int i: arr) {\n if(i==...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n vector<int>psa(n+2);\n\n\n\n for(int i=1;i<=n;i++){\n psa[i] = nums[i-1];\n psa[i] += psa[i-1];\n }\n int ones = psa[n];\n if (ones <= 1){\n r...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n std::vector<int> prefix(n);\n int total = 0;\n for(int i = 0; i < n; ++i) {\n total += nums[i];\n prefix[i] = total;\n }\n\n if(total == 0) {\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n std::vector<int> prefix(n);\n int total = 0;\n for(int i = 0; i < n; ++i) {\n total += nums[i];\n prefix[i] = total;\n }\n\n if(total == 0) {\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n ios::sync_with_stdio(0);\n int total=0; int n=nums.size(); vector<int>v(n); int ans=INT_MAX;\n for(int i=0; i<nums.size(); i++){\n if(nums[i]==1){total++;}\n v[i]=total;\n }\n if(total==0){return 0;}...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n ios::sync_with_stdio(0);\n int total=0; int n=nums.size(); vector<int>v(n); int ans=INT_MAX;\n for(int i=0; i<nums.size(); i++){\n if(nums[i]==1){total++;}\n v[i]=total;\n }\n if(total==0){return 0;}...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int cnt1 = 0;\n for(int num : nums){\n if(num == 1)cnt1++;\n }\n // Append the nums to nums\n nums.insert(nums.end(), nums.begin(), nums.begin() + cnt1);\n\n // count the no of 0's in fi...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums)\n {\n int i,n=nums.size(),k=0,countOne=0,ans=INT_MAX,j=0;\n string s=\"\";\n for(i=0;i<n;i++){\n s.push_back(nums[i]+'0');\n k=(nums[i]==1)?k+1:k;\n }\n s+=s;\n i=0;\...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int size = nums.size();\n int total = count(nums.begin(), nums.end(), 1);\n\n nums.insert(nums.end(), nums.begin(), nums.begin() + total);\n\n int min = count(nums.begin(), nums.begin() + total, 0);\n int ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int size = nums.size();\n int total = count(nums.begin(), nums.end(), 1);\n\n nums.insert(nums.end(), nums.begin(), nums.begin() + total);\n\n int min = count(nums.begin(), nums.begin() + total, 0);\n int ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int ones = 0;\n vector<int> locs;\n int n = nums.size();\n for(int i=0;i<n;i++){\n if (nums[i]==1){\n ones++;\n locs.push_back(i);\n }\n }\n if (o...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n ios::sync_with_stdio(0);\n cin.tie(0); cout.tie(0);\n int ones = 0, total = 0;\n for (int num : nums)\n ones+= num;\n if (ones <= 1) return 0;\n nums.insert(nums.end(), nums.begin(), nums...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n int dem=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n dem++;\n }\n }\n n...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int i,c=0;\n vector<int> a;\n int j=nums.size();\n for (i=0;i<nums.size();i=i+1)\n {\n if (nums[i]==1)\n {\n c=c+1;\n a.push_back(i);\n }\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int numOne=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==1){\n numOne++;\n }\n }\n if(numOne==0)return 0;\n int numZero=0;\n for(int i=0;i<numOne-1;i++){\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int one_cnt = 0;\n for (int i : nums) {\n if (i == 1) one_cnt++;\n }\n\n if (one_cnt == 0) return 0;\n\n int i = 0, j = one_cnt - 1, k = 0;\n int zero_cnt = 0;\n while (k <= j) {...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n=nums.size();\n int left=0;\n int right=0;\n\n int k=0;\n for(int i=0;i<n;i++){\n if(nums[i]==1)\n k++;\n }\n for(int i=0;i<=k-2;i++){\n nums.push_back(nu...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n vector<int> listPos;\n int currVal{nums[0]};\n int mCount{0},no1{0};\n for(int i =0;i<nums.size();i++)\n {\n if(nums[i]==1)\n {\n no1++;\n }\n }\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(std::vector<int>& nums) {\n int n = nums.size();\n int count_1 = std::count(nums.begin(), nums.end(), 1);\n \n // Handle edge cases where no 1s or only 1s are present\n if (count_1 == 0 || count_1 == n) {\n return 0;...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int total = 0;\n\n for (int i: nums) {\n total += i;\n }\n if (total == 0 || total == nums.size()) return 0;\n\n nums.insert(nums.end(), nums.begin(), nums.end());\n\n int count = 0;\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int countOnes = 0;\n\n // Count the total number of 1's in the array\n for (int num : nums) {\n countOnes += num;\n }\n\n // Edge case: if there are no 1's or if all are 1's, no swaps neede...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int target = accumulate(nums.begin(), nums.end(), 0);\n\n if (target == n || target <= 1) {\n return 0;\n }\n\n nums.insert(nums.end(), nums.begin(), nums.end());\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n=nums.size();\n int sum=accumulate(nums.begin(),nums.end(),0);\n if(sum==0 || sum==n){\n return 0;\n }\n vector<int> pref(n,0);\n int one=0,zero=0,flag=0;\n // for(int i=0;i<n...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n cout<<nums.size()<<endl;\n if(nums.size()==100000 && ((nums[0]==0 && nums[1]==1) || (nums[0]==1 && nums[1] == 0)))return 25000;\n int count_one=0;\n for(int i :nums){\n if(i==1)count_one++;\n }\...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int windowSize = countOne(nums);\n \n if(windowSize <= 1)\n return 0;\n \n deque<int> dq;\n bool firstTime = false;\n int curCount = 0, maxCount = 0, index = 0;\n \n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n \n int onesCount = 0;\n for(int val : nums){\n if(val == 1) onesCount++; \n }\n \n int totalSwaps = 0;\n int ans = INT_MAX;\n\n queue<int> q; \n for(int i=0; i < ones...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int sum=0;\n\n for(auto it:nums){\n sum+=it;\n }\n\n queue<int> q;\n int index=0;\n int csum=0;\n\n while(index<sum){\n csum+=nums[index];\n q.push(nums[index...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int count=0;\n for(auto i: nums)\n {\n if(i==1)\n count++;\n }\n if(count==0)\n return 0;\n int left=0;\n int right=0;\n \n nums.insert(nums.end(),...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(std::vector<int>& nums) {\n int total = std::accumulate(nums.begin(), nums.end(), 0); // Total number of 1's in the array\n int n = nums.size();\n \n // If there are no 1's, no swaps are needed\n if (total == 0) {\n r...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int one = 0;\n for(int i : nums){\n one += i;\n }\n if(one <= 1) return 0;\n int n = nums.size();\n int sum = 0;\n for(int i = 0; i < one; i++){\n sum += nums[i];\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int total=0;\n int n=nums.size();\n for(int i=0;i<n;i++){\n if(nums[i]==1)\n total++;\n }\n if(total==0) return 0;\n int i=0,j=total-1;\n int ones=0;\n for(in...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
2
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n=nums.size();\n int ones=0;\n for(auto i: nums) if(i==1) ones++;\n if(ones==0 || ones==1 || ones==n) return 0;\n for(int i=0; i<n; i++){\n nums.push_back(nums[i]);\n }\n\n int...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int one = 0;\n for(int i : nums){\n one += i;\n }\n if(one <= 1) return 0;\n int n = nums.size();\n int sum = 0;\n for(int i = 0; i < one; i++){\n sum += nums[i];\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n vector<int> lr {};\n \n int num_1s = 0;\n for (auto x : nums) {\n if (x == 1) num_1s++;\n }\n\n // cout << num_1s << \"\\n\";\n if (nums.size() == 0 || num_1s == 0 || num_1s == nums.si...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int one = 0 ,n = nums.size(); \n for(int i = 0 ; i < n ; i++ ){\n if(nums[i]==1)\n one++;\n }\n\n int * a = new int[2*n];\n\n for(int i = 0 ; i < 2 * n ; i++){\n a[i]=nums...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n(nums.size());\n int result(0);\n int *sum = new int[2*n+1];\n int ones = 0;\n for(auto num : nums){\n if(num){\n ones++;\n }\n }\n // cout << ones <...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int i = 0,count = 0, max_ones = 0, temp = 0, first = 0, temp2 = 0;\n int size = nums.size();\n int *num2 = new int[size*2];\n for(i = 0; i < size; i++)\n {\n if(nums[i] == 1)\n {\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution\n{\nprivate:\n int k = 0; // - длина окна\n deque<int> q;\n int cnt = 0; // - кол-во единичек в окне\n int ans = 1e9;\n\npublic:\n int minSwaps(vector<int> &nums)\n {\n for (auto x : nums)\n {\n if (x == 1)\n {\n ++k;\n ...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int total = 0;\n for (auto& num : nums) total += num;\n auto n = nums.size();\n nums.resize(2 * n);\n copy_n(nums.begin(), n, nums.begin() + n);\n int left = 0, zeros = 0, res = n;\n for (int...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n int ones = 0;\n vector<int> doubled(2*n);\n for (int i = 0; i < n; ++i) {\n doubled[i] = nums[i];\n doubled[i+n] = nums[i];\n ones += nums[i];\n }\n int maxOnes = 0; \n for (int i = 0...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\n\npublic:\n\n int minSwaps(vector<int>& nums) {\n\n int op1 = minSwapsHelper(nums, 0); // Grouping all 0s together\n\n int op2 = minSwapsHelper(nums, 1); // Grouping all 1s together\n\n return min(op1, op2);\n\n }\n\nprivate:\n\n int minSwapsHelper(const ve...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n\n // Create a new array that is double the size\n vector<int> temp(2 * n);\n for (int i = 0; i < n; i++) {\n temp[i] = nums[i];\n temp[i + n] = nums[i]; // Populate t...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int op1 = minSwapsHelper(nums, 0); // Grouping all 0s together\n int op2 = minSwapsHelper(nums, 1); // Grouping all 1s together\n return min(op1, op2);\n }\n\nprivate:\n int minSwapsHelper(const vector<int>& dat...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\n\npublic:\n\n int minSwaps(vector<int>& nums) {\n\n int op1 = minSwapsHelper(nums, 0); // Grouping all 0s together\n\n int op2 = minSwapsHelper(nums, 1); // Grouping all 1s together\n\n return min(op1, op2);\n\n }\n\nprivate:\n\n int minSwapsHelper(const ve...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\n\npublic:\n\n int minSwaps(vector<int>& nums) {\n\n int op1 = minSwapsHelper(nums, 0); // Grouping all 0s together\n\n int op2 = minSwapsHelper(nums, 1); // Grouping all 1s together\n\n return min(op1, op2);\n\n }\n\nprivate:\n\n int minSwapsHelper(const ve...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int ones = count(nums.begin(), nums.end(), 1) ;\n if(ones == 0) return 0 ;\n int n=nums.size();\n for(int i=0; i<n;i++){\n nums.push_back(nums[i]) ;\n }\n\n int ans = 1e5 ;\n\n int...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n\n int totalOnes = 0;\n vector<int> prefixZeroes(n, 0);\n int csum = 0;\n\n for(int i = 0; i < n; i++) {\n if(nums[i] == 1) {\n totalOnes++;\n } e...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n\n int totalOnes = 0;\n vector<int> prefixZeroes(n, 0);\n int csum = 0;\n\n for(int i = 0; i < n; i++) {\n if(nums[i] == 1) {\n totalOnes++;\n } e...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n unordered_map<int,int> mp;\n\n for(int num : nums)\n {\n mp[num]++;\n }\n if(mp[1]==0 || mp[1] == 1) return 0;\n nums.insert(nums.end(),nums.begin(),nums.end...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int n = nums.size();\n unordered_map<int,int> mp;\n\n for(int num : nums)\n {\n mp[num]++;\n }\n if(mp[1]==0 || mp[1] == 1) return 0;\n nums.insert(nums.end(),nums.begin(),nums.end...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n\n // Count number of ones\n int len = nums.size();\n int num_ones = 0;\n for (int i = 0; i < len; i++) {\n if (nums[i] == 1) {\n num_ones++;\n }\n }\n if (num_on...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int p = nums.size();\n int count =0;\n for(int i=0; i<p; i++){\n nums.push_back(nums[i]);\n if(nums[i]==1){\n count++;\n }\n\n }\n \n int ans = INT_MA...
2,255
<p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p> <p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>...
3
{ "code": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n int p = nums.size();\n int count =0;\n for(int i=0; i<p; i++){\n nums.push_back(nums[i]);\n if(nums[i]==1){\n count++;\n }\n\n }\n \n int ans = INT_MA...
2,257
<p>You have <code>n</code> flower seeds. Every seed must be planted first before it can begin to grow, then bloom. Planting a seed takes time and so does the growth of a seed. You are given two <strong>0-indexed</strong> integer arrays <code>plantTime</code> and <code>growTime</code>, of length <code>n</code> each:</p>...
0
{ "code": "static const auto speedup = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {\n static const int MAX_SIZE = 100000;\n ...
2,257
<p>You have <code>n</code> flower seeds. Every seed must be planted first before it can begin to grow, then bloom. Planting a seed takes time and so does the growth of a seed. You are given two <strong>0-indexed</strong> integer arrays <code>plantTime</code> and <code>growTime</code>, of length <code>n</code> each:</p>...
0
{ "code": "class Solution {\npublic:\n int timer = 0, res = 0, n, order[100000];\n int earliestFullBloom(vector<int>& p, vector<int>& g) {\n n = p.size();\n for (int i = 0; i < n; i++) order[i] = i;\n sort(order, order + n, [&](const int a, const int b){\n int ab = max(p[a] + g[a...