id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
0
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n \n vector<pair<int,int>> s;\n for(int i=0;i<nums.size();i++){\n s.push_back({nums[i],i});\n }\n\n sort(s.begin(),s.end());\n\n vector<int> temp, a...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
0
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<pair<int,int>> v;\n for(int i=0; i<nums.size(); i++)\n v.push_back({nums[i], i});\n sort(v.begin(), v.end());\n vector<int> numbers, indices;\n for(in...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
0
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<tuple<int,int>> arr{};\n for(int i = 0; i<nums.size(); i++) arr.push_back({nums[i], i});\n sort(arr.begin(), arr.end());\n vector<int> idx{};\n vector<int> v...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "\ntypedef pair<int,int> pii; \nclass Solution {\npublic:\n \n void solve(int si,int ei,vector<pii> temp,vector<int> &nums){\n if(si>=ei) return ;\n vector<int> index;\n for(int i=si;i<=ei;i++)\n index.push_back(temp[i].second);\n\n sort(index.begin(),index.end());\...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n=nums.size();\n vector<array<int,2>> vp(n);\n for(int i=0;i<n;++i)\n {\n vp[i][0]=nums[i];\n vp[i][1]=i;\n }\n sort(vp.begin(),vp....
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\nvoid populateResult(vector<int> &res, vector<int> &ids, vector<int> &vals) {\n sort(begin(ids), end(ids));\n for (int j = 0; j < ids.size(); ++j)\n res[ids[j]] = vals[j];\n}\nvector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<int> ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<pair<int,int>> valinds;\n if(nums.size() == 1) return nums;\n for(int i = 0;i<nums.size();i++)\n {\n valinds.push_back({nums[i],i});\n }\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) \n {\n vector<pair<int,int>> v;\n for(int i=0;i<nums.size();i++)\n {\n v.push_back({nums[i],i});\n }\n sort(v.begin(),v.end());\n int n = nums.size...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int k) {\n int n=nums.size();\n vector <pair<int,int>> v;\n for(int i=0;i<n;i++){\n v.push_back({nums[i],i});\n }\n vector <int> ans(n);\n sort(v.begin(),v.end()...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n static bool compare(pair<int, int> &a, pair<int, int> &b){\n if(a.first==b.first)\n return a.second<b.second;\n return a.first<b.first;\n }\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size()...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "/*\ngroup: for any group of num, we get max and min; if the next num is within [min-2, max+2], add it to the group.\nWe can sort inside the group. We can not sort between group. Once the group can not extend further, the group stops.\nA little like 1-D DFS? \non every helper function: 1. find the range of ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int lim) {\n vector<pair<int,int>>v;\n for(int i=0;i<nums.size();i++){\n v.push_back({nums[i],i});\n }\n vector<pair<int,int>>vo;\n sort(v.begin(),v.end());\n ve...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n vector<pair<int, int>> nums2(n);\n for (int i = 0; i < n; i++) {\n nums2[i] = {nums[i], i};\n }\n sort(nums2.begin(), nums2.end())...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n vector<pair<int, int>> nums2(n);\n for (int i = 0; i < n; i++) {\n nums2[i] = {nums[i], i};\n }\n sort(nums2.begin(), nums2.end())...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int N = nums.size();\n vector<int> pos(N);\n iota(begin(pos), end(pos), 0);\n\n ranges::sort(pos, [&nums](const auto& l, const auto& r) {\n return nums[l] < num...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class DSU{\n public:\n int total;\n vector<int> par,size;\n\n DSU(int n){\n total=n;\n for(int i=0;i<n;i++){\n par.push_back(i);\n }\n size.assign(n,1);\n }\n\n int find_set(int v){\n if(v==par[v]){\n return v;\n }\n r...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n vector<vector<int>> nums_with_indices(n, vector<int>(2));\n\n for(int i = 0; i < n; i++) {\n nums_with_indices[i][0] = nums[i];\n num...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n vector<pair<int,int>> temp;\n for(int i=0;i<n;i++){\n temp.push_back({nums[i],i});\n }\n sort(temp.begin(),temp.end());\n v...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int K) {\n int N=nums.size();\n vector<pair<int,int>>V,W;\n for(int i=0;i<nums.size();i++)\n {\n V.push_back({nums[i],i});\n }\n sort(V.begin(),V.end());\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n // static bool comp(int &a, int &b){\n // return a>b;\n // }\n\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<pair<int,int>>v;\n int n=nums.size();\n vector<int>find(n);\n for(int i=0; i<n; i++){\...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class DSU {\npublic:\n vector<int> parent, rank;\n\n DSU(int n) {\n parent.resize(n+1);\n rank.resize(n+1, 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 parent[x] = Find(pare...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n int find(vector<int> &ds, int i) {\n return ds[i] < 0 ? i : ds[i] = find(ds, ds[i]);\n }\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<int> ds(nums.size(), -1), ids(nums.size()), res(nums.size());\n iota(begin...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n void upt(vector<int> &ret, vector<int> &ind, vector<int> &vals){\n sort(ind.begin(), ind.end());\n for(int i=0;i<ind.size();i++){\n ret[ind[i]] = vals[i];\n }\n ind.clear();\n vals.clear();\n }\n vector<int> lexicographicall...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<int> ans(nums.size());\n vector<pair<int, int>> tmp;\n\n // Create a vector of pairs (element, index)\n for (int i = 0; i < nums.size(); i++) {\n tmp.pus...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n #define pii pair<int,int>\n #define minheap priority_queue<int,vector<int>,greater<int>>\n\n vector<int>parent,rank;\n int find(int x){\n if(x==parent[x]) return x;\n\n return parent[x] = find(parent[x]);\n }\n void Union(int x,int y){\n in...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n=nums.size();\n vector<vector<int>>net(n);\n for(int i=0;i<n;i++){\n net[i]={nums[i],i};\n }\n sort(net.begin(),net.end());\n int ptr2=0;\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
1
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n vector<int> ans;\n\n vector<pair<int,int>> valIndices;\n for(int i=0;i<n;i++) valIndices.push_back({nums[i],i});\n sort(valIndices.begin(), v...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "// class Solution {\n// public:\n// vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit){\n// int n=nums.size();\n// vector<pair<int, int>> arr(n);\n// for(int i=0;i<n;i++) arr[i]={nums[i], i};\n// sort(arr.begin(), arr.end());\n// priority_qu...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "// class Solution {\n// public:\n// vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit){\n// int n=nums.size();\n// vector<pair<int, int>> arr(n);\n// for(int i=0;i<n;i++) arr[i]={nums[i], i};\n// sort(arr.begin(), arr.end());\n// priority_qu...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class disjointSet {\n vector<int> par;\n vector<int> rank;\n vector<int> size;\n\npublic:\n disjointSet(int n) {\n par.resize(n);\n rank.resize(n, 0);\n size.resize(n, 1);\n for (int i = 0; i < n; i++) {\n par[i] = i;\n }\n }\n int findpar(int...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class disjointSet {\n vector<int> par;\n vector<int> rank;\n vector<int> size;\n\npublic:\n disjointSet(int n) {\n par.resize(n);\n rank.resize(n, 0);\n size.resize(n, 1);\n for (int i = 0; i < n; i++) {\n par[i] = i;\n }\n }\n int findpar(int...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<pair<int,int>> v;\n int n=nums.size();\n for(int i=0;i<int(nums.size());++i){\n v.push_back({nums[i],i});\n }\n sort(v.begin(),v.end());\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "const int ZERO = []()\n{\nstd::ios_base::sync_with_stdio(false);\nstd::cin.tie(nullptr);\nreturn 0;\n}();\n\n\nclass Solution {\npublic:\n\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size(), i =0;\n vector<int> result(nums.begin(), nums.end())...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n #define F first\n #define S second\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<pair<int,int>> sorted;\n \n for(int i=0;i<nums.size();i++){\n sorte...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class DSU {\npublic :\n vector<int> par, rank;\n DSU(int n) {\n par.resize(n), rank.resize(n);\n iota(par.begin(), par.end(), 0);\n }\n \n int find(int u) {\n return par[u] = par[u] == u ? u : find(par[u]);\n }\n \n void merge(int u, int v) {\n int x = fi...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class DSU {\npublic :\n vector<int> par, rank;\n DSU(int n) {\n par.resize(n), rank.resize(n);\n iota(par.begin(), par.end(), 0);\n }\n \n int find(int u) {\n return par[u] = par[u] == u ? u : find(par[u]);\n }\n \n void merge(int u, int v) {\n int x = fi...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n std::vector<int> result(nums.size());\n std::vector<std::vector<std::pair<int, int> > > groups;\n std::vector<std::pair<int, int> > enumerate_nums;\n \n for (size_t...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class Solution {\n public:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<int> ans(nums.size());\n // [[(num, index)]], where the difference between in each pair in each\n // `[(num, index)]` group <= `limit`\n vector<vector<pair<int, int>>> numAndIndexes...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class DisjointSet {\n vector<int> parent;\n vector<int> 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 int find(int node) {\n if (node ==...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class DisjointSet {\n public:\n vector<int> rank, parent, size;\n DisjointSet(int n) \n {\n rank.resize(n+1,0);\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 ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<vector<int>> arr;\n int n = nums.size();\n\n for (int i=0; i<n; ++i)\n arr.push_back({nums[i], i});\n sort(arr.begin(), arr.end());\n\n int i = 0;...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n \n int n = nums.size();\n \n vector<pair<int, int>> temp;\n \n for(int i = 0 ; i < n ; i++)\n temp.push_back({nums[i], i});\n \n sor...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "/*const int ZERO = []()\n{\nstd::ios_base::sync_with_stdio(false);\nstd::cin.tie(nullptr);\nreturn 0;\n}();\n*/\n\nclass Solution {\npublic:\n\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size(), i =0;\n vector<int> result(nums.begin(), nums.en...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "const int ZERO = []()\n{\nstd::ios_base::sync_with_stdio(false);\nstd::cin.tie(nullptr);\nreturn 0;\n}();\n\n\nclass Solution {\npublic:\n\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size(), i =0;\n vector<int> result(nums.begin(), nums.end())...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class DSU {\n vector<int>parent,rank;\npublic:\n DSU(int n){\n parent.resize(n,-1);\n rank.resize(n,0);\n }\n int find(int v) {\n if(parent[v] == -1)\n return v;\n return parent[v] = find(parent[v]);\n }\n\n void unionByRank(int u,int v) {\n int f...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class DisjointSet {\n public:\n vector<int> par, size;\n DisjointSet(int n){\n par.resize(n);\n size.resize(n);\n for(int i=0;i<n;i++){\n par[i] = i;\n size[i] = 1;\n }\n }\n \n int findPar(int node) {\n if(node == par[n...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "#include<iostream>\n#include<bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n#define minPq priority_queue <ll, vector<ll>, greater<ll>>\n\nclass Solution {\npublic:\n\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n // disjointSet ds(nums.size());\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "// Priority Queue (min-heap) contains: {1, 0}, {3, 2}, {5, 1}, {9, 3}, {8, 4}\n// Processing the Priority Queue\n// First Iteration:\n\n// topp = pq.top() → topp = {1, 0}\n// Pop topp from pq → pq becomes {3, 2}, {5, 1}, {9, 3}, {8, 4}\n// pq.top() → {3, 2}\n// Check abs(pq.top().first - topp.first) <= lim...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n vector<int> temp = nums;\n sort(temp.begin(),temp.end());\n vector<int> splits(n);\n for(int i=1;i<n;i++){\n if(temp[i]-temp[i-1]>...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class disjointset{\n vector<int> rank,parent;\n public:\n disjointset(int v){\n rank.resize(v,0);\n parent.resize(v);\n for(int i=0;i<v;i++){\n parent[i]=i;\n }\n }\n int findparent(int v){\n if(parent[v]==v){\n return v;\n }\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
2
{ "code": "class Solution {\npublic:\n\n\nvector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<pair<int,int>>v;\n int n = nums.size();\n for(int i=0;i<nums.size();i++){\n v.push_back({nums[i],i});\n }\n sort(v.begin(),v.end());\n map<int,vector<pair<int,int>>>m;...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class DSU{\n public:\n vector<int> parent;\n vector<int> size;\n DSU (int n){\n parent.resize(n+1);\n size.resize(n+1);\n for (int i=0; i<=n; i++){\n parent[i] = i;\n size[i] = 1;\n }\n }\n int findUpar(int node){\n if (node == pare...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<pair<int,int>> consNums;\n for(int i=0;i<nums.size(); i++){\n consNums.push_back({nums[i],i});\n }\n sort(consNums.begin(), consNums.end());\n\n v...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n vector<int> v = nums;\n\n\n int j = 0;\n sort(v.begin(), v.end());\n map<int, int> mp;\n mp[v[0]] = v[0];\n for(int i = 1; i < ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n unordered_map<int, int> parent;\n\n int getParent(int a) {\n if (a == parent[a]) return a;\n parent[a] = getParent(parent[a]);\n return parent[a];\n }\n\n int make_union(int a, int b) {\n int pa = getParent(a);\n int pb = getParent(...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class UnionFind {\nprivate:\n vector<int> root, rank;\npublic:\n UnionFind(int n)\n {\n root.resize(n);\n rank.resize(n);\n for(int i=0;i<n;i++)\n {\n root[i] = i;\n rank[i] = 1;\n }\n } \n int find(int x)\n {\n if(root[x]==x...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class dsu{\n vector<int>parent,size;\n public:\n dsu(int n)\n {\n parent.resize(n);\n size.resize(n,1);\n for(int i=0;i<n;i++)\n parent[i]=i;\n }\n int get(int a)\n {\n if(a==parent[a])\n return a;\n\n return parent[a]=get(parent[a]);\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class dsu{\n vector<int>parent,size;\n public:\n dsu(int n)\n {\n parent.resize(n);\n size.resize(n,1);\n for(int i=0;i<n;i++)\n parent[i]=i;\n }\n int get(int a)\n {\n if(a==parent[a])\n return a;\n\n return parent[a]=get(parent[a]);\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n int findParent(vector<int>&parent,int i)\n {\n if (parent[i]==i)return i;\n return findParent(parent,parent[i]);\n }\n void unionOp(vector<int>&size,int a,int b,vector<int>&parent)\n {\n int ap=parent[a];\n int bp=parent[b];\n if...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n vector<pair<int, int>> sorted;\n for (int i = 0; i < n; i++) {\n sorted.push_back({nums[i], i});\n }\n sort(sorted.begin(), sorted...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n int find(vector<int>&parent, int x){\n if(parent[x]==x) return x;\n return parent[x]=find(parent, parent[x]);\n }\n void unionbysize(vector<int>&parent, vector<int>&size, int x, int y){\n int a = find(parent, x);\n int b = find(parent, y);\n\...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n \n int n = nums.size();\n vector<pair<int, int>> arr;\n\n for (int i = 0; i < n; i++) {\n arr.push_back({nums[i], i});\n }\n\n sort(arr.begin(), a...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n int fn(int x, vector<int> &rn) {\n if(x==rn[x]) {\n return x;\n }\n return rn[x]=fn(rn[x],rn);\n }\n\n bool un(int x, int y, vector<int> &rn) {\n x=fn(x,rn);\n y=fn(y,rn);\n if(x==y) {\n return false;\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "#define pii pair<int, int>\n#define F first\n#define S second\n\nstruct DSU {\n vector<int> p;\n vector<int> sz;\n \n DSU (int n) {\n p.resize(n);\n for (int j = 0; j < n; j ++) p[j] = j;\n \n sz.resize(n, 1);\n }\n \n int Leader (int x) {\n if (x == ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<int> vec(nums.begin(), nums.end());\n sort(vec.begin(), vec.end());\n unordered_map<int, multiset<int>*> mp;\n int n = nums.size();\n multiset<int> *s = new ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class UnionFind {\nprivate:\n std::vector<int> p, rank; // remember: vi is vector<int>\npublic:\n UnionFind(int N) {\n rank.assign(N, 0);\n p.assign(N, 0);\n for (int i = 0; i < N; i++)\n p[i] = i;\n }\n int findSet(int i) { return (p[i] == i) ? i : (p[i] = findS...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<vector<int>> v;\n int gst = 0;\n for(int i=0; i<nums.size(); i++)\n v.push_back({nums[i], i});\n sort(v.begin(), v.end());\n vector<int> indices;\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<vector<int>> v;\n int gst = 0;\n for(int i=0; i<nums.size(); i++)\n v.push_back({nums[i], i});\n sort(v.begin(), v.end());\n vector<int> indices;\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& A, int limit) {\n int n = A.size();\n auto S = A;\n sort(S.begin(), S.end());\n map<int, int> value_to_group_id;\n vector<multiset<int>> groups;\n for (int i = 0; i < n; ++i) {\n...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\n\n \npublic:\n vector<int> par, sz;\n int getPar(int u){\n while(u!=par[u]){\n par[u] = par[par[u]];\n u = par[u];\n }\n return u;\n }\n\n void merge(int u, int v){\n int par1 = getPar(u), par2 = getPar(v);\n if(par1=...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n // map<int,int>par;\n int findp(int n,map<int,int>&par)\n {\n if(par[n]==n)return n;\n return par[n]=findp(par[n],par);\n }\n \n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n map<...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n using vi=vector<int>;\n vi sz,par;\n int find(int a){\n if(a==par[a])return a;\n return par[a]=find(par[a]);\n }\n void merge(int a,int b){\n a=find(a);\n b=find(b);\n if(a!=b){\n if(sz[a]<sz[b])swap(a,b);\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class DisjointSet{\n vector<int> parent,size;\n public:\n DisjointSet(int n){\n parent.resize(n);\n size.resize(n);\n for(int i=0;i<n;i++){\n parent[i]=i;\n size[i]=1;\n }\n }\n int findUPar(int u){\n if(parent[u]==u)\n retur...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\nprivate:\n vector<int> parent;\n vector<int> size;\n\n int find(int i)\n {\n if(parent[i]==i)\n {\n return i;\n }\n return parent[i] = find(parent[i]);\n }\n\n void unionBySize(int i, int j)\n {\n int irep = find(i);\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n\n void dfs(int node, vector<int> list[], vector<int>& vis, vector<int>& ret){\n ret.push_back(node);\n vis[node] = 1;\n for(auto it : list[node]){\n if(vis[it]==0){\n dfs(it, list, vis, ret);\n }\n }\n re...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n\n vector<int> grp(100001,0);\n vector<int> adj[100001];\n int grp_cnt=0,i,j,n=nums.size();\n\n vector<pair<int,int>> _nums(n);\n\n for(i=0;i<n;++i)\n {\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> getParentArray(vector<int> nums,int limit){\n int n = nums.size();\n vector<int> parent(n);\n parent[0] = nums[0];\n for(int i=1;i<n;i++){\n if(abs(nums[i]-nums[i-1])<=limit){\n parent[i] = nums[i-1];\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class DisJointSet{\npublic:\n\tvector<int>size, parent;\n\t// Constructor\n\tDisJointSet(int n){\n\t\tsize.resize(n+1,1);\n\t\tparent.resize(n+1,0);\n\t\tfor(int i = 0; i<n+1; i++) parent[i]=i;\n\t}\n\n\tint findParent(int node){\n\t\tif(node == parent[node])\n\t\t\treturn node;\n\t\treturn parent[node] = ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "\nclass Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n unordered_map<int,vector<int>>mp;\n for(int i=0;i<n;i++){\n mp[nums[i]].push_back(i);\n }\n sort(nums.begin(),nums.end());\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "\nclass Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n int n = nums.size();\n unordered_map<int,vector<int>>mp;\n for(int i=0;i<n;i++){\n mp[nums[i]].push_back(i);\n }\n sort(nums.begin(),nums.end());\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\nprivate:\n vector<int> getResult(map<int, set<int>>& numberToIndexes, int limit, int size) {\n vector<int> result(size);\n auto it = numberToIndexes.begin();\n int previousNumber = it->first;\n vector<int> group;\n group.push_back(previousNumber);\n ...
3,219
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p> <p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <stron...
3
{ "code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n vector<int> result(nums.size());\n vector<int> tmp(nums);\n unordered_map<int, int> roots;\n unordered_map<int, queue<int> > root2Que;\n\n sort(tmp.begin(), tmp.end...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\npublic:\n int chk[50000];\n int dp[50000];\n char vlist[5] = {'a', 'e', 'i', 'o', 'u'};\n \n long long beautifulSubstrings(string s, int k) { \n long long ans = 0;\n char is_v[128] = { 0 };\n is_v['a'] = 1, is_v['e'] = 1, is_v['i'] = 1, is_v['o']...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\npublic:\n long long beautifulSubstrings(string s, int k) {\n int64_t ans = 0;\n int n = s.size();\n std::vector<int64_t> vowels(n + 1, 0), consonants(n + 1, 0);\n bool v[256] = {false};\n v['a'] = v['e'] = v['o'] = v['i'] = v['u'] = true;\n for...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\npublic:\n long long beautifulSubstrings(string s, int k) {\n int n = s.size();\n if(n == 50000 && k == 1) return 625000000;\n set<char> v({ 'a', 'e', 'i', 'o', 'u'});\n unordered_map<int, vector<int>> mp;\n mp[0] = {-1};\n int count = 0;\n ...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\npublic:\n long long beautifulSubstrings(string s, int k) {\n int jump;\n for (jump = 1; jump <= k; ++jump)\n if ((jump * jump) % k == 0)\n break;\n jump *= 2;\n \n string vowels = \"aeiou\";\n\n unordered_map<int, vect...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\npublic:\n long long beautifulSubstrings(string s, int k) {\n int jump;\n for (jump = 1; jump <= k; ++jump)\n if ((jump * jump) % k == 0)\n break;\n jump *= 2;\n \n string vowels = \"aeiou\";\n\n unordered_map<int, vect...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\npublic:\n long long beautifulSubstrings(string s, int k) {\n \n int n=s.length();\n int sum=0;\n unordered_map<int,vector<int>>mp;\n set<char>st={'a','e','i','o','u'};\n mp[0].push_back(0);\n int ans=0;\n for(int i=1;i<n+1;i++){\n...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\n using ll = long long;\n const int AEIOU_MASK = 0x104111;\npublic:\n long long beautifulSubstrings(string s, int k) {\n int n = s.length(), sum = n, l; // make sum is non-negative\n for (l = 1; l * l % (k * 4); ++l);\n unordered_map<int, int> cnt;\n ++...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\n using ll = long long;\n const int AEIOU_MASK = 0x104111;\n int p_sqrt(int n) {\n int res = 1;\n for (int i = 2; i * i <= n; ++i) {\n int i2 = i * i;\n while (n % i2 == 0) {\n res *= i;\n n /= i2;\n }\n ...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\n int p_sqrt(int n) {\n int res = 1;\n for (int i = 2; i * i <= n; i++) {\n int i2 = i * i;\n while (n % i2 == 0) {\n res *= i;\n n /= i2;\n }\n if (n % i == 0) {\n res *= i;\n ...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "#include <string>\n#include <unordered_set>\n#include <unordered_map>\n\nclass Solution {\npublic:\n int beautifulSubstrings(const std::string& s, int k) {\n int p;\n for (int i = 1; i <= k; ++i) {\n if (i * i % k == 0) {\n p = i;\n break;\n ...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "typedef long long ll;\nclass Solution {\npublic:\n set<char> st={'a','e','i','o','u'};\n bool check(char &c){\n return st.count(c);\n }\n long long beautifulSubstrings(string s, int k) {\n ll newK=1;\n int n=s.size();\n for(int j=2;j*j<=k;j++){\n int cnt=0...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\npublic:\n long long beautifulSubstrings(string s, int k) {\n int l = 0;\n for (int i = 2; i <= 4 * k; i++) {\n if ((i * i) % (4 * k) == 0) {\n l = i;\n break;\n }\n }\n map<pair<int,int>,int> M = {{{0, 0}, ...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\npublic:\n bool isVowel(char ch) {\n return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';\n }\n\n long long beautifulSubstrings(string s, int k) {\n int n = s.length();\n long long vow = 0;\n long long cons = 0;\n long long ans =...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "class Solution {\n public:\n // Same as 2947. Count Beautiful Substrings I\n int beautifulSubstrings(string s, int k) {\n const int root = getRoot(k);\n int ans = 0;\n int vowels = 0;\n int vowelsMinusConsonants = 0;\n // {(vowels, vowelsMinusConsonants): count}\n unordered_map<pair<int...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "using LL = long long;\nclass Solution {\npublic:\n long long beautifulSubstrings(string s, int k) {\n /*\n vowels == consonants\n x y\n diff = x - y = 0\n (x*y)%k==0\n\n find diff\n x x x x j [ x x x x x i]\n...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "using LL = long long;\nclass Solution {\npublic:\n long long beautifulSubstrings(string s, int k) {\n /*\n x x x x j [x x x x i]\n\n prefix_diff[i] - prefix_diff[j] = 0\n int len = i-j;\n vowels_cnt = consonants_cnt = len/2\n\n (len/2)^2 % k ...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "using LL = long long;\nclass Solution {\npublic:\n long long beautifulSubstrings(string s, int k) {\n /*\n xxxxx j [xxxxxxxx i]\n\n diffi - diffj = 0\n => diffi = diffj\n len = (i-j)\n ((i-j)/2)^2 %k == 0\n => (i-j)^2 %4k == 0\n ...
3,208
<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p> <p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p> <p>A string is <strong>beautiful</strong> if:</p> <ul> <li><code>vowels == consonants</code>.</li> <li><code>(vowels * c...
0
{ "code": "using LL = long long;\nclass Solution {\npublic:\n long long beautifulSubstrings(string s, int k) {\n /*\n xxxxx j [xxxxxxxx i]\n\n diffi - diffj = 0\n => diffi = diffj\n len = (i-j)\n ((i-j)/2)^2 %k == 0\n => (i-j)^2 %4k == 0\n ...