id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n // Edge case\n if (nums.size() <= 1) return 0;\n\n // Lenghth of subarray\n int length = 0;\n\n // Tracking vector\n unordered_map<int, pair<int, int>> tracker;\n tracker[0] = {0, 0};\n\...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "//bit manipulation, sliding window, dp, prefix\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n unordered_map<int, pair<int, int>> m;\n\n for(int i=0; i<n; i++){\n if(nums[i]==0) nums[i]=-1;\n }\n\n m[0].first = -1...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "/*\n v\n 0 1 2 3 4 5 6 7 8\n 0 0 1 0 0 0 1 1 0\n \n -1 0 1 4 5 \nb = 5\nmax=2\n\n*/\nusing iter = vector<int>::iterator;\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int, iter> balan...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n vector<int> sums;\n int sum = 0;\n for (auto num : nums) {\n sum += num;\n sums.push_back(sum);\n }\n\n int max_length = nums.size();\n if (nums.size() % 2 != 0) {\n max_length -= 1;\n...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "class Solution {\npublic:\n bool isValid(vector<int>& integral_nums, int k) {\n for(int i = k;i < integral_nums.size();i++) {\n if (integral_nums[i] - integral_nums[i-k] == k/2) {\n return true;\n }\n }\n return false;\n }\n\n int findMaxLe...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int, int> mp;\n vector<int> pfx(nums.size());\n mp[0] = -1;\n int len = 0;\n\n for(auto i = 0; i < nums.size(); i++) {\n if(nums[i] == 0) {\n pfx[i] = (i > 0 ? ...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n=nums.size();\n vector<int> p(n,1);\n for(int i=0;i<n;i++) if(nums[i]==0) nums[i]=-1;\n p[0]=nums[0];\n for(int i=1;i<n;i++) p[i]=p[i-1]+nums[i];\n // for(auto i:p) cout<<i<<\" \";\n ...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n unordered_map<int, int> mp;\n vector<int> pfx(nums.size());\n mp[0] = -1;\n int len = 0;\n\n for(auto i = 0; i < nums.size(); i++) {\n if(nums[i] == 0) {\n pfx[i] = (i > 0 ? ...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int one=0,zero=0,ans=0,n=nums.size();\n vector<int> prefix(n,0);\n unordered_map<int,int> mp;\n for(int i=0;i<n;i++){\n if(nums[i]==1){\n one++;\n }else{\n ...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n vector<int> pre(n);\n if (nums[0] == 0) {\n pre[0] = -1;\n } else {\n pre[0] = 1;\n }\n for (int i = 1; i < n; i++) {\n if (nums[i] == 1)...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "class Solution {\npublic:\n vector<int>a;\n map<int,int>preSumMax;\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n a.resize(n);\n for(int i=0;i<n;i++){\n if(nums[i]==0)nums[i]=-1;\n a[i] = nums[i];\n }\n int maxLen = 0;\n ...
525
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
3
{ "code": "class Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n vector<int> pref(n);\n map<int, int> mp;\n int sum = 0;\n mp[0] = -1;\n int maxi = 0;\n\n for (int i = 0; i < n; i++) {\n if (nums[i] == 1)\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
0
{ "code": "class Solution {\npublic:\n int solve(int i,int n,int mask){\n if(i>n){\n return 1;\n } \n int cnt=0; \n for(int j=1;j<=n;j++){\n if((i%j==0||j%i==0) && ((mask&(1<<j))==(1<<j))){\n mask=mask^(1<<j);\n cnt+=solve(i+1,n,mas...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
0
{ "code": "class Solution {\npublic:\n bool seen[16] = {};\n int res = 0;\n int dfs(int n, int pos = 1) {\n if (pos > n) return res++;\n for (int i = 1; i <= n; i++) {\n if (!seen[i] && (i % pos == 0 || pos % i == 0)) {\n // marking i as seen\n seen[i] =...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "#include <vector>\n\nclass Solution {\npublic:\n int helper(std::vector<std::vector<int>>& vec, int n, std::vector<bool>& vis, int begin) {\n if (begin >= n) {\n return 1;\n }\n \n int ans = 0;\n for (int i = 0; i < vec[begin].size(); i++) {\n int...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> match;\n vector<bool> vis;\n int res = 0;\n\n void dfs(int u, int n){\n if(u == n + 1){\n res ++;\n return;\n }\n for(auto a: match[u]){\n if(!vis[a]){\n vis[a] = true;\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n void fun(int n,int ind,vector<int>&nums,int& cnt,vector<int>&v,vector<vector<int>>&a){\n if(ind==n){\n a.push_back(v);\n return ;\n }\n for(int i=ind;i<n;i++){\n \n if(nums[i]%(ind+1)==0 || (ind+1)%nums[i]==0){\...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int m;\n int dp[(1<<15)+1][17];\n int func(int i, int marks, unordered_map<int, set<int>>& mp){\n if(marks==((1<<m)-1)) return 1;\n if(i==(m+1)) return 0;\n\n if(dp[marks][i]!=-1) return dp[marks][i];\n\n int ans=0;\n ans=func(i+1, mar...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int num;\n int ans = 0;\n int dp[17][66000];\n int func(int index, int bitmask, int left){\n if (index == num + 1){\n if (left == 0) return 1;\n return 0;\n }\n if (dp[index][bitmask] != -1) return dp[index][bitmask];\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n long solve(vector<vector<int>>& dp, int mask, int idx, int n) {\n if (idx == n) return 1;\n if (dp[idx][mask] != -1) return dp[idx][mask];\n int a = 0;\n for(int i = 0; i < n; i++) {\n if (((i + 1) % (idx + 1) == 0 || (idx + 1) % (i + 1)...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "#include <bits/stdc++.h>\n#define ss second\n#define ff first\n#define pii pair<int,int>\n#define pb push_back\n#define ll long long\n#define vi vector<int>\n#define vvi vector<vi>\n#define vll vector<ll>\t\n#define pll pair<ll,ll>\n#define vpll vector<pll>\n#define vvll vector<vll>\n#define ld long double...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int dp[16][100000];\n int solve(int i,int n,int state)\n {\n if(i>n)\n {\n return 1;\n }\n if(dp[i][state]!=-1)\n return dp[i][state];\n int ans = 0;\n for(int j = 1;j<=n;j++)\n {\n if(((1<<j)...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int solve(int i, int n, unordered_set<int> &us){\n // base case\n if (i==0) return ((int)us.size()==n-1);\n\n int ans=0;\n // choices 1, 2, 3, 4, 5 ... n\n for(int j=1; j<n+1; j++){\n if (us.find(j)!=us.end()) continue;\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n void solve(int idx,int n,vector<int> &arr,vector<int> &vis,vector<vector<int>>&ans){\n if(idx==n+1){\n ans.push_back(arr);\n return ;\n }\n for(int i=0;i<n;i++){\n int num=i+1;\n if(vis[i+1]==0 && ( num%idx==0 |...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> ans;\n \n // Recursive function to place numbers in the arrangement\n void solve(int n, vector<int>& curr, vector<bool>& used, int i) {\n if (i > n) {\n ans.push_back(curr); // If all positions are filled, store the arrangement\...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> permutation;\n void get_permute(vector<int>& nums, vector<int>& curr, vector<bool>& freq) {\n if (curr.size() == nums.size()) {\n permutation.push_back(curr);\n return;\n }\n for (int i = 0; i < nums.size(); i+...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n\n void solve(set<vector<int>>& ans, vector<int>& cur, vector<int>& used, int i, int n) {\n if (i == n + 1)\n ans.insert(cur);\n else {\n for (int x = 1; x <= n; ++x)\n if (used[x - 1] == 0 && (x % i == 0 || i % x == 0)) {\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int solve(int index,int subset,int &n,vector<vector<int>>&dp){\n if(index>n)return 1;\n if(dp[index-1][subset]!=-1)return dp[index-1][subset];\n int ans=0;\n for(int i=1;i<=n;i++){\n if( (subset&(1<<i))==false && (i%index==0 || index%i=...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\nprivate:\n void swap( vector<int> & perms, int i ,int j ){\n int temp = perms[i];\n perms[i] = perms[j];\n perms[j] = temp;\n }\n void getCount(vector<int> & perms,vector<vector<int>> & ans,int pos,int n){\n if(pos >= perms.size()){\n ans.p...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\nprivate:\n void generate_perm(int n, int mask, vector<int>& v, int ct, vector<vector<int>>& perms){\n if(ct==n){\n perms.push_back(v);\n return;\n }\n\n for(int i=1;i<=n;i++){\n int curr_mask=1<<(i-1);\n if(!(mask&curr_ma...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "#include<vector> \n#include<math.h> \n\nclass Solution {\npublic: \n void resolve(int index,std::vector<int>& source,int range\n ,std::vector<bool>& marks,std::vector<std::vector<int>>& nested){\n if(index>range){\n std::vector<int> clone(source) ; \n nested.push_back(...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\n set<vector<int>>ans;\n void recursion(int n , vector<int>&temp, int ind){\n if(ind==n-1){\n if((ind+1)%temp[ind]==0)\n ans.insert(temp);\n \n return;\n }\n if(((ind+1)%temp[ind])==0 || (temp[ind]%(ind+1)==0) ){\n recurs...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\nvector<bool> mapp;\nmap<pair<int,vector<bool>>,int> dp;\nvector<int> possible[16];\n\n int cal(int index, int n) {\n\n if(index>n)\n return 1;\n if(dp.find({index,mapp})!=dp.end()){\n return dp[{index,mapp}];\n }\n int ret=0;\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\nvector<bool> mapp;\nmap<pair<int,vector<bool>>,int> dp;\nvector<int> possible[16];\n\n int cal(int index, int n) {\n\n if(index>n)\n return 1;\n if(dp.find({index,mapp})!=dp.end()){\n return dp[{index,mapp}];\n }\n int ret=0;\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\nint ans(vector<int>&vis,int idx,int n, map<pair<vector<int>,int>,int>&mp){\n if(idx>n) return 1;\n if(mp.find({vis,idx})!=mp.end()) return mp[{vis,idx}];\n int anss=0;\n for(int i=1;i<=n;i++){\n if(!vis[i]&&(idx%i==0||i%idx==0)){\n vis[i]=1;\n ans...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\nvector<int> mapp;\nmap<pair<int,vector<int>>,int> dp;\n\n int cal(int index, int n, vector<int>* possible) {\n\n if(index>n)\n return 1;\n if(dp.find({index,mapp})!=dp.end()){\n return dp[{index,mapp}];\n }\n int ret=0;\n fo...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int res;\n int countArrangement(int n) {\n res = 0;\n set<int> cur;\n dfs(1,cur,n);\n\n return res;\n }\n \n void dfs(int index, set<int>& cur, int n){\n if(index==n){\n res++;\n return ;\n }\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int countArrangement(int n) {\n vector<int> vis(n + 1);\n map<pair<int,vector<int>>,int> dp;\n return solve(n, 1, vis, dp);\n }\n\n int solve(int n, int idx, vector<int> vis, map<pair<int,vector<int>>,int>& dp){\n if(idx > n) {\n r...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\nint Helper(int i,int n,int mask,vector<vector<int>>& dp){\n if(i==n)return 1;\n if(dp[i][mask]!=-1)return dp[i][mask];\n int ans=0;\n for(int k=1;k<=n;k++){\n if(mask & (1<<k))continue;\n if(k%(i+1)==0 || (i+1)%k==0)\n {\n //int nm=mask...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int solve(int i, int& taken, int n, vector<vector<int>>&dp){\n if(i==n+1)\n return 1;\n if(dp[i][taken]!=-1) return dp[i][taken];\n int ans = 0;\n for(int j = 1;j<=n;j++){\n if(!(taken & (1<<j)) && (j%i==0 || i%j==0)){\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n bool isThere(int vis,int i)\n {\n if(((1<<i) & vis)!=0)\n {\n return 1;\n }\n return 0;\n }\n int fun(int ind,int n,int &vis,vector<vector<int>> &dp)\n {\n if(ind==n+1) return 1;\n\n if(dp[ind][vis]!=-1) r...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n bool check(int num, int i) {\n return (num % i == 0) || (i % num == 0);\n }\n\n int solve(int i, int n, int mask,vector<vector<int>>& dp) {\n if (i == n + 1) {\n return 1;\n }\n\n if(dp[i][mask] != -1) return dp[i][mask];\n\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int ans = 0;\n \n void rec(int level, set<int> &choices){\n if(level==0){\n ans++;\n return;\n }\n\n set<int> temp = choices;\n\n for(auto it: temp){\n if(it%level ==0 || level%it == 0){\n choic...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\n int ans;\n void rec(int n, set<int>& st) {\n if (n == 0) {\n ans++;\n return;\n }\n\n set<int> temp = st;\n for (auto x : temp) {\n if (x % n == 0|| n % x == 0) {\n st.erase(x);\n rec(n-1, st...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int goFind(int num, int mask, int &n, unordered_map<int, vector<int>> &mp, vector<vector<int>> &dp) {\n if(num==0) {\n return 1;\n }\n if(dp[num][mask] != -1) {\n return dp[num][mask];\n }\n\n int cnt = 0;\n for(...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n \n void helper(int ind ,int n, unordered_map<int,int>&mp, int &ans){\n if(ind>n){\n ans++;\n return;\n }\n for(int i=1;i<=n;i++){\n if((i%ind==0 || ind%i==0) && mp.find(i)==mp.end()){\n mp[i]=1;\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\nunordered_map<int,int> x;\n int countArrangement(int n) {\n vector<int> res;\n return findp(n,res,1);\n }\n int findp(int n,vector<int> &res,int idx)\n {\n if(idx>n) return 1;\n int a=0;\n for(int i=1;i<=n;i++)\n {\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int find(int pos , int n , unordered_map<int,int>& mp , vector<int>& ans){\n \n if(pos > n) {\n \n //for(auto x : ans) cout << x << \" \";\n cout << endl;\n \n return 1;\n }\n \n int cnt ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\nprivate:\n int f(int index, int& n, unordered_set<int>& seen){\n if(index == n) return 1;\n\n int ways = 0; \n for(int i = 1 ; i <= n ; i++){\n if(seen.find(i) != seen.end()) continue;\n\n if(i % (index+1) == 0 || (index+1) % i == 0){\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int solve(int index,int n,unordered_set<int>& st){\n if(index>=n){\n return 1;\n }\n int count=0;\n for(int i=1;i<=n;i++){\n if(st.find(i)==st.end() && ((index+1)%i==0 ||i%(index+1)==0)){\n st.insert(i);\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int solve(int index,int n,unordered_set<int>& st){\n if(index>=n){\n return 1;\n }\n int count=0;\n for(int i=1;i<=n;i++){\n if(st.find(i)==st.end() && ((index+1)%i==0 ||i%(index+1)==0)){\n st.insert(i);\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int countArrangement(int n) {\n\n std::unordered_set<int> visited {};\n\n std::function<int(int)> bt = [&](int i) {\n if (i == n + 1) {\n return 1;\n }\n int cnt = 0;\n for (int j = 1; j <= n; j++) {\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n void backtrack(vector<vector<int>>& matrix,int row,unordered_set<int>& usedCols,int& count){\n int n=matrix.size();\n if(row==n){\n count++;\n return;\n }\n for(int col=0;col<n;col++) {\n if(matrix[row][col]==1 && u...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n\n void per(int n,int &c, vector<bool> vis,int pos){\n if(pos>n){\n c++;\n return;\n }\n for(int i=1;i<=n;i++){\n if(!vis[i] and ( pos % i ==0 or i%pos ==0)){\n vis[i]=true;\n per(n,c,vis,pos+1...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution {\n public:\n int countArrangement(int n) {\n vector<bool> visited(n + 1, false);\n return calculate(visited, 1);\n }\n\n private:\n int calculate(vector<bool> visited, int nums) {\n if (nums >= visited.s...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int c=0;\n void func(int n,vector<bool> b,int ind){\n if(ind==n){\n c++;\n return;\n }\n for(int i=0;i<n;i++){\n if(!b[i]&&((i+1)%(ind+1)==0||(ind+1)%(i+1)==0)){\n b[i]=true;\n func(n,b,ind...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int solve(int i,int n,unordered_set<int> &vis){\n if(i>n){\n return 1;\n } \n int cnt=0; \n for(int j=1;j<=i;j++){\n if(i%j==0 && vis.count(j)==0){\n vis.insert(j);\n cnt+=solve(i+1,n,vis);\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int solve(int i,int n,unordered_set<int> &vis){\n if(i==n+1){\n return 1;\n } \n int cnt=0; \n for(int j=1;j<=i;j++){\n if(i%j==0 && vis.count(j)==0){\n //cout<<j<<\" \";\n vis.insert(j);\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int dfs(unordered_set <int> &s , int n , int pos){\n if (pos > n) return 1;\n int ans = 0;\n for (int i=1 ; i<=n ; i++){\n if (s.count(i)) continue;\n if (i%pos == 0){\n s.insert(i);\n ans+= dfs(s , n , ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int solve(int ind, int n, vector<int> &s, map<pair<vector<int>, int>, int> &dp)\n {\n if(ind > n) return 1;\n\n if(dp[{s, ind}] != 0) return dp[{s, ind}];\n\n int ans = 0;\n for(int i=1; i<=n; i++)\n {\n if(s[i] == 1 && (i % in...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n \n //cur_ind & mask -> for prev used values\n int bitmaskdp[20][1<<20]; \n int dfs(int ind, int mask , int n) {\n if(ind==n+1) {\n return 1; \n }\n if(bitmaskdp[ind][mask]!=-1) return bitmaskdp[ind][mask] ; \n int res=0; \n f...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int solve(int idx,int n,set<int>&st)\n {\n if(idx>n)\n {\n return 1;\n }\n int ans=0;\n for(int i=1;i<=n;i++)\n {\n if(st.find(i)==st.end() && (i%idx==0 || idx%i==0))\n {\n st.insert(i);\n ans+=...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n void ans(int n,set<int>&s,int &c,int j)\n {\n if(s.size()==n)\n {\n c++;\n return;\n }\n for(int i=1;i<=n;i++)\n {\n if(s.find(i)==s.end() && (i%j==0 || j%i==0))\n {\n s.insert(i)...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int countArrangement(int n) {\n vector<int> nums(n+1,-1);\n for(int i=1; i<=n; ++i) nums[i]=i;\n int ans=0;\n set<int> cset;\n backtrack(nums,1,cset,ans);\n return ans;\n }\n \n void backtrack(vector<int>& nums, int i, set<in...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int countArrangement(int n) {\n int ans=0;\n set<int> cset;\n backtrack(n,1,cset,ans);\n return ans;\n }\n \n void backtrack(int n, int i, set<int>& cset, int& ans){\n if(i>n){\n ans++;\n return;\n }\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n void helper(vector<int>& curr, int& res, int n, vector<int>& num, set<int>& visited){\n if(curr.size() == n){\n res++;\n return;\n }\n for(int i = 0; i < num.size(); i++){\n if(!visited.contains(num[i]) && ((curr.size()+1)...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int count;\n\n void solve(int idx, int n, vector<int>& temp, set<int>& vis) {\n if (idx == n+1) {\n count++;\n return;\n }\n\n for (int i = 1; i <= n; i++) {\n if (!vis.count(i) && (i%idx == 0 || idx%i == 0) ) {\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int solve(int n,int cnt,map<int,int>& mp)\n {\n \n if(mp.size()==n)\n return 1;\n if(mp.size()<cnt)\n return 0;\n int ans=0;\n for(int i=1;i<=n;i++)\n {\n if(i%(cnt+1)==0 || (cnt+1)%i==0)\n {\n ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int rec(int level,set<int>&ch,set<int>&pos){\n if(level==0){ \n return 1;\n }\n int ans=0;\n set<int>temp=ch;\n map<int,vector<int>>opt;\n for(auto v:pos){\n for(auto x:ch){\n if(!(v%x)||!(x%v)){\n...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int rec(int level,set<int>&ch,set<int>&pos){\n if(level==0){ \n return 1;\n }\n int ans=0;\n set<int>temp=ch;\n map<int,vector<int>>opt;\n for(auto v:pos){\n for(auto x:ch){\n if(!(v%x)||!(x%v)){\n...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int rec(vector<vector<int>>& dp, vector<bool>& vis, int i, int& n, int bit){\n \n if(dp[bit][i]!=-1){\n //cout<<dp[bit][i]<<\" \";\n return dp[bit][i];\n }\n int sm = 0;\n for(int u=1;u<=n;u++){\n if(!vis[u] ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int rec(vector<vector<int>>& dp, vector<bool>& vis, int i, int& n, int bit){\n \n if(dp[bit][i]!=-1){\n //cout<<dp[bit][i]<<\" \";\n return dp[bit][i];\n }\n int sm = 0;\n for(int u=1;u<=n;u++){\n if(!vis[u] ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int rec(int level,set<int>&ch,set<int>&pos){\n if(level==0){ \n return 1;\n }\n int ans=0;\n set<int>temp=ch;\n unordered_map<int,vector<int>>opt;\n for(auto v:pos){\n for(auto x:ch){\n if(!(v%x)||...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n map<pair<vector<int>, int>, int> dp;\n\n int f(vector<int> q, int ind, int n) {\n \n if (q.size() > n + 1 - ind) return 0;\n if (ind == n + 1) return 1;\n\n int ans = 0;\n for (auto ele : q) {\n if (ele % ind == 0 || ind % ele ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n int rec(int level,unordered_set<int>&ch,unordered_set<int>&pos){\n if(level==0){ \n return 1;\n }\n int ans=0;\n unordered_set<int>temp=ch;\n unordered_map<int,vector<int>>opt;\n for(auto v:pos){\n for(auto x:ch)...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n //vector<vector<int>>dp(18,vector<int>(100005,-1));\n int solve(int last , int mask , int curr,vector<vector<int>>&dp,int n){\n if(mask == (1<<n)-1)return 1;\n int &res = dp[last][mask];\n if(res!=-1)return res;\n res=0;\n for(int i=1;i<=n;i++){\n if(...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n void helper(vector<int> nums,int pst,int &cnt){\n \n if(pst==nums.size()){\n cnt++;\n return ;\n \n }\n for(int i=pst;i<nums.size();i++){\n swap(nums[pst],nums[i]);\n if((nums[pst]%(pst+1)==0) ||...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n void helper(vector<int> nums, int start, int& ans) {\n if (start == nums.size()) {\n ans++;\n return;\n }\n for (int i = start; i < nums.size(); i++) {\n swap(nums[start], nums[i]);\n if (nums[start] % (start + ...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\n \npublic:\nvoid helper(vector <int> nums, int start, int &ans){\n\tif(start==nums.size()){\n ans++;\n return;\n }\n for(int i=start;i<nums.size();i++){\n swap(nums[start],nums[i]);\n if(nums[start]%(start+1)==0 || (start+1)%num...
526
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
3
{ "code": "class Solution {\npublic:\n void helper(vector<int> nums, int start, int& ans) {\n if (start == nums.size()) {\n ans++;\n return;\n }\n for (int i = start; i < nums.size(); i++) {\n swap(nums[start], nums[i]);\n if (nums[start] % (start + ...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "#define kFactor 102\n\nclass Solution {\nprivate:\n vector<int> w_normalized;\n\n auto perform_sum(vector<int>& w) -> int {\n int value = 0;\n for (int i = 0; i < w.size(); i++) { value += w[i]; }\n return value;\n }\n\n auto normalize(vector<int>& w) -> vector<int> {\n ...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "#define kFactor 102\n\nclass Solution {\nprivate:\n vector<int> w_normalized;\n\n auto perform_sum(vector<int>& w) -> int {\n int value = 0;\n for (int i = 0; i < w.size(); i++) { value += w[i]; }\n return value;\n }\n\n auto normalize(vector<int>& w) -> vector<int> {\n ...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "class Solution {\nprivate:\n vector<int> &w_normalized;\n\n auto normalize(vector<int>& w) -> vector<int> {\n int sum = std::accumulate(w.begin(), w.end(), 0);\n vector<int> normalized = vector<int>();\n\n for (int i = 0; i < w.size(); i++) {\n const double& value = st...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "class Solution {\nprivate:\n vector<int> w_normalized;\n\n auto perform_sum(vector<int>& w) -> int {\n int value = 0;\n for (int i = 0; i < w.size(); i++) {\n value += w[i];\n }\n return value;\n }\n\n auto normalize(vector<int>& w) -> vector<int> {\n ...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "class Solution {\npublic:\n Solution(vector<int>& w) {\n record = vector<int>(w.size(), 0);\n for (int i = 0; i < w.size(); i++)\n {\n record[i] = sum;\n sum += w[i];\n }\n srand(time(NULL));\n }\n\n int pickIndex() {\n int target = r...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "class Solution {\npublic:\n Solution(vector<int>& w) : total_(0), prefix_sums_(w.size()) {\n for(int i = 0; i < w.size(); ++i) {\n total_ += w[i];\n prefix_sums_[i] = total_;\n }\n }\n \n int pickIndex() {\n int r = rand() % total_;\n int start ...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "class Solution {\npublic:\n int i = 0;\n vector<int> g;\n \n Solution(vector<int>& w) {\n int n = w.size();\n g.resize(n);\n for(int i=0;i<n;i++)\n {\n if(i == 0) \n {\n g[i] = w[i];\n continue;\n }\n ...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "class Solution {\n vector<int> arr;\npublic:\n Solution(vector<int>& w) {\n int sum = 0;\n for (int i = 1 ;i < w.size(); i++) {\n w[i] += w[i-1];\n }\n arr = w;\n }\n\n int pickIndex() {\n int len = arr.size();\n int val = rand()%arr[len-1];\...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "class Solution {\n public:\n Solution(vector<int>& w) : prefix(w.size()) {\n partial_sum(w.begin(), w.end(), prefix.begin());\n }\n\n int pickIndex() {\n const int target = rand() % prefix.back();\n int l = 0;\n int r = prefix.size();\n\n while (l < r) {\n const int m = (l + r) / 2;\...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "class Solution {\npublic: \n vector<int>weight;\n int sum =0;\n Solution(vector<int>& w) {\n int n = w.size();\n weight.resize(n);\n for(int i=0;i<w.size();i++){\n if(i==0){\n weight[i] = w[i];\n continue;\n }\n weight[i] = ...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
0
{ "code": "class Solution {\npublic:\n vector<int> w;\n Solution(vector<int>& w) {\n for(int i=1;i<w.size();i++) {\n w[i] += w[i-1];\n }\n srand(time(NULL));\n this->w = w;\n }\n \n int pickIndex() {\n double num = (double)rand()/RAND_MAX*(w[w.size()-1]);\n...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
1
{ "code": "class Solution {\npublic:\n Solution(vector<int>& w) {\n srand(time(NULL));\n int runsum = 0;\n for (int i = 0; i < w.size(); i++) {\n runsum += w[i];\n prefix.push_back(runsum);\n }\n }\n \n int pickIndex() {\n float rng = (float) rand()...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
1
{ "code": "class Solution {\npublic:\n Solution(vector<int>& w) {\n srand(time(NULL));\n int runsum = 0;\n for (int i = 0; i < w.size(); i++) {\n runsum += w[i];\n prefix.push_back(runsum);\n }\n }\n \n int pickIndex() {\n float rng = (float) rand()...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
1
{ "code": "class Solution {\npublic:\n vector<int> prefixSum;\n int n;\n Solution(vector<int>& w) {\n\n \n \n for(int i=1;i<w.size();i++)\n {\n \n \n w[i]+=w[i-1];\n }\n\n for(int i=0;i<w.size();i++)\n {\n prefixSum.push_back(w[i]);\n ...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
1
{ "code": "class Solution {\n vector<int> s;\npublic:\n Solution(vector<int>& w) {\n \n for(auto k: w) {\n if(s.empty()) s.push_back(k);\n else s.push_back(k+s.back());\n }\n }\n \n int pickIndex() {\n \n int r = rand() % s[s.size()-1];\n ...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
3
{ "code": "class Solution {\npublic:\n vector<int>prefixSum;\n int totalSum;\n Solution(vector<int>& w) {\n totalSum=0;\n for(int i=0;i<w.size();i++){\n totalSum+=w[i];\n prefixSum.push_back(totalSum);\n }\n }\n \n int pickIndex() {\n int randomNum =...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
3
{ "code": "class Solution {\npublic:\n vector<int> v;\n Solution(vector<int>& w) {\n v.push_back(w[0]);\n for(int i=1;i<w.size();i++)\n {\n v.push_back(v[i-1] + w[i]);\n }\n }\n \n int pickIndex() {\n int rd = rand()%v[v.size()-1];\n auto x = upper_b...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
3
{ "code": "class Solution {\npublic:\n vector<double> weights;\n Solution(vector<int>& v) {\n int sum=accumulate(v.begin(), v.end(), 0);\n double prev=0.0;\n\n for(int i=0;i<v.size();i++){\n double d=static_cast<double>(v[i])/static_cast<double>(sum);\n weights.push_back(prev+d);...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
3
{ "code": "class Solution {\nprivate:\n vector<int> nums;\n long long sum;\npublic:\n Solution(vector<int>& w) {\n nums.clear();\n sum = 0;\n for (auto n: w) {\n nums.push_back(n);\n sum += n;\n }\n }\n \n int pickIndex() {\n double x = (doubl...
912
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
3
{ "code": "class Solution {\nprivate:\n vector<double> table;\npublic:\n Solution(vector<int>& w)\n {\n int sum = 0;\n for(auto val: w)\n {\n sum += val;\n }\n double cur = 0;\n for(auto& val: w)\n {\n cur += (double)val/sum;\n ...