From 39145574e1834b570a282285a43557e6487338f5 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 27 Jul 2019 13:28:25 +0530 Subject: [PATCH 01/61] Add files via upload --- Arrays/KEEP SWAPPING 2.cpp | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Arrays/KEEP SWAPPING 2.cpp diff --git a/Arrays/KEEP SWAPPING 2.cpp b/Arrays/KEEP SWAPPING 2.cpp new file mode 100644 index 0000000..9efb2b8 --- /dev/null +++ b/Arrays/KEEP SWAPPING 2.cpp @@ -0,0 +1,63 @@ +#define mxn 100005 +#define mxnn 200005 +#define mod (ll)10000000 +#define ll long long +int a[21][100005]; +int temp[mxn]; +map m; +string Solution::solve(string A,string B, int C) +{ + m.clear(); + long long p10 = 1; + reverse(B.begin(),B.end()); + long long D = 0; + for(int i=0;i Date: Sat, 27 Jul 2019 13:28:54 +0530 Subject: [PATCH 02/61] Add files via upload --- Strings/Ugly Pairs.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Strings/Ugly Pairs.cpp diff --git a/Strings/Ugly Pairs.cpp b/Strings/Ugly Pairs.cpp new file mode 100644 index 0000000..cb9739a --- /dev/null +++ b/Strings/Ugly Pairs.cpp @@ -0,0 +1,31 @@ +int Solution::solve(string A) { + + string odd = ""; + string even = ""; + + for(int i = 0; i < A.length(); i++) + { + if((int)A[i] % 2 == 1) + { + odd = odd + A[i]; + } + else + { + even = even + A[i]; + } + } + + for(int i = 0; i < even.length(); i++) + { + for(int j = 0; j < odd.length(); j++) + { + if((((int)even[i] + 1) != (int)odd[j]) && (((int)even[i] - 1) != (int)odd[j])) + { + return 1; + } + + } + } + + return 0; +} From 5fe388191328421e7d68c0ec9f7df5f07f6cce84 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 27 Jul 2019 13:29:22 +0530 Subject: [PATCH 03/61] Add files via upload --- Math/LAST SUBSET PROBLEM.cpp | 54 ++++++++++++++++++++++++++++++++++++ Math/SUM OF SQUARES.cpp | 32 +++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 Math/LAST SUBSET PROBLEM.cpp create mode 100644 Math/SUM OF SQUARES.cpp diff --git a/Math/LAST SUBSET PROBLEM.cpp b/Math/LAST SUBSET PROBLEM.cpp new file mode 100644 index 0000000..08198b3 --- /dev/null +++ b/Math/LAST SUBSET PROBLEM.cpp @@ -0,0 +1,54 @@ +#define mod1 1000000007 +#define mod2 1000000006 +#define mxn 100005 +vector pr(mxn, true); +void seive() +{ + pr[0] = false; + pr[1] = false; + for(int i=2;i &A) +{ + seive(); + int n = A.size(); + long long val = powe(2LL, n-1LL, 1000000006LL); + long long ans = 1LL; + for(int i=0;i>=1; + } + return r; +} + +int solveit(vector &a){ + int n=a.size(); + long long multiple=modexp(2LL,1LL*(n-1)); + long long ans=0; + for(auto &it:a){ + assert(it>=1&&it<=INT_MAX); + long long p=1LL*it*it; + p%=mod; + long long temp=(1LL*p*multiple)%mod; + ans=(ans+temp); + while(ans>=mod) + ans-=mod; + } + return (int)(ans); +} + +int Solution::solve(vector &A){ + return solveit(A); +} \ No newline at end of file From 4dab10d04ea8f841e2b630fea8db86fbb3db4da7 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 27 Jul 2019 13:29:59 +0530 Subject: [PATCH 04/61] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bb23591..9ad959f 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ I have solved quite a number of problems from several topics. See the below tabl | Topic Name| Number of Problems Solved| | :--------: | :--------: | -| [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 63| +| [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 64| | [Backtracking](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Backtracking)| 20| | [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 18| | [Bit Manipulation](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Bit%20Manipulation)| 7| @@ -25,9 +25,9 @@ I have solved quite a number of problems from several topics. See the below tabl | [Hashing](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Hashing)| 26| | [Heaps And Maps](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Heaps%20and%20Maps)| 20| | [Linked Lists](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Linked%20Lists)| 22| -| [Math](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Math)| 43| +| [Math](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Math)| 45| | [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 25| -| [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 31| +| [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 32| | [Time Complexity](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Time%20Complexity)| 29| | [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 52| | [Two Pointers](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Two%20Pointers)| 21| From b79137b31adbe495807fd7ce5e323a85f7d61ad4 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 3 Aug 2019 20:14:22 +0530 Subject: [PATCH 05/61] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9ad959f..f6420bb 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ I have solved quite a number of problems from several topics. See the below tabl | Topic Name| Number of Problems Solved| | :--------: | :--------: | -| [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 64| -| [Backtracking](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Backtracking)| 20| +| [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 66| +| [Backtracking](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Backtracking)| 21| | [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 18| | [Bit Manipulation](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Bit%20Manipulation)| 7| | [Codersbit](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Codersbit)| 19| @@ -29,7 +29,7 @@ I have solved quite a number of problems from several topics. See the below tabl | [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 25| | [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 32| | [Time Complexity](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Time%20Complexity)| 29| -| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 52| +| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 53| | [Two Pointers](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Two%20Pointers)| 21| From b10ad0fdcae75bd10e9995b8d1870147da2010aa Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 3 Aug 2019 20:15:24 +0530 Subject: [PATCH 06/61] Add files via upload --- ...of an increasing subsequence of size 3.cpp | 38 +++++++++++++++++++ Arrays/Row with maximum number of ones.cpp | 17 +++++++++ 2 files changed, 55 insertions(+) create mode 100644 Arrays/Maximum product of an increasing subsequence of size 3.cpp create mode 100644 Arrays/Row with maximum number of ones.cpp diff --git a/Arrays/Maximum product of an increasing subsequence of size 3.cpp b/Arrays/Maximum product of an increasing subsequence of size 3.cpp new file mode 100644 index 0000000..a37284e --- /dev/null +++ b/Arrays/Maximum product of an increasing subsequence of size 3.cpp @@ -0,0 +1,38 @@ +const int mod = 1000000007; + + +int solveit(vector &a){ + int n=a.size(); + long long ans=-1; + long long smaller[n]; + set s; + memset(smaller,-1,sizeof smaller); + set::iterator it; + + for(int i=0; i0; --i){ + if(a[i]>suffixmax) + suffixmax=a[i]; + else if(smaller[i]!=-1){ + long long temp=1LL*smaller[i]*a[i]*suffixmax; + if(ans &A) { + return solveit(A); +} diff --git a/Arrays/Row with maximum number of ones.cpp b/Arrays/Row with maximum number of ones.cpp new file mode 100644 index 0000000..197a067 --- /dev/null +++ b/Arrays/Row with maximum number of ones.cpp @@ -0,0 +1,17 @@ +int solveit(vector > &a){ + int n=a.size(); + int ans=0; + int row =0,i,j; + for (i=0,j=n-1; i0;i++){ + while (a[i][j]==1&&j>=0){ + row = i; + j--; + } + } + return row; +} + +int Solution::solve(vector > &A) { + return solveit(A); + +} \ No newline at end of file From aa07b3c7ca12629f412745c9592103caa93beed0 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 3 Aug 2019 20:15:50 +0530 Subject: [PATCH 07/61] Add files via upload --- Binary Search/Ath Magical Number.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Binary Search/Ath Magical Number.cpp diff --git a/Binary Search/Ath Magical Number.cpp b/Binary Search/Ath Magical Number.cpp new file mode 100644 index 0000000..9620a2f --- /dev/null +++ b/Binary Search/Ath Magical Number.cpp @@ -0,0 +1,20 @@ +int solveit(int N, int A, int B) { + long long ab=(1LL*A*B)/__gcd(A,B); + long long l=1LL,r=(long long)(1e18),m,ans; + long long mod=1000000007; + while(l<=r){ + m=(l+r)>>1; + long long temp=m/A+m/B-m/ab; + if(temp>=N){ + ans=m; + r=m-1; + } + else + l=m+1; + } + return (int)(ans%mod); +} + +int Solution::solve(int A, int B, int C) { + return solveit(A,B,C); +} From 89335f902b2291937cd450f70a476554166ebcf8 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 3 Aug 2019 20:16:14 +0530 Subject: [PATCH 08/61] Add files via upload --- Trees/TOP VIEW.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Trees/TOP VIEW.cpp diff --git a/Trees/TOP VIEW.cpp b/Trees/TOP VIEW.cpp new file mode 100644 index 0000000..1f1a5f4 --- /dev/null +++ b/Trees/TOP VIEW.cpp @@ -0,0 +1,45 @@ +vector topview(TreeNode* root) +{ + vector ans; + if(root==NULL) + return ans; + queueq; + map m; + map l; + int hd=0; + l[root->val] = hd; + + q.push(root); + + while(q.size()) + { + hd=l[root->val]; + + if(m.count(hd)==0) + m[hd]=root->val; + if(root->left) + { + l[root->left->val] = hd-1LL; + q.push(root->left); + } + if(root->right) + { + l[root->right->val]=hd+1; + q.push(root->right); + } + q.pop(); + root=q.front(); + + } + + + for(auto i=m.begin();i!=m.end();i++) + { + ans.push_back(i->second); + } + return ans; +} + +vector Solution::solve(TreeNode* A) { + return topview(A); +} \ No newline at end of file From ed5d4b00bf12489a6afd641c39a0a08acd26d6ce Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sun, 11 Aug 2019 19:50:16 +0530 Subject: [PATCH 09/61] Add files via upload --- Math/ALL GCD PAIR.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Math/ALL GCD PAIR.cpp diff --git a/Math/ALL GCD PAIR.cpp b/Math/ALL GCD PAIR.cpp new file mode 100644 index 0000000..9359346 --- /dev/null +++ b/Math/ALL GCD PAIR.cpp @@ -0,0 +1,34 @@ +vector Solution::solve(vector &A) +{ + int n = A.size(); + sort(A.begin(), A.end()); + reverse(A.begin(), A.end()); + vector freq(A[0] + 1, 0); + for (int i=0;i B(n, 0); + int x, l = 0; + + for (int i = 0; i < n; i++) { + if (freq[A[i]] != 0) { + + B[l] = A[i]; + + freq[B[l]]--; + l++; + for (int j = 0; j < l; j++) { + if (i != j) { + + x = __gcd(A[i], B[j]); + + freq[x] -= 2; + } + } + } + } +return B; +} \ No newline at end of file From f0d06556a551341c29008cad076d3fa0798490b2 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sun, 11 Aug 2019 19:50:36 +0530 Subject: [PATCH 10/61] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f6420bb..12ae505 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ I have solved quite a number of problems from several topics. See the below tabl | [Hashing](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Hashing)| 26| | [Heaps And Maps](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Heaps%20and%20Maps)| 20| | [Linked Lists](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Linked%20Lists)| 22| -| [Math](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Math)| 45| +| [Math](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Math)| 46| | [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 25| | [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 32| | [Time Complexity](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Time%20Complexity)| 29| From 4209960f66ccbeed1cad8986354c1f077a9ec73a Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sun, 18 Aug 2019 20:34:50 +0530 Subject: [PATCH 11/61] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 12ae505..77694f6 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ I have solved quite a number of problems from several topics. See the below tabl | Topic Name| Number of Problems Solved| | :--------: | :--------: | -| [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 66| +| [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 67| | [Backtracking](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Backtracking)| 21| -| [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 18| +| [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 19| | [Bit Manipulation](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Bit%20Manipulation)| 7| | [Codersbit](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Codersbit)| 19| | [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 71| @@ -29,7 +29,7 @@ I have solved quite a number of problems from several topics. See the below tabl | [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 25| | [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 32| | [Time Complexity](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Time%20Complexity)| 29| -| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 53| +| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 54| | [Two Pointers](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Two%20Pointers)| 21| From c7cc5ccac66561f2504426fab59eb8ee402068f7 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sun, 18 Aug 2019 20:35:34 +0530 Subject: [PATCH 12/61] Add files via upload --- Arrays/Bob and Queries.cpp | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Arrays/Bob and Queries.cpp diff --git a/Arrays/Bob and Queries.cpp b/Arrays/Bob and Queries.cpp new file mode 100644 index 0000000..ba3975a --- /dev/null +++ b/Arrays/Bob and Queries.cpp @@ -0,0 +1,39 @@ +int pre[500010]; +int val[500010]; + +void ini(){ + for(int i=0;i<500010;++i) pre[i]=0; + for(int i=0;i<500010;++i) val[i]=0; +} + +void update(int idx,int x){ + for(;idx<500010;idx+=(idx&-idx)) + pre[idx]+=x; +} + +long long query(int idx){ + long long res=0; + for(;idx>0;idx-=(idx&-idx)){ + res += pre[idx]; + } + return res; +} + +vector Solution::solve(int A, vector > &B) { + int q= B.size(); + ini(); + vector ans; + for(int i=0;i Date: Sun, 18 Aug 2019 20:35:59 +0530 Subject: [PATCH 13/61] Add files via upload --- Binary Search/ADD OR NOT.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Binary Search/ADD OR NOT.cpp diff --git a/Binary Search/ADD OR NOT.cpp b/Binary Search/ADD OR NOT.cpp new file mode 100644 index 0000000..6bda12e --- /dev/null +++ b/Binary Search/ADD OR NOT.cpp @@ -0,0 +1,28 @@ +vector Solution::solve(vector &A, int B) { + sort(A.begin(), A.end()); + int n = A.size(); + int mx = INT_MIN; + int el = 0; + int req = 0; + int i=0,j=0; + while(j ans; + ans.push_back(mx); + ans.push_back(el); + return ans; +} From 6d6b7e7ac17030f3f5e18e2a8c5a8e1c082f718d Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sun, 18 Aug 2019 20:36:59 +0530 Subject: [PATCH 14/61] Add files via upload --- Trees/Power of 3.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Trees/Power of 3.cpp diff --git a/Trees/Power of 3.cpp b/Trees/Power of 3.cpp new file mode 100644 index 0000000..cd4262a --- /dev/null +++ b/Trees/Power of 3.cpp @@ -0,0 +1,83 @@ +#define f first +#define s second +#define debug1(x) cout< +#define pii pair +#define pb push_back +#define mp make_pair +#define mxn 100005 +#define mxnn 200005 +#define mod (ll)1000000007 +#define ll long long +int BIT[2][mxn]; +void update(int loc,int index,int val) +{ + for(int i = index;i0; index-=(index & (-index))) + { + sum += BIT[loc][index]; + } + return sum; +} +vector Solution::solve(string A, vector > &B) +{ + memset(BIT, 0, sizeof(BIT)); + vector ans; + int n = A.size(); + int q = B.size(); + // cout< Date: Sat, 24 Aug 2019 20:28:55 +0530 Subject: [PATCH 15/61] Add files via upload --- Trees/GREEDY PICK.cpp | 24 ++++++++++++++ Trees/K DISTANCE.cpp | 27 +++++++++++++++ Trees/Trees and Sum of Distances.cpp | 49 ++++++++++++++++++++++++++++ Trees/Vertex Cover Problem.cpp | 29 ++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 Trees/GREEDY PICK.cpp create mode 100644 Trees/K DISTANCE.cpp create mode 100644 Trees/Trees and Sum of Distances.cpp create mode 100644 Trees/Vertex Cover Problem.cpp diff --git a/Trees/GREEDY PICK.cpp b/Trees/GREEDY PICK.cpp new file mode 100644 index 0000000..9b0c340 --- /dev/null +++ b/Trees/GREEDY PICK.cpp @@ -0,0 +1,24 @@ +#define ll long long +#define mod 1000000007 +vector robSub(TreeNode* root) { + if (root == NULL) { + return vector(2,0LL); + } + + vector left = robSub(root->left); + vector right = robSub(root->right); + + vector res(2,0LL); + res[0] = max(left[0], left[1]) + max(right[0], right[1]); + res[1] = root->val + left[0] + right[0]; + res[0]%=mod; + res[1]%=mod; + vector ans(2); + ans[0] = res[0]; + ans[1] = res[1]; + return ans; +} +int Solution::solve(TreeNode* A) { + vector res = robSub(A); + return max(res[0], res[1]); +} diff --git a/Trees/K DISTANCE.cpp b/Trees/K DISTANCE.cpp new file mode 100644 index 0000000..3bba7c5 --- /dev/null +++ b/Trees/K DISTANCE.cpp @@ -0,0 +1,27 @@ +int BIT[200000]; +#define ni 200000 +void update(int x, int delta) +{ + for(; x <= ni; x += x&-x) + BIT[x] += delta; +} +int query(int x) +{ + int sum = 0; + for(; x > 0; x -= x&-x) + sum += BIT[x]; + return sum; +} +#define mod 1000000007 +int Solution::solve(TreeNode* A, int B) { + if(A == NULL) + { + return 0; + } + + update(A->val,1); + int val1 = solve(A->left,B); + int val2 = solve(A->right,B); + update(A->val, -1); + return (val1+val2+query(min(A->val+B,100000)) - query(max(A->val-B-1,0)))%mod; +} diff --git a/Trees/Trees and Sum of Distances.cpp b/Trees/Trees and Sum of Distances.cpp new file mode 100644 index 0000000..b1aa0ca --- /dev/null +++ b/Trees/Trees and Sum of Distances.cpp @@ -0,0 +1,49 @@ +const int MAXN = 1e4 + 10; +vector graph[MAXN], res, cnt; +int n; + +void reset(){ + for(int i = 0; i < MAXN; i++) + graph[i].clear(); + res.resize(n); + cnt.resize(n); + for(int i = 0; i < n; i++){ + res[i] = 0; + cnt[i] = 1; + } +} + +void dfs(int node, int par){ + for(int u : graph[node]){ + if(u != par){ + dfs(u, node); + cnt[node] += cnt[u]; + res[node] += res[u] + cnt[u]; + } + } +} + +void dfs2(int node, int par){ + for(int u : graph[node]){ + if(u != par){ + res[u] = res[node] - cnt[u] + n - cnt[u]; + dfs2(u, node); + } + } +} + +vector solveit(vector > &A){ + n = A.size()+1; + reset(); + for(int i = 0; i < A.size(); i++){ + graph[A[i][0]].push_back(A[i][1]); + graph[A[i][1]].push_back(A[i][0]); + } + dfs(0, -1); + dfs2(0, -1); + return res; +} +vector Solution::solve(vector > &A) { + return solveit(A); +} + diff --git a/Trees/Vertex Cover Problem.cpp b/Trees/Vertex Cover Problem.cpp new file mode 100644 index 0000000..50d0577 --- /dev/null +++ b/Trees/Vertex Cover Problem.cpp @@ -0,0 +1,29 @@ +void trav(TreeNode *A) + { + if(A==NULL) + return ; + A->val=0; + trav(A->left); + trav(A->right); + } + int v(TreeNode* A) + { + if(A==NULL) + return 0; + if(A->right==NULL && A->left==NULL) + return 0; + if(A->val!=0) + return A->val; + int s1=1+v(A->left)+v(A->right); + int s2=0; + if(A->left!=NULL) + s2+=1+v(A->left->left)+v(A->left->right); + if(A->right!=NULL) + s2+=1+v(A->right->left)+v(A->right->right); + return A->val=min(s2,s1); + } +int Solution::solve(TreeNode* A) { + int count=0; + trav(A); + return v(A); +} \ No newline at end of file From 341c882151df0eeb4487d35c277ae28a917214fb Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 24 Aug 2019 20:29:56 +0530 Subject: [PATCH 16/61] Add files via upload --- Time Complexity/XOR TRIPLETS.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Time Complexity/XOR TRIPLETS.cpp diff --git a/Time Complexity/XOR TRIPLETS.cpp b/Time Complexity/XOR TRIPLETS.cpp new file mode 100644 index 0000000..c854e76 --- /dev/null +++ b/Time Complexity/XOR TRIPLETS.cpp @@ -0,0 +1,29 @@ +int Solution::solve(vector &A) +{ +int n = A.size(); +long long xorval = 0; +map> m; +pair p; +p.first = -1; +p.second = 1; +m[xorval] = p; +long long ans = 0; +for (int i = 0; i < n; i++) { +xorval ^= A[i]; +if (m.find(xorval) == m.end()) { +pair p; + p.first = i; + p.second = 1; + m[xorval] = p; +} else { +pair p = m[xorval]; +long long x = p.first; +long long y = p.second; +ans += i * y - x - y; +p.first += i; +p.second++; +m[xorval] = p; +} +} +return ans; +} From 9b4d0e64ef0e9c77b190c2a9379b33dae2266904 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 24 Aug 2019 20:30:17 +0530 Subject: [PATCH 17/61] Add files via upload --- .../Subarrays with B Different Integers.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Heaps and Maps/Subarrays with B Different Integers.cpp diff --git a/Heaps and Maps/Subarrays with B Different Integers.cpp b/Heaps and Maps/Subarrays with B Different Integers.cpp new file mode 100644 index 0000000..a207f94 --- /dev/null +++ b/Heaps and Maps/Subarrays with B Different Integers.cpp @@ -0,0 +1,42 @@ +unordered_map mp; + +void add(int x){ + ++mp[x]; +} + +void removee(int x){ + --mp[x]; + if(mp[x]==0) + mp.erase(x); +} + +int solveit(vector& A, int K) { + mp.clear(); + int n=A.size(); + if(n &A, int B) { + return solveit(A,B); +} \ No newline at end of file From 8eac328e032ef0bf77c4a7b9415c02ea63257b22 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 7 Sep 2019 13:17:26 +0530 Subject: [PATCH 18/61] Add files via upload --- Trees/LCA of array.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Trees/LCA of array.cpp diff --git a/Trees/LCA of array.cpp b/Trees/LCA of array.cpp new file mode 100644 index 0000000..a5571ca --- /dev/null +++ b/Trees/LCA of array.cpp @@ -0,0 +1,72 @@ +pair segtree[800008]; +int in[100001],out[100001],vis[100001]; +vector gr[100001]; +vector > euler; +void dfs(int i=0,int depth=0) +{ + if(vis[i]) + return; + vis[i]=1; + in[i]=euler.size(); + euler.push_back({depth,i}); + for(int j:gr[i]) + { + if(vis[j]==0) + { + dfs(j,depth+1); + euler.push_back({depth,i}); + } + } + out[i]=euler.size()-1; +} + +void build(int node,int tl,int tr) +{ + if(tl==tr) + { + segtree[node]=euler[tl]; + return; + } + int tm=(tl+tr)/2; + build(2*node,tl,tm); + build(2*node+1,tm+1,tr); + segtree[node]=min(segtree[2*node],segtree[2*node+1]); +} + +pair query(int node,int tl,int tr,int l,int r) +{ + if(l==tl and r==tr) + return segtree[node]; + int tm=(tl+tr)/2; + if(tm>=r) + return query(2*node,tl,tm,l,r); + if(tm &A, vector &B) +{ + int n=A.size(); + for(int i=0;i<=n;i++) + gr[i].clear(); + euler.clear(); + memset(vis,0,sizeof vis); + for(int i=0;i &A, vector &B) { + return solveit(A,B); +} From fba46e2352e46a2ba914b10e059df25a496a7163 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 7 Sep 2019 13:18:07 +0530 Subject: [PATCH 19/61] Add files via upload --- .../Height of every node in a tree.cpp | 45 +++++++++++++++++++ ...Maximise the coins by popping balloons.cpp | 15 +++++++ ...robablity of knight inside chess board.cpp | 38 ++++++++++++++++ Dynamic Programming/RECTANGLE SUM.cpp | 39 ++++++++++++++++ .../Travelling Saleman Problem.cpp | 21 +++++++++ ...m sum of array with no repeating digit.cpp | 42 +++++++++++++++++ 6 files changed, 200 insertions(+) create mode 100644 Dynamic Programming/Height of every node in a tree.cpp create mode 100644 Dynamic Programming/Maximise the coins by popping balloons.cpp create mode 100644 Dynamic Programming/Probablity of knight inside chess board.cpp create mode 100644 Dynamic Programming/RECTANGLE SUM.cpp create mode 100644 Dynamic Programming/Travelling Saleman Problem.cpp create mode 100644 Dynamic Programming/maximum sum of array with no repeating digit.cpp diff --git a/Dynamic Programming/Height of every node in a tree.cpp b/Dynamic Programming/Height of every node in a tree.cpp new file mode 100644 index 0000000..0d87caf --- /dev/null +++ b/Dynamic Programming/Height of every node in a tree.cpp @@ -0,0 +1,45 @@ +int in[100001],out[100001]; +void dfs1(vector adj[],int u,int p){ + in[u]=0; + for(int child : adj[u]){ + if(child==p)continue; + dfs1(adj,child,u); + in[u]=max(in[u],1+in[child]); + } +} +void dfs2(vector adj[],int u,int p){ + int mx1=-1,mx2=-1; + for(int child : adj[u]){ + if(child==p)continue; + if(in[child]>=mx1){ + mx2=mx1; + mx1=in[child]; + } + else if(in[child]>mx2){ + mx2=in[child]; + } + } + for(int child : adj[u]){ + if(child==p)continue; + int l=mx1; + if(mx1==in[child]){ + l=mx2; + } + out[child]=1+max(out[u],1+l); + dfs2(adj,child,u); + } +} +vector Solution::solve(int A, vector > &B) { + vector adj[A+1]; + for(int i=0;i ans(A); + for(int i=0;i &A) { + int n=A.size(); + A.insert(A.begin(),1); + A.push_back(1); + vector> dp(n+2,vector(n+2,0)); + for(int len=1;len<=n;len++){ + for(int i=1;i<=n-len+1;i++){ + int j=i+len-1; + for(int k=i;k<=j;k++){ + dp[i][j]=max(dp[i][j],dp[i][k-1]+A[i-1]*A[k]*A[j+1]+dp[k+1][j]); + } + } + } + return dp[1][n]; +} diff --git a/Dynamic Programming/Probablity of knight inside chess board.cpp b/Dynamic Programming/Probablity of knight inside chess board.cpp new file mode 100644 index 0000000..f61627f --- /dev/null +++ b/Dynamic Programming/Probablity of knight inside chess board.cpp @@ -0,0 +1,38 @@ +const int MOD=1e8+7; + +int solve(int A,int B,int C,int D) +{ + long long dp[A+4][A+4][2]; + memset(dp,0,sizeof dp); + dp[C+1][D+1][0]=1;int ans=0; + for(int k=1;k<=B;k++) + { + for(int i=2;i<=A+1;i++) + { + for(int j=2;j<=A+1;j++) + { + dp[i][j][1]=12500001LL*(dp[i-2][j-1][0]+dp[i-1][j-2][0]+dp[i+1][j-2][0]+dp[i+2][j-1][0]+dp[i-1][j+2][0]+dp[i-2][j+1][0]+dp[i+2][j+1][0]+dp[i+1][j+2][0]); + dp[i][j][1]%=MOD; + } + } + for(int i=2;i<=A+1;i++) + { + for(int j=2;j<=A+1;j++) + { + dp[i][j][0]=dp[i][j][1]; + } + } + } + for(int i=2;i<=A+1;i++) + { + for(int j=2;j<=A+1;j++) + { + ans+=dp[i][j][0]; + ans%=MOD; + } + } + return ans; +} +int Solution::solve(int A, int B, int C, int D) { + return ::solve(A,B,C,D); +} diff --git a/Dynamic Programming/RECTANGLE SUM.cpp b/Dynamic Programming/RECTANGLE SUM.cpp new file mode 100644 index 0000000..c3b9675 --- /dev/null +++ b/Dynamic Programming/RECTANGLE SUM.cpp @@ -0,0 +1,39 @@ +int Solution::solve(vector > &A) { + for(int i=0;ians){ + ans=max1; + } + if(max1<0){ + max1=0; + } + } + else{ + max1=max1+A[k][j]-A[k][i-1]; + if(max1>ans){ + ans=max1; + } + if(max1<0){ + max1=0; + } + } + } + } + } + if(ans<0){ + return 0; + } + + return ans; + +} \ No newline at end of file diff --git a/Dynamic Programming/Travelling Saleman Problem.cpp b/Dynamic Programming/Travelling Saleman Problem.cpp new file mode 100644 index 0000000..9654a99 --- /dev/null +++ b/Dynamic Programming/Travelling Saleman Problem.cpp @@ -0,0 +1,21 @@ +int dp[10][(1<<10)]; +int minCost(vector > &B,int A,int u,int mask){ + if(mask==0){ + return B[u][0]; + } + int ans=INT_MAX; + if(dp[u][mask]!=-1)return dp[u][mask]; + for(int i=0;i > &B) { + if(A==1)return 0; + for(int i=0;i<10;i++){ + for(int j=0;j<(1<<10);j++)dp[i][j]=-1; + } + return minCost(B,A,0,((1< &A) +{ + int sum[1024] = {0}; + int num; + + int n = A.size(); + + for(int i = 0; i < n; i++) + { + num = A[i]; + int nm = 0; + while(num) + { + nm |= (1 << (num % 10)); + num /= 10; + } + sum[nm] = max(sum[nm], A[i]); + } + + for(int i = 0; i < n; i++) + { + num = A[i]; + int nm = 0; + while(num) + { + nm |= (1 << (num % 10)); + num /= 10; + } + + for(int j = 0; j < 1024; j++) + { + if((nm & j) == 0) + sum[nm + j] = max(sum[nm + j], sum[nm] + sum[j]); + } + } + + int mx = 0; + + for(int i = 0; i < 1024; i++) + mx = max(mx, sum[i]); + return mx; +} From 5a2cee7aeddc5dd306c65db57edca85a0eb60df9 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 7 Sep 2019 13:18:36 +0530 Subject: [PATCH 20/61] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 77694f6..3f4ce9d 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ I have solved quite a number of problems from several topics. See the below tabl | [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 19| | [Bit Manipulation](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Bit%20Manipulation)| 7| | [Codersbit](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Codersbit)| 19| -| [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 71| +| [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 77| | [Graphs](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Graphs)| 29| | [Greedy](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Greedy)| 13| | [Hashing](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Hashing)| 26| @@ -29,7 +29,7 @@ I have solved quite a number of problems from several topics. See the below tabl | [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 25| | [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 32| | [Time Complexity](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Time%20Complexity)| 29| -| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 54| +| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 55| | [Two Pointers](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Two%20Pointers)| 21| From 301ab15f5c1aa9214cc0721ae07dc5ba6324e454 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 7 Sep 2019 13:53:38 +0530 Subject: [PATCH 21/61] Add files via upload --- Dynamic Programming/Text Justification.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Dynamic Programming/Text Justification.cpp diff --git a/Dynamic Programming/Text Justification.cpp b/Dynamic Programming/Text Justification.cpp new file mode 100644 index 0000000..10f008a --- /dev/null +++ b/Dynamic Programming/Text Justification.cpp @@ -0,0 +1,18 @@ +vector Solution::solve(vector &words, int L) +{ + vector res; + for(int i = 0, k, l; i < words.size(); i += k) { + for(k = l = 0; i + k < words.size() and l + words[i+k].size() <= L - k; k++) { + l += words[i+k].size(); + } + string tmp = words[i]; + for(int j = 0; j < k - 1; j++) { + if(i + k >= words.size()) tmp += "#"; + else tmp += string((L - l) / (k - 1) + (j < (L - l) % (k - 1)), '#'); + tmp += words[i+j+1]; + } + tmp += string(L - tmp.size(), '#'); + res.push_back(tmp); + } + return res; +} From f75332f38773b91c26a82309f242ec511f4db691 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 7 Sep 2019 13:54:02 +0530 Subject: [PATCH 22/61] Add files via upload --- Time Complexity/Arithmetic Subsequences.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Time Complexity/Arithmetic Subsequences.cpp diff --git a/Time Complexity/Arithmetic Subsequences.cpp b/Time Complexity/Arithmetic Subsequences.cpp new file mode 100644 index 0000000..3640f05 --- /dev/null +++ b/Time Complexity/Arithmetic Subsequences.cpp @@ -0,0 +1,19 @@ +int Solution::solve(vector &A) +{ + if(A.empty()) return 0; + vector> dp(A.size());//[index, [difference, count]] + + int res = 0; + for(int i = 0; i < A.size(); ++i){ + for(int j = 0; j < i; ++j){ + if((long)A[i] - (long)A[j] > INT_MAX || (long)A[i] - (long)A[j] < INT_MIN) continue;// do not ignore this step, it will help you save time & space to pass OJ. + int dif = A[i] - A[j]; + dp[i][dif] += 1; + if(dp[j].find(dif) != dp[j].end()){ + dp[i][dif] += dp[j][dif]; + res += dp[j][dif]; + } + } + } + return res; +} From a490e6a77e0a7a8a74bc5c7efa3f55a6852a2472 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 7 Sep 2019 13:54:34 +0530 Subject: [PATCH 23/61] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3f4ce9d..0ad48a3 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ I have solved quite a number of problems from several topics. See the below tabl | [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 19| | [Bit Manipulation](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Bit%20Manipulation)| 7| | [Codersbit](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Codersbit)| 19| -| [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 77| +| [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 78| | [Graphs](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Graphs)| 29| | [Greedy](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Greedy)| 13| | [Hashing](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Hashing)| 26| @@ -28,7 +28,7 @@ I have solved quite a number of problems from several topics. See the below tabl | [Math](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Math)| 46| | [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 25| | [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 32| -| [Time Complexity](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Time%20Complexity)| 29| +| [Time Complexity](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Time%20Complexity)| 30| | [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 55| | [Two Pointers](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Two%20Pointers)| 21| From 9912bdf3f5c0c7f199773468956928620c63ae89 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Thu, 26 Sep 2019 20:35:31 +0530 Subject: [PATCH 24/61] Update README.md --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0ad48a3..1879bac 100644 --- a/README.md +++ b/README.md @@ -14,23 +14,23 @@ I have solved quite a number of problems from several topics. See the below tabl | Topic Name| Number of Problems Solved| | :--------: | :--------: | -| [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 67| +| [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 68| | [Backtracking](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Backtracking)| 21| | [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 19| -| [Bit Manipulation](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Bit%20Manipulation)| 7| +| [Bit Manipulation](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Bit%20Manipulation)| 8| | [Codersbit](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Codersbit)| 19| -| [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 78| -| [Graphs](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Graphs)| 29| +| [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 82| +| [Graphs](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Graphs)| 31| | [Greedy](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Greedy)| 13| | [Hashing](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Hashing)| 26| -| [Heaps And Maps](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Heaps%20and%20Maps)| 20| +| [Heaps And Maps](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Heaps%20and%20Maps)| 21| | [Linked Lists](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Linked%20Lists)| 22| -| [Math](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Math)| 46| -| [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 25| +| [Math](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Math)| 49| +| [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 26| | [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 32| | [Time Complexity](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Time%20Complexity)| 30| -| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 55| -| [Two Pointers](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Two%20Pointers)| 21| +| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 59| +| [Two Pointers](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Two%20Pointers)| 22| ## How to contribute? From 73f9b7504b45ba710658d45af0434cef33419efa Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Thu, 26 Sep 2019 20:36:47 +0530 Subject: [PATCH 25/61] Add files via upload --- Arrays/Into Blocks.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Arrays/Into Blocks.cpp diff --git a/Arrays/Into Blocks.cpp b/Arrays/Into Blocks.cpp new file mode 100644 index 0000000..5b250a3 --- /dev/null +++ b/Arrays/Into Blocks.cpp @@ -0,0 +1,25 @@ +int Solution::solve(vector &a) { + vectorcnt(200005,0); + vectorr(200005,0); + int last=0; + int maxx=0; + int ans=0; + int most=0; + + for(int i = 0;i Date: Thu, 26 Sep 2019 20:38:20 +0530 Subject: [PATCH 26/61] Add files via upload --- Bit Manipulation/SUBARRAY OR.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Bit Manipulation/SUBARRAY OR.cpp diff --git a/Bit Manipulation/SUBARRAY OR.cpp b/Bit Manipulation/SUBARRAY OR.cpp new file mode 100644 index 0000000..f1b14a3 --- /dev/null +++ b/Bit Manipulation/SUBARRAY OR.cpp @@ -0,0 +1,31 @@ +int mod = 1000000007; + +int Solution::solve(vector &A) { + + int n = A.size(); + vector p(n); + vector> v (32,p); + int index=n; + for(int i=0; i<32; i++) { + index=n; + for(int j=n-1; j>=0; j--) { + int t = A[j]&(1<<(i)); + if(t!=0) { + index=j; + } + v[i][j]=index; + } + } + + long long sum=0; + for(int i=0; i<32; i++) { + long long s=0; + for(int j=0; j Date: Thu, 26 Sep 2019 20:38:53 +0530 Subject: [PATCH 27/61] Add files via upload --- Dynamic Programming/Jesse and two string.cpp | 85 +++++++++++++++++++ .../LONGEST COMMON SUBSTRING.cpp | 20 +++++ .../Maximum coin in a tree.cpp | 39 +++++++++ Dynamic Programming/REDUCTION.cpp | 20 +++++ 4 files changed, 164 insertions(+) create mode 100644 Dynamic Programming/Jesse and two string.cpp create mode 100644 Dynamic Programming/LONGEST COMMON SUBSTRING.cpp create mode 100644 Dynamic Programming/Maximum coin in a tree.cpp create mode 100644 Dynamic Programming/REDUCTION.cpp diff --git a/Dynamic Programming/Jesse and two string.cpp b/Dynamic Programming/Jesse and two string.cpp new file mode 100644 index 0000000..19d2232 --- /dev/null +++ b/Dynamic Programming/Jesse and two string.cpp @@ -0,0 +1,85 @@ +int dp[1000][1000],vis[1000][1000]; +string str; +int mark[1000]; +void markfun(int i,int j) +{ + if(i>j or vis[i][j]) + return; + if(i==j) + { + mark[i]=1; + return; + } + vis[i][j]=1; + if(str[i]==str[j]) + { + markfun(i+1,j-1); + return; + } + if(dp[i][j]==dp[i][j-1]) + { + markfun(i,j-1); + } + if(dp[i][j]==dp[i+1][j]) + { + markfun(i+1,j); + } +} +int fun(int i,int j) +{ + if(i>j) + return 0; + if(dp[i][j]>=0) + return dp[i][j]; + if(i==j) + return dp[i][j]=1; + if(str[i]==str[j]) + dp[i][j]=2+fun(1+i,j-1); + else + dp[i][j]=max(fun(i+1,j),fun(i,j-1)); + return dp[i][j]; +} + +int solveit(string A,string B) +{ + int ans1,ans2; + int charac1[26],charac2[26]; + memset(charac1,0,sizeof charac1); + memset(charac2,0,sizeof charac2); + str=A; + memset(dp,-1,sizeof dp); + memset(mark,0,sizeof mark); + memset(vis,0,sizeof vis); + ans1=fun(0,str.length()-1); + markfun(0,str.length()-1); + for(int i=0;i gr[50001]; +int ans[2][50001],vis[50001]; + +void dfs(int n,vector &B) +{ + if(vis[n]) + return; + vis[n]=1; + ans[0][n]=B[n-1]; + for(int v:gr[n]) + { + if(vis[v]==0) + { + dfs(v,B); + ans[0][n]+=ans[1][v]; + ans[1][n]+=max(ans[0][v],ans[1][v]); + } + } +} + +int solveit(vector &A, vector &B) +{ + int n=A.size(); + for(int i=0;i<=n;i++) + gr[i].clear(); + for(int i=1;i &A, vector &B) { + return solveit(A,B); +} + diff --git a/Dynamic Programming/REDUCTION.cpp b/Dynamic Programming/REDUCTION.cpp new file mode 100644 index 0000000..b911ed2 --- /dev/null +++ b/Dynamic Programming/REDUCTION.cpp @@ -0,0 +1,20 @@ +int Solution::solve(vector &A, int B) +{ + int N = (int)A.size(); + int K = B; + if((N - 1) % (K - 1)) return -1; + + vector sum(N + 1, 0); + for(int i = 0; i < N; i++) sum[i + 1] = sum[i] + A[i]; + + vector > dp(N + 1, vector(N, 0)); + for(int l = K; l <= N; l++) + for(int i = 0; i + l <= N; i++) + { + dp[l][i] = 10000; + for(int k = 1; k < l; k += K - 1) + dp[l][i] = min(dp[l][i], dp[k][i] + dp[l - k][i + k]); + if((l - 1) % (K - 1) == 0) dp[l][i] += sum[i + l] - sum[i]; + } + return dp[N][0]; +} \ No newline at end of file From c835f85825827caf017b4145eb15f93ddb291d6c Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Thu, 26 Sep 2019 20:39:16 +0530 Subject: [PATCH 28/61] Add files via upload --- Graphs/Cows and snacks.cpp | 26 +++++++++++++++++++++++ Graphs/Roads and Libraries.cpp | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 Graphs/Cows and snacks.cpp create mode 100644 Graphs/Roads and Libraries.cpp diff --git a/Graphs/Cows and snacks.cpp b/Graphs/Cows and snacks.cpp new file mode 100644 index 0000000..fd1b549 --- /dev/null +++ b/Graphs/Cows and snacks.cpp @@ -0,0 +1,26 @@ +int dsu[100001]; +int find(int x) +{ + while(x!=dsu[x]) + { + dsu[x]=dsu[dsu[x]]; + x=dsu[x]; + } + return x; +} + +int solveit(int A,vector > &B) +{ + int ans=0; + for(int i=0;i<=A;i++) + dsu[i]=i; + for(auto i:B) + if(find(i[0])==find(i[1])) + ans++; + else + dsu[find(i[0])]=find(i[1]); + return ans; +} +int Solution::solve(int A, vector > &B) { + return solveit(A,B); +} \ No newline at end of file diff --git a/Graphs/Roads and Libraries.cpp b/Graphs/Roads and Libraries.cpp new file mode 100644 index 0000000..3dd5dee --- /dev/null +++ b/Graphs/Roads and Libraries.cpp @@ -0,0 +1,39 @@ +int Rank[100001],dsu[100001]; +int find(int x) +{ +while(x!=dsu[x]) +x=dsu[x]; +return x; +} +void un_ion(int a,int b) +{ +int x=find(a),y=find(b); +if(find(a)==find(b)) +return; +if(Rank[x]Rank[y]) +dsu[y]=x; +else +dsu[x]=y,Rank[y]++; +} +int solveit(int A,vector>B,int C,int D) +{ + for(int i=0;i<=A;i++) + dsu[i]=i,Rank[i]=1; + for(auto i:B) + un_ion(i[0],i[1]); + +int NOC=0; +for(int i=1;i<=A;i++) + if(dsu[i]==i) + NOC+=1; +return NOC*D + (A-NOC)*min(C,D); +} + +int Solution::solve(int A, vector > &B, int C, int D) +{ + + return solveit(A, B, C, D); + +} From f240d822dfd2d1c932a4695f556818924720d1c9 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Thu, 26 Sep 2019 20:39:43 +0530 Subject: [PATCH 29/61] Add files via upload --- .../B-th Smallest Prime Fraction.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Heaps and Maps/B-th Smallest Prime Fraction.cpp diff --git a/Heaps and Maps/B-th Smallest Prime Fraction.cpp b/Heaps and Maps/B-th Smallest Prime Fraction.cpp new file mode 100644 index 0000000..3ed14ff --- /dev/null +++ b/Heaps and Maps/B-th Smallest Prime Fraction.cpp @@ -0,0 +1,36 @@ +#define y second.second +#define x second.first + +vector Solution::solve(vector &A, int B) { + int K = B; + priority_queue>,vector>>,greater>>> q; + unordered_map nxt; + for(int i = A.size()-1;i > 0;i--) + nxt[A[i]] = A[i-1]; + + double last = A[A.size()-1],d; + nxt[1] = 0; + + for(int i = 0;i < A.size();i++){ + d = (double)A[i]/last; + q.push(make_pair(d,make_pair(A[i],last))); + } + + while(q.size() && K){ + K--; + if(K == 0) break; + + pair> p = q.top();q.pop(); + if(nxt[p.y] != 0){ + p.y = nxt[p.y]; + d = (double)p.x/(double)p.y; + q.push(make_pair(d,p.second)); + } + } + + vector ret(2); + ret[0] = q.top().second.first; + ret[1] = q.top().second.second; + + return ret; +} \ No newline at end of file From f4feeb3782d9008ce81230c34aa5dfb0a6197990 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Thu, 26 Sep 2019 20:40:17 +0530 Subject: [PATCH 30/61] Add files via upload --- Math/Alice and Bob.cpp | 22 ++++++++++++++++++++++ Math/Consecutive Number Sum.cpp | 18 ++++++++++++++++++ Math/Consecutive Numbers Sum.cpp | 16 ++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 Math/Alice and Bob.cpp create mode 100644 Math/Consecutive Number Sum.cpp create mode 100644 Math/Consecutive Numbers Sum.cpp diff --git a/Math/Alice and Bob.cpp b/Math/Alice and Bob.cpp new file mode 100644 index 0000000..439c989 --- /dev/null +++ b/Math/Alice and Bob.cpp @@ -0,0 +1,22 @@ +int solveit(vector &A) +{ + int g=A[0],mx=A[0],n=A.size(); + + for(int x:A) + { + g = __gcd(x,g); + + mx=max(mx,x); + + } + + return (mx/g - n)%2; + +} + +int Solution::solve(vector &A) +{ + + return solveit(A); + +} diff --git a/Math/Consecutive Number Sum.cpp b/Math/Consecutive Number Sum.cpp new file mode 100644 index 0000000..aa7de19 --- /dev/null +++ b/Math/Consecutive Number Sum.cpp @@ -0,0 +1,18 @@ +int Solution::solve(int A) + { + while ((A & 1) == 0) A >>= 1; + int ans = 1, d = 3; + + while (d * d <= A) { + int e = 0; + while (A % d == 0) { + A /= d; + e++; + } + ans *= e + 1; + d += 2; + } + + if (A > 1) ans <<= 1; + return ans; + } \ No newline at end of file diff --git a/Math/Consecutive Numbers Sum.cpp b/Math/Consecutive Numbers Sum.cpp new file mode 100644 index 0000000..b52be5d --- /dev/null +++ b/Math/Consecutive Numbers Sum.cpp @@ -0,0 +1,16 @@ +int Solution::solve(int A) { + while((A&1)==0) A >>= 1; + int ans = 1, d = 3; + while(d*d <= A){ + int e = 0; + while(A%d == 0){ + A /= d; + e++; + } + ans *= e + 1; + d += 2; + } + if(A > 1) + ans <<= 1; + return ans; +} \ No newline at end of file From 91b38c8e8f78c3acd25d45444810ba19cab5a7a4 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Thu, 26 Sep 2019 20:40:44 +0530 Subject: [PATCH 31/61] Add files via upload --- .../Is given level order a BST.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Stacks and Queues/Is given level order a BST.cpp diff --git a/Stacks and Queues/Is given level order a BST.cpp b/Stacks and Queues/Is given level order a BST.cpp new file mode 100644 index 0000000..33649fd --- /dev/null +++ b/Stacks and Queues/Is given level order a BST.cpp @@ -0,0 +1,37 @@ +struct node { + int val; + int min; + int max; + node(int val, int min, int max) : val(val), min(min), max(max) {}; +}; + +int Solution::solve(vector &A) { + if(A.empty()) return 1; + queue q; + bool left_available = true; + bool right_available = true; + node cur_node(A.front(), numeric_limits::min(), numeric_limits::max()); + int i = 1; + while(i < A.size()) { + if(cur_node.min <= A[i] && A[i] <= cur_node.max) { + if(left_available && A[i] < cur_node.val) { + q.emplace(A[i], cur_node.min, cur_node.val); + left_available = false; + i++; + continue; + } + if(right_available && cur_node.val < A[i]) { + q.emplace(A[i], cur_node.val, cur_node.max); + left_available = false; + right_available = false; + i++; + continue; + } else if(q.empty()) return 0; + } else if(q.empty()) return 0; + cur_node = q.front(); + q.pop(); + left_available = true; + right_available = true; + } + return 1; +} From 299a466096b45e171550d1f0ca3160c29a30112d Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Thu, 26 Sep 2019 20:41:14 +0530 Subject: [PATCH 32/61] Add files via upload --- Trees/Balanced Brackets Queries.cpp | 46 +++++++++++++++++++ Trees/Count Complete Tree Nodes.cpp | 36 +++++++++++++++ Trees/Distance between Nodes of BST.cpp | 33 +++++++++++++ ... from Preorder of a Binary Search Tree.cpp | 20 ++++++++ 4 files changed, 135 insertions(+) create mode 100644 Trees/Balanced Brackets Queries.cpp create mode 100644 Trees/Count Complete Tree Nodes.cpp create mode 100644 Trees/Distance between Nodes of BST.cpp create mode 100644 Trees/Leaf nodes from Preorder of a Binary Search Tree.cpp diff --git a/Trees/Balanced Brackets Queries.cpp b/Trees/Balanced Brackets Queries.cpp new file mode 100644 index 0000000..d0821cc --- /dev/null +++ b/Trees/Balanced Brackets Queries.cpp @@ -0,0 +1,46 @@ +#define pb push_back +struct Node{ + int val=0,o=0,c=0; +}; +void build(string &str,Node* tree,int nd, int s, int e){ + if (s==e) {tree[nd].val=0; + if (str[s]=='(') {tree[nd].o=1;} + else {tree[nd].c=1;} + return; + } + int m = (s+e)/2; + build(str,tree,2*nd,s,m); + build(str,tree,(2*nd)+1,m+1,e); + int t = min(tree[2*nd].o, tree[(2*nd)+1].c); + tree[nd].val = tree[2*nd].val+tree[(2*nd)+1].val+t; + tree[nd].o = tree[2*nd].o+tree[(2*nd)+1].o-t; + tree[nd].c = tree[2*nd].c+tree[(2*nd)+1].c-t; + return; +} +Node que(Node* tree, int nd, int s, int e, int l, int r){ + if (s==l and e==r) {return tree[nd];} + int m = (s+e)/2; + Node p1,p2,ans; + if (r<=m) {return que(tree,2*nd,s,m,l,r);} + if (l>=m+1) {return que(tree,(2*nd)+1,m+1,e,l,r);} + p1 = que(tree,2*nd,s,m,l,m); + p2 = que(tree,(2*nd)+1,m+1,e,m+1,r); + int t = min(p1.o,p2.c); + ans.val = p1.val+p2.val+t; + ans.o = p1.o+p2.o-t; + ans.c = p1.c+p2.c-t; + return ans; +} +vector Solution::solve(string A, vector > &B) { + int n = A.size(); + vector ans; + Node tree[4*n+5]; + for (int i=n;i>0;i--){ + A[i] = A[i-1]; + } + build(A,tree,1,1,n); + for (int i=0;ileft; + depth++; + } + return depth-1; +} + +bool checkNode(TreeNode *root, int count) { + // check if (count)th node exists + vector path; + while(count > 1) { + path.push_back(count & 1); + count >>= 1; + } + for(int i=path.size()-1; i>=0; i--) + if(path[i]) root = root->right; + else root = root->left; + return root; +} +int Solution::solve(TreeNode* A) { + if(!A) return 0; + + int depth = getDepth(A); + // binary search on number of nodes + int lo = 1 << depth, hi = lo*2 - 1, mid; + while(lo < hi) { + mid = lo + (hi - lo + 1)/2; + if(checkNode(A, mid)) lo = mid; + else hi = mid-1; + } + + if(checkNode(A, lo)) return lo; + return lo - 1; +} diff --git a/Trees/Distance between Nodes of BST.cpp b/Trees/Distance between Nodes of BST.cpp new file mode 100644 index 0000000..661a57d --- /dev/null +++ b/Trees/Distance between Nodes of BST.cpp @@ -0,0 +1,33 @@ +int distanceFromRoot(TreeNode* root, int x) +{ + if (root->val == x) + return 0; + else if (root->val > x) + return 1 + distanceFromRoot(root->left, x); + return 1 + distanceFromRoot(root->right, x); +} + + +int distanceBetween2(TreeNode* root, int a, int b) +{ + if (!root) + return 0; + + if (root->val > a && root->val > b) + return distanceBetween2(root->left, a, b); + + + if (root->val < a && root->val < b) + return distanceBetween2(root->right, a, b); + + + if (root->val >= a && root->val <= b) + return distanceFromRoot(root, a) + + distanceFromRoot(root, b); +} +int Solution::solve(TreeNode *A, int B, int C) +{ + if (B > C) + swap(B, C); + return distanceBetween2(A, B, C); +} \ No newline at end of file diff --git a/Trees/Leaf nodes from Preorder of a Binary Search Tree.cpp b/Trees/Leaf nodes from Preorder of a Binary Search Tree.cpp new file mode 100644 index 0000000..ebd1240 --- /dev/null +++ b/Trees/Leaf nodes from Preorder of a Binary Search Tree.cpp @@ -0,0 +1,20 @@ +#define pb emplace_back + +vector Solution::solve(vector &A) { + int n = A.size(); + stack st; + vector ans; + for (int i=0;iA[i+1]) {st.push(A[i]);} + else{ + while (!st.empty()){ + if (A[i+1]>st.top()) {st.pop(); f=1;} + else {break;} + } + if (f==1) {ans.pb(A[i]);} + } + } + ans.pb(A[n-1]); + return ans; +} \ No newline at end of file From 571b4971d43c1e74e38eb40cbfd28f031775545b Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Thu, 26 Sep 2019 20:41:41 +0530 Subject: [PATCH 33/61] Add files via upload --- Two Pointers/DEVICE CROSSOVER.cpp | 41 +++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Two Pointers/DEVICE CROSSOVER.cpp diff --git a/Two Pointers/DEVICE CROSSOVER.cpp b/Two Pointers/DEVICE CROSSOVER.cpp new file mode 100644 index 0000000..628728d --- /dev/null +++ b/Two Pointers/DEVICE CROSSOVER.cpp @@ -0,0 +1,41 @@ +int Solution::solve(vector &A, vector &B) +{ + int n = A.size(); + int m = B.size(); + int ptr1 = 0,ptr2 = 0; + int ans = 0,last = 0; + while(ptr1 Date: Tue, 8 Oct 2019 21:22:00 +0530 Subject: [PATCH 34/61] Add files via upload --- ...mage Size of Factorial Zeroes Function.cpp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Binary Search/Preimage Size of Factorial Zeroes Function.cpp diff --git a/Binary Search/Preimage Size of Factorial Zeroes Function.cpp b/Binary Search/Preimage Size of Factorial Zeroes Function.cpp new file mode 100644 index 0000000..89f795e --- /dev/null +++ b/Binary Search/Preimage Size of Factorial Zeroes Function.cpp @@ -0,0 +1,53 @@ +long long numberofzeroes(long long x) +{ + long long ans=0,five=5; + while(five<=x) + { + ans+=x/five; + five*=5; + } + return ans; +} +int solveit(int K) { + long long lostart=0,histart=5e9,loend=0,hiend=5e9,start=-1,end,mid; + while(lostart<=histart) + { + mid=(lostart+histart)/2; + if(numberofzeroes(mid)K) + { + histart=mid-1; + } + else + { + start=mid; + histart=mid-1; + } + } + if(start==-1) + return 0; + while(loend<=hiend) + { + mid=(loend+hiend)/2; + if(numberofzeroes(mid)K) + { + hiend=mid-1; + } + else + { + end=mid; + loend=mid+1; + } + } + return end-start+1; +} +int Solution::solve(int A) { + return solveit(A); +} From 201c1417e7a4c578bd71fea2776e691a0d0dde0d Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Tue, 8 Oct 2019 21:22:34 +0530 Subject: [PATCH 35/61] Add files via upload --- Dynamic Programming/Deepu and girlfriend.cpp | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Dynamic Programming/Deepu and girlfriend.cpp diff --git a/Dynamic Programming/Deepu and girlfriend.cpp b/Dynamic Programming/Deepu and girlfriend.cpp new file mode 100644 index 0000000..cded6f5 --- /dev/null +++ b/Dynamic Programming/Deepu and girlfriend.cpp @@ -0,0 +1,23 @@ +string Solution::solve(int A, vector &B) { + bool game[A+1]; + memset(game,0,sizeof(game)); + for(int i=0;i Date: Tue, 8 Oct 2019 21:22:55 +0530 Subject: [PATCH 36/61] Add files via upload --- Graphs/Rust and his transfer.cpp | 77 ++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Graphs/Rust and his transfer.cpp diff --git a/Graphs/Rust and his transfer.cpp b/Graphs/Rust and his transfer.cpp new file mode 100644 index 0000000..ff48953 --- /dev/null +++ b/Graphs/Rust and his transfer.cpp @@ -0,0 +1,77 @@ +typedef int ll; +const ll mod=1e9+15; + +vector V[3001], dS(3001), dD(3001); +ll visit[3001], mat[3001][3001][2]; + +void dijkstra (ll s, vector& dist) +{ + set> S; + memset(visit, 0, sizeof(visit)); + S.insert({0, s}); + while (!S.empty()) + { + pair p = *S.begin(); + S.erase(S.begin()); + ll u, v; + u = p.first; + v = p.second; + if (visit[v]) + continue; + visit[v] = 1; + dist[v] = u; + for (auto j : V[v]) + { + ll d = mat[v][j][0]; + if (!visit[j] && (dist[v] + d) > &B,int C,int D) +{ + ll n, m, i, j, k, l, x, y, S; + n=A; + m=B.size(); + for (i=0; i<=3000; ++i) + for (j=0; j<=3000; ++j) + mat[i][j][0] = mat[i][j][1] = mod; + for (i=0; i > &B, int C, int D) { + return solveit(A,B,C,D); +} From 011346b69b20eb61a5eeca68ee5e4e3eacdfe507 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Tue, 8 Oct 2019 21:23:14 +0530 Subject: [PATCH 37/61] Add files via upload --- Hashing/Invoiced Billing.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Hashing/Invoiced Billing.cpp diff --git a/Hashing/Invoiced Billing.cpp b/Hashing/Invoiced Billing.cpp new file mode 100644 index 0000000..72150fc --- /dev/null +++ b/Hashing/Invoiced Billing.cpp @@ -0,0 +1,11 @@ +int Solution::countMismatchedPayments(vector &A, vector &B, vector &C, vector &D) { + unordered_map mp; + int cnt=0; + for (int i=0;i Date: Tue, 8 Oct 2019 21:23:57 +0530 Subject: [PATCH 38/61] Add files via upload --- Trees/Maximum Weight Edge Sum.cpp | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Trees/Maximum Weight Edge Sum.cpp diff --git a/Trees/Maximum Weight Edge Sum.cpp b/Trees/Maximum Weight Edge Sum.cpp new file mode 100644 index 0000000..0693995 --- /dev/null +++ b/Trees/Maximum Weight Edge Sum.cpp @@ -0,0 +1,52 @@ +vector > >edges; +const long long int mxn=1e5+5; +const long long int mod=1e9+7; +long long int par[mxn],size[mxn]; +void initialize() +{ + for(int i=0;i > &A) { + long long int n=A.size(),i; + initialize(); + for(i=0;i Date: Tue, 8 Oct 2019 21:25:27 +0530 Subject: [PATCH 39/61] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1879bac..de3a2a5 100644 --- a/README.md +++ b/README.md @@ -16,20 +16,20 @@ I have solved quite a number of problems from several topics. See the below tabl | :--------: | :--------: | | [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 68| | [Backtracking](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Backtracking)| 21| -| [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 19| +| [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 20| | [Bit Manipulation](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Bit%20Manipulation)| 8| | [Codersbit](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Codersbit)| 19| -| [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 82| -| [Graphs](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Graphs)| 31| +| [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 83| +| [Graphs](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Graphs)| 32| | [Greedy](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Greedy)| 13| -| [Hashing](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Hashing)| 26| +| [Hashing](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Hashing)| 27| | [Heaps And Maps](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Heaps%20and%20Maps)| 21| | [Linked Lists](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Linked%20Lists)| 22| | [Math](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Math)| 49| | [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 26| | [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 32| | [Time Complexity](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Time%20Complexity)| 30| -| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 59| +| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 60| | [Two Pointers](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Two%20Pointers)| 22| From 4240321d57b240575c75299652f181f9b3d627d7 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:00:35 +0530 Subject: [PATCH 40/61] Update README.md --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index de3a2a5..891afe2 100644 --- a/README.md +++ b/README.md @@ -14,23 +14,23 @@ I have solved quite a number of problems from several topics. See the below tabl | Topic Name| Number of Problems Solved| | :--------: | :--------: | -| [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 68| -| [Backtracking](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Backtracking)| 21| -| [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 20| -| [Bit Manipulation](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Bit%20Manipulation)| 8| +| [Arrays](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Arrays)| 78| +| [Backtracking](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Backtracking)| 22| +| [Binary Search](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Binary%20Search)| 22| +| [Bit Manipulation](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Bit%20Manipulation)| 10| | [Codersbit](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Codersbit)| 19| -| [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 83| -| [Graphs](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Graphs)| 32| -| [Greedy](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Greedy)| 13| -| [Hashing](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Hashing)| 27| -| [Heaps And Maps](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Heaps%20and%20Maps)| 21| -| [Linked Lists](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Linked%20Lists)| 22| -| [Math](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Math)| 49| -| [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 26| -| [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 32| +| [Dynamic Programming](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Dynamic%20Programming)| 95| +| [Graphs](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Graphs)| 43| +| [Greedy](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Greedy)| 15| +| [Hashing](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Hashing)| 36| +| [Heaps And Maps](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Heaps%20and%20Maps)| 25| +| [Linked Lists](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Linked%20Lists)| 26| +| [Math](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Math)| 56| +| [Stacks And Queues](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Stacks%20and%20Queues)| 27| +| [Strings](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Strings)| 36| | [Time Complexity](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Time%20Complexity)| 30| -| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 60| -| [Two Pointers](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Two%20Pointers)| 22| +| [Trees](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Trees)| 66| +| [Two Pointers](https://github.com/black-shadows/InterviewBit-Topicwise-Solutions/tree/master/Two%20Pointers)| 26| ## How to contribute? From 9317deadae4da348499c79e3c969a641d4f9f802 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:03:37 +0530 Subject: [PATCH 41/61] Add files via upload --- Arrays/Balance Array.cpp | 40 ++++++++++++++++++++ Arrays/Leaders in an array.cpp | 12 ++++++ Arrays/Max Min.cpp | 11 ++++++ Arrays/Maximum Area of Triangle!.cpp | 39 ++++++++++++++++++++ Arrays/Maximum Sum Square SubMatrix.cpp | 42 +++++++++++++++++++++ Arrays/Maximum Sum Triplet.cpp | 49 +++++++++++++++++++++++++ Arrays/Minimum Lights to Activate.cpp | 38 +++++++++++++++++++ Arrays/Perfect Peak of Array.cpp | 27 ++++++++++++++ Arrays/Pick from both sides!.cpp | 12 ++++++ Arrays/Sort array with squares!.cpp | 24 ++++++++++++ 10 files changed, 294 insertions(+) create mode 100644 Arrays/Balance Array.cpp create mode 100644 Arrays/Leaders in an array.cpp create mode 100644 Arrays/Max Min.cpp create mode 100644 Arrays/Maximum Area of Triangle!.cpp create mode 100644 Arrays/Maximum Sum Square SubMatrix.cpp create mode 100644 Arrays/Maximum Sum Triplet.cpp create mode 100644 Arrays/Minimum Lights to Activate.cpp create mode 100644 Arrays/Perfect Peak of Array.cpp create mode 100644 Arrays/Pick from both sides!.cpp create mode 100644 Arrays/Sort array with squares!.cpp diff --git a/Arrays/Balance Array.cpp b/Arrays/Balance Array.cpp new file mode 100644 index 0000000..53ae0d4 --- /dev/null +++ b/Arrays/Balance Array.cpp @@ -0,0 +1,40 @@ +int Solution::solve(vector &A) { + int left_odd = 0; + int left_even = 0; + int right_odd = 0; + int right_even = 0; + for(int i = 0; i < A.size(); ++i) + { + if( i % 2 == 0) + { + right_even += A[i]; + } + else + { + right_odd += A[i]; + } + } + int count = 0; + for(int i = 0; i < A.size(); ++i) + { + if(i % 2 == 0) + { + right_even -= A[i]; + if(left_odd + right_even == left_even + right_odd) + { + ++count; + } + left_even += A[i]; + } + else + { + right_odd -= A[i]; + if(left_odd + right_even == left_even + right_odd) + { + ++count; + } + left_odd += A[i]; + } + } + return count; +} \ No newline at end of file diff --git a/Arrays/Leaders in an array.cpp b/Arrays/Leaders in an array.cpp new file mode 100644 index 0000000..f2dc036 --- /dev/null +++ b/Arrays/Leaders in an array.cpp @@ -0,0 +1,12 @@ +vector Solution::solve(vector &A) { + vector ans; + int soldier=A[A.size()-1]; + ans.push_back(soldier); + for(int i=A.size()-2;i>=0;i--){ + if(A[i]>soldier){ + ans.push_back(A[i]); + } + soldier=max(soldier,A[i]); + } + return ans; +} \ No newline at end of file diff --git a/Arrays/Max Min.cpp b/Arrays/Max Min.cpp new file mode 100644 index 0000000..dcdf244 --- /dev/null +++ b/Arrays/Max Min.cpp @@ -0,0 +1,11 @@ +int Solution::solve(vector &A) +{ + int n=A.size(); + int maxm=A[0],minm=A[0]; + for(int i=1;imaxm) maxm=A[i]; + else if(A[i] &A) { + vector minCol(3, INT_MAX); + vector maxCol(3, INT_MIN); + vector> colMin(3, vector(A[0].length(),INT_MAX)); + vector> colMax(3, vector(A[0].length(),INT_MIN)); + int cols=A[0].length(); + for(int i=0;i 0) + ans -= psum[xi - 1][yj]; + if (xj > 0) + ans -= psum[yi][xj - 1]; + if (xi > 0 and xj > 0) + ans += psum[xi - 1][xj - 1]; + return ans; +} + + +int Solution::solve(vector > &matrix, int B) { + int n = matrix.size(), m = matrix[0].size(); + if(n==1 and B==1) return matrix[0][0]; + + // prefix sum + psum[0][0] = matrix[0][0]; + for (int i = 1; i < n; i++) + psum[i][0] = psum[i - 1][0] + matrix[i][0]; + for (int i = 1; i < m; i++) + psum[0][i] = psum[0][i - 1] + matrix[0][i]; + for (int i = 1; i < n; i++) + for (int j = 1; j < m; j++) + psum[i][j] = psum[i - 1][j] + psum[i][j - 1] - psum[i - 1][j - 1] + matrix[i][j]; + + int ans = INT_MIN; + for (int i = B-1; i < n; i++) + { + for (int j = B-1; j < m; j++) + { + int x=i-(B-1),y=j-(B-1); + if(x>=0 and y>=0){ + int s=range_sum(x,y,i,j); + ans=std::max(ans,s); + } + } + } + return ans; +} \ No newline at end of file diff --git a/Arrays/Maximum Sum Triplet.cpp b/Arrays/Maximum Sum Triplet.cpp new file mode 100644 index 0000000..2b06c9d --- /dev/null +++ b/Arrays/Maximum Sum Triplet.cpp @@ -0,0 +1,49 @@ +int Solution::solve(vector &A) { + int n=A.size(); + int dp[n]={0}; + dp[n-2]=A[n-1]; + for(int i=n-3;i>=0;i--) + { + dp[i]=max(dp[i+1],A[i+1]); + } + set> s; + int ans=0; + for(int i=n-2;i>=0;i--) + { + if(A[i]0) + { + vector> v1,v2; + auto it=s.end(); + it--; + while(true) + { + if(it->second>A[i]) + { + ans=max(ans,it->first+A[i]); + if(A[i]+dp[i]>it->first) + { + v2.push_back({A[i]+dp[i],A[i]}); + } + } + else if(A[i]+dp[i]>=it->first) + { + v1.push_back(*it); + v2.push_back({A[i]+dp[i],A[i]}); + } + if(it==s.begin()) break; + it--; + } + for(auto x:v1) s.erase(x); + for(auto x:v2) s.insert(x); + } + else + { + s.insert({A[i]+dp[i],A[i]}); + } + } + } + if(ans) return ans; + else return -1; +} \ No newline at end of file diff --git a/Arrays/Minimum Lights to Activate.cpp b/Arrays/Minimum Lights to Activate.cpp new file mode 100644 index 0000000..26ae82a --- /dev/null +++ b/Arrays/Minimum Lights to Activate.cpp @@ -0,0 +1,38 @@ +nt lcount=0; +int findLamp(int index,vector &A,int B){ + int start = index+B-1=index;i--){ + if(A[i]==1) + return i; + } + int end = index-B+1>0?index-B+1:0; + for(int i=index-1;i>=end;i--){ + if(A[i]==1) + return i; + } + return -1; +} +void lightUpCorr(int index,vector &corr,int B){ + int i; + int end = index+B-1=index-B+1&& i>=0;i--) + corr[i] = true; + lcount++; +} +int Solution::solve(vector &A, int B) { + vector corr; + lcount=0; + corr.resize(A.size(),false); + for(int i=0;i &A) { + int n=A.size(); + int i=1,l=A[0]; + if(n<3) + return 0; + while(il) + { + l=A[i]; + int j=i+1; + while(j &a, int k) { + int n=a.size(); + vector prefix(n,0); + for(int i=0;i=-1){ + ans=max(ans,(l>=0?prefix[l]:0)+prefix[n-1]-prefix[r-1]); + l--,r--; + } + return ans; +} diff --git a/Arrays/Sort array with squares!.cpp b/Arrays/Sort array with squares!.cpp new file mode 100644 index 0000000..1cb5fb0 --- /dev/null +++ b/Arrays/Sort array with squares!.cpp @@ -0,0 +1,24 @@ +vector Solution::solve(vector &A) { + int s = 0, e = A.size()-1; + vector res; + while(s<=e){ + if(s==e){ res.push_back(A[s]*A[s]); break;} + if(abs(A[s])>abs(A[e])){ + res.push_back(A[s]*A[s]); + s++; + } + else if(abs(A[s]) Date: Sat, 19 Sep 2020 01:03:59 +0530 Subject: [PATCH 42/61] Add files via upload --- Backtracking/Maximal String.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Backtracking/Maximal String.cpp diff --git a/Backtracking/Maximal String.cpp b/Backtracking/Maximal String.cpp new file mode 100644 index 0000000..9309c3c --- /dev/null +++ b/Backtracking/Maximal String.cpp @@ -0,0 +1,27 @@ +int N,b; +string sol,ans; + +void recur(int indx, int left, string &A){ + if(left==0 || indx==N){ + sol = (A>sol ? A : sol); + return; + } + + for(int i=indx+1; iA[indx]){ + swap(A[indx],A[i]); + recur(indx+1,left-1,A); + swap(A[indx],A[i]); + } + } + recur(indx+1,left,A); +} + +string Solution::solve(string A, int B) { + sol.clear(); ans.clear(); + N=A.size(), b=B; + if(N<=1 || B==0) return A; + + recur(0,B,A); + return sol; +} \ No newline at end of file From 50b12b49149e492e9e82ae54822b6ae62b9a4c56 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:04:18 +0530 Subject: [PATCH 43/61] Add files via upload --- Binary Search/Search in Bitonic Array!.cpp | 31 ++++++++++++++ Binary Search/WoodCutting Made Easy!.cpp | 47 ++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 Binary Search/Search in Bitonic Array!.cpp create mode 100644 Binary Search/WoodCutting Made Easy!.cpp diff --git a/Binary Search/Search in Bitonic Array!.cpp b/Binary Search/Search in Bitonic Array!.cpp new file mode 100644 index 0000000..257c38a --- /dev/null +++ b/Binary Search/Search in Bitonic Array!.cpp @@ -0,0 +1,31 @@ +int Solution::solve(vector &A, int B) { + int n=A.size(); + int i; + int start, end; + // finding pivot point; + for(i=0;iA[i+1]) break; + } + if(B == A[i]) return i; + + start=i+1; + end=n-1; + //searching first half + while(start <= end){ + int mid=(start+end)/2; + if(B== A[mid]) return mid; + else if(B A,int B){ + long long sum; + if(start<=end){ + sum=0; + int mid=start+(end-start)/2; + + for(int i=0;imid){ + sum+=A[i]-mid; + } + } + if(sum>=(long long)B){ + return Bisearch(mid+1,end,A,B); + } + else if(sum &A, int B) { + int n=A.size(); + int mx=0; + for(int i=0;imx)mx=A[i]; + + } + int index=Bisearch(0,mx,A,B); + int ans=index; + for(int i=index; ;i--){ + long long sum=0; + for(int j=0;ji){ + sum+=(long long)(A[j]-i); + } + } + if(sum>=(long long)B){ + ans=i; + break; + } + + } + + return ans; +} \ No newline at end of file From dbacca578f5a8551240c98b70e9e65f3717226ed Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:04:38 +0530 Subject: [PATCH 44/61] Add files via upload --- Bit Manipulation/Trailing Zeroes.cpp | 8 ++++++++ Bit Manipulation/XOR-ing the Subarrays!.cpp | 10 ++++++++++ 2 files changed, 18 insertions(+) create mode 100644 Bit Manipulation/Trailing Zeroes.cpp create mode 100644 Bit Manipulation/XOR-ing the Subarrays!.cpp diff --git a/Bit Manipulation/Trailing Zeroes.cpp b/Bit Manipulation/Trailing Zeroes.cpp new file mode 100644 index 0000000..6e92a47 --- /dev/null +++ b/Bit Manipulation/Trailing Zeroes.cpp @@ -0,0 +1,8 @@ +int Solution::solve(int a) { + for(int i=0; i<31; i++){ + if((a>>i) & 1){ + return i; + } + } + return 31; +} \ No newline at end of file diff --git a/Bit Manipulation/XOR-ing the Subarrays!.cpp b/Bit Manipulation/XOR-ing the Subarrays!.cpp new file mode 100644 index 0000000..cb9c64f --- /dev/null +++ b/Bit Manipulation/XOR-ing the Subarrays!.cpp @@ -0,0 +1,10 @@ +int Solution::solve(vector &A) { + int res=0; + for(int i=0;i Date: Sat, 19 Sep 2020 01:05:20 +0530 Subject: [PATCH 45/61] Add files via upload --- Dynamic Programming/0-1 Knapsack.cpp | 25 ++++ Dynamic Programming/Chain of Pairs.cpp | 21 +++ Dynamic Programming/Egg Drop Problem!.cpp | 12 ++ .../Increasing Path in Matrix.cpp | 29 ++++ Dynamic Programming/Max edge queries!.cpp | 129 ++++++++++++++++++ .../Maximum Path in Triangle.cpp | 12 ++ .../Maximum Size Square Sub-matrix.cpp | 14 ++ Dynamic Programming/Merge elements.cpp | 23 ++++ .../Minimum Difference Subsets!.cpp | 30 ++++ Dynamic Programming/Paint House!.cpp | 15 ++ Dynamic Programming/Subset Sum Problem!.cpp | 16 +++ Dynamic Programming/Tiling With Dominoes.cpp | 12 ++ 12 files changed, 338 insertions(+) create mode 100644 Dynamic Programming/0-1 Knapsack.cpp create mode 100644 Dynamic Programming/Chain of Pairs.cpp create mode 100644 Dynamic Programming/Egg Drop Problem!.cpp create mode 100644 Dynamic Programming/Increasing Path in Matrix.cpp create mode 100644 Dynamic Programming/Max edge queries!.cpp create mode 100644 Dynamic Programming/Maximum Path in Triangle.cpp create mode 100644 Dynamic Programming/Maximum Size Square Sub-matrix.cpp create mode 100644 Dynamic Programming/Merge elements.cpp create mode 100644 Dynamic Programming/Minimum Difference Subsets!.cpp create mode 100644 Dynamic Programming/Paint House!.cpp create mode 100644 Dynamic Programming/Subset Sum Problem!.cpp create mode 100644 Dynamic Programming/Tiling With Dominoes.cpp diff --git a/Dynamic Programming/0-1 Knapsack.cpp b/Dynamic Programming/0-1 Knapsack.cpp new file mode 100644 index 0000000..f92221f --- /dev/null +++ b/Dynamic Programming/0-1 Knapsack.cpp @@ -0,0 +1,25 @@ +int Solution::solve(vector &A, vector &B, int C) { + int n = A.size(); + + int dp[n+1][C+1]; + + for(int i=0;i<=n;i++){ + dp[i][0] = 0; + } + for(int j=0;j<=C;j++){ + dp[0][j] = 0; + } + + for(int i=1;i<=n;i++){ + for(int j=0;j<=C;j++){ + if(B[i-1] <= j){ + dp[i][j] = max(A[i-1] + dp[i-1][j-B[i-1]], dp[i-1][j]); + } + else{ + dp[i][j] = dp[i-1][j]; + } + } + } + + return dp[n][C]; +} \ No newline at end of file diff --git a/Dynamic Programming/Chain of Pairs.cpp b/Dynamic Programming/Chain of Pairs.cpp new file mode 100644 index 0000000..e787fea --- /dev/null +++ b/Dynamic Programming/Chain of Pairs.cpp @@ -0,0 +1,21 @@ +int Solution::solve(vector > &a) { + + int n=a.size(); + int dp[n]; + dp[0]=1; + for(int i=1;i a[j][1] && dp[i]<=dp[j]){ + dp[i]=1+dp[j]; + } + } + } + int ans=INT_MIN; + for(int i=0;ians){ + ans=dp[i]; + } + } + return ans; +} diff --git a/Dynamic Programming/Egg Drop Problem!.cpp b/Dynamic Programming/Egg Drop Problem!.cpp new file mode 100644 index 0000000..95b04d0 --- /dev/null +++ b/Dynamic Programming/Egg Drop Problem!.cpp @@ -0,0 +1,12 @@ +int Solution::solve(int A, int B) { + vectordp(A+1, 0); + int m; + for(m = 0; dp[A] < B; m++) + { + for(int k = A; k>0; k--) + { + dp[k] += dp[k-1] + 1; + } + } + return m; +} \ No newline at end of file diff --git a/Dynamic Programming/Increasing Path in Matrix.cpp b/Dynamic Programming/Increasing Path in Matrix.cpp new file mode 100644 index 0000000..1b8c565 --- /dev/null +++ b/Dynamic Programming/Increasing Path in Matrix.cpp @@ -0,0 +1,29 @@ +int Solution::solve(vector > &A) { + int n=A.size(); + int m=A[0].size(); + vector>dp(n,vector(m,-1)); + dp[0][0]=1; + for(int i=1;iA[0][i-1]) + dp[0][i]=1+dp[0][i-1]; + } + for(int i=1;iA[i-1][0]) + dp[i][0]=1+dp[i-1][0]; + } + for(int i=1;iA[i-1][j]||A[i][j]>A[i][j-1]) + dp[i][j]=1+max(dp[i-1][j],dp[i][j-1]); + } + } + if(dp[n-1][m-1]==0) + return -1; + return dp[n-1][m-1]; +} \ No newline at end of file diff --git a/Dynamic Programming/Max edge queries!.cpp b/Dynamic Programming/Max edge queries!.cpp new file mode 100644 index 0000000..1072076 --- /dev/null +++ b/Dynamic Programming/Max edge queries!.cpp @@ -0,0 +1,129 @@ +const int N = 100005; + +// Depths of Nodes +vector level(N); +const int LG = 20; + +// Parent at every 2^i level +vector > dp(LG, vector(N)); + +// Maximum node at every 2^i level +vector > mx(LG, vector(N)); + +// Graph that stores destinations +// and its weight +vector > > v(N); +int n; + +// Function to traverse the nodes +// using the Depth-First Search Traversal +void dfs_lca(int a, int par, int lev) +{ + dp[0][a] = par; + level[a] = lev; + for (auto i : v[a]) { + + // Condition to check if its + // equal to its parent then skip + if (i.first == par) + continue; + mx[0][i.first] = i.second; + + // DFS Recursive Call + dfs_lca(i.first, a, lev + 1); + } +} + +// Function to find the ansector +void find_ancestor() +{ + + // Loop to set every 2^i distance + for (int i = 1; i < LG; i++) { + // Loop to calculate for + // each node in the N-ary tree + for (int j = 1; j <= n; j++) { + dp[i][j] + = dp[i - 1][dp[i - 1][j]]; + + // Storing maximum edge + mx[i][j] + = max(mx[i - 1][j], + mx[i - 1][dp[i - 1][j]]); + } + } +} + +int getMax(int a, int b) +{ + // Swaping if node a is at more depth + // than node b because we will + // always take at more depth + if (level[b] < level[a]) + swap(a, b); + + int ans = 0; + + // Diffeence between the depth of + // the two given nodes + int diff = level[b] - level[a]; + while (diff > 0) { + int log = log2(diff); + ans = max(ans, mx[log][b]); + + // Changing Node B to its + // parent at 2 ^ i distance + b = dp[log][b]; + + // Subtracting distance by 2^i + diff -= (1 << log); + } + + // Take both a, b to its + // lca and find maximum + while (a != b) { + int i = log2(level[a]); + + // Loop to find the maximum 2^ith + // parent the is differnet + // for both a and b + while (i > 0 + && dp[i][a] == dp[i][b]) + i--; + + // Updating ans + ans = max(ans, mx[i][a]); + ans = max(ans, mx[i][b]); + + // Changing value to its parent + a = dp[i][a]; + b = dp[i][b]; + } + return ans; +} + +// Function to compute the Least +// common Ansector +void compute_lca() +{ + dfs_lca(1, 0, 0); + find_ancestor(); +} +vector Solution::solve(vector > &A, vector > &B) { + int i; + n = A.size() + 1; + for(i=0;i<=n;i++) v[i].clear(); + for(i=0;i ans(q); + for(i=0;i > &A) { +int m=A.size(); +int n=A[0].size(); + for(int i=A.size()-2;i>=0;i--) + { + for(int j=0;j > &A) { + + int n=A.size(); + int m=A[0].size(); + int ans=1; + for(int i=1;i &A) { + int n = A.size(); + if(n==0) + return 0; + + vector> dp(n,vector(n,0)); + vector sum(n,0); + sum[0] = A[0]; + + for(int i=1;i &A) { +int n=A.size(); +int s=0; +for(int i=0; i=0; i--) +{ + if(dp[n][i]) + return( abs(s-2*i) ) ; +} +} diff --git a/Dynamic Programming/Paint House!.cpp b/Dynamic Programming/Paint House!.cpp new file mode 100644 index 0000000..35b3ee4 --- /dev/null +++ b/Dynamic Programming/Paint House!.cpp @@ -0,0 +1,15 @@ +int Solution::solve(vector > &A) { + int n=A.size(); + int dp[n+1][3]; + int i,j; + for(i=0;i<=2;i++) + dp[0][i]=0; + for(i=1;i<=n;i++) + { + dp[i][0]=A[i-1][0]+min(dp[i-1][1],dp[i-1][2]); + dp[i][1]=A[i-1][1]+min(dp[i-1][0],dp[i-1][2]); + dp[i][2]=A[i-1][2]+min(dp[i-1][0],dp[i-1][1]); + } + + return min(dp[n][0],min(dp[n][1],dp[n][2])); +} \ No newline at end of file diff --git a/Dynamic Programming/Subset Sum Problem!.cpp b/Dynamic Programming/Subset Sum Problem!.cpp new file mode 100644 index 0000000..a909d10 --- /dev/null +++ b/Dynamic Programming/Subset Sum Problem!.cpp @@ -0,0 +1,16 @@ +int Solution::solve(vector &A, int B) { + int sum=0; + int n=A.size(); + for(int i=0;isum) return 0; + vector dp(sum+1,0); + dp[0] = 1; + for(int i=0; i Date: Sat, 19 Sep 2020 01:06:08 +0530 Subject: [PATCH 46/61] Add files via upload --- Graphs/Cycle in Directed Graph.cpp | 34 ++++++++++++++ Graphs/Cycle in Undirected Graph.cpp | 15 +++++++ Graphs/Delete Edge!.cpp | 41 +++++++++++++++++ Graphs/Min Cost Path.cpp | 43 ++++++++++++++++++ Graphs/Path in Directed Graph.cpp | 33 ++++++++++++++ Graphs/Path with good nodes!.cpp | 38 ++++++++++++++++ Graphs/Permutation Swaps!.cpp | 34 ++++++++++++++ Graphs/Region in BinaryMatrix.cpp | 50 +++++++++++++++++++++ Graphs/Snake Ladder Problem!.cpp | 67 ++++++++++++++++++++++++++++ Graphs/Useful Extra Edges.cpp | 58 ++++++++++++++++++++++++ Graphs/Water Flow.cpp | 58 ++++++++++++++++++++++++ 11 files changed, 471 insertions(+) create mode 100644 Graphs/Cycle in Directed Graph.cpp create mode 100644 Graphs/Cycle in Undirected Graph.cpp create mode 100644 Graphs/Delete Edge!.cpp create mode 100644 Graphs/Min Cost Path.cpp create mode 100644 Graphs/Path in Directed Graph.cpp create mode 100644 Graphs/Path with good nodes!.cpp create mode 100644 Graphs/Permutation Swaps!.cpp create mode 100644 Graphs/Region in BinaryMatrix.cpp create mode 100644 Graphs/Snake Ladder Problem!.cpp create mode 100644 Graphs/Useful Extra Edges.cpp create mode 100644 Graphs/Water Flow.cpp diff --git a/Graphs/Cycle in Directed Graph.cpp b/Graphs/Cycle in Directed Graph.cpp new file mode 100644 index 0000000..5962df7 --- /dev/null +++ b/Graphs/Cycle in Directed Graph.cpp @@ -0,0 +1,34 @@ +bool findCycle(int cur, vector>&adj, vector&visited, vector&recVisited){ + visited[cur] = true; + recVisited[cur] = true; + + for(auto itr: adj[cur]){ + if(!visited[itr]){ + if(findCycle(itr, adj, visited, recVisited))return true; + } + // if it is visited then check in recVisited + else if(recVisited[itr]){ + return true; + } + } + recVisited[cur] = false; + return false; +} +int Solution::solve(int v, vector > &B) { + + vector>adj(v+1); + for(int i = 0; i < B.size(); ++i){ + adj[B[i][0]].push_back(B[i][1]); + } + vectorvisited(v+1); + vectorrecVisited(v+1); + + for(int i = 1; i <= v; ++i){ + if(!visited[i]){ + if(findCycle(i, adj, visited, recVisited)){ + return true; + } + } + } + return false; +} \ No newline at end of file diff --git a/Graphs/Cycle in Undirected Graph.cpp b/Graphs/Cycle in Undirected Graph.cpp new file mode 100644 index 0000000..7685811 --- /dev/null +++ b/Graphs/Cycle in Undirected Graph.cpp @@ -0,0 +1,15 @@ +int findpar(int x,vector&par){ + while(par[x]!=-1) + x=par[x]; + return x; +} +int Solution::solve(int A, vector > &B) { +vector par(A+1,-1); + for(int i=0;i adj[maxn]; +#define MOD 1000000007 + +int dfs(int u,int v,vector &A) +{ + int s=0; + s+=A[u-1]; + + for(int i=0;i &A, vector > &B) { + sum=0; + maxe=0; + + for(int i=0;i &C) { + // vector > color(A, vector(B,0)); + vector > tot(A,vector(B,100000000)); + deque> dq; + dq.push_back(make_pair(0,0)); + tot[0][0]=0; + while(!dq.empty()){ + pair temp=dq.front(); + dq.pop_front(); + int x=temp.first, y=temp.second; + // cout<0 && tot[x-1][y]>tot[x][y]+(C[x][y]!='U')){ + tot[x-1][y]=tot[x][y]+(C[x][y]!='U'); + if(C[x][y]=='U'){ + dq.push_front(make_pair(x-1,y)); + } else { + dq.push_back(make_pair(x-1,y)); + } + } + if(xtot[x][y]+(C[x][y]!='D')){ + tot[x+1][y]=tot[x][y]+(C[x][y]!='D'); + if(C[x][y]=='D'){ + dq.push_front(make_pair(x+1,y)); + } else { + dq.push_back(make_pair(x+1,y)); + } + } + if(y>0 && tot[x][y-1]>tot[x][y]+(C[x][y]!='L')){ + tot[x][y-1]=tot[x][y]+(C[x][y]!='L'); + if(C[x][y]=='L'){ + dq.push_front(make_pair(x,y-1)); + } else dq.push_back(make_pair(x,y-1)); + } + if(ytot[x][y]+(C[x][y]!='R')){ + // cout<<"lol"< adj[maxn]; +int visited[maxn]; +void ini() { + for (int i = 0; i < maxn; i++) adj[i].clear(); + memset(visited, 0, sizeof(visited)); +} +bool isReachable(int s, int d) { + if (s == d) return true; + queue < int > q; + q.push(s); + visited[s] = 1; + while (q.empty() == false) { + int s = q.front(); + q.pop(); + for (auto v: adj[s]) { + if (v == d) return true; + if (visited[v] == 0) { + visited[v] = 1; + q.push(v); + } + } + } + return false; +} +int Solution::solve(int A, vector < vector < int > > & B) { + assert(A >= 2 && A <= 100000); + assert(B.size() <= 200000); + ini(); + for (auto & it: B) adj[it[0]].push_back(it[1]); + if (isReachable(1, A)) return 1; + return 0; +} diff --git a/Graphs/Path with good nodes!.cpp b/Graphs/Path with good nodes!.cpp new file mode 100644 index 0000000..75b0887 --- /dev/null +++ b/Graphs/Path with good nodes!.cpp @@ -0,0 +1,38 @@ +vector> adj; + +int dfs(int u,int p,int c,vector &A){ + int ans=0; + int children=0; + for(int v: adj[u]){ + if(v!=p){ + if(A[u-1]==0){ + ans+=dfs(v,u,c,A); + }else{ + ans+=dfs(v,u,c-1,A); + } + children++; + } + } + + if(children==0){ + if(c>=1 || (c==0 && A[u-1]==0))return 1; + else return 0; + } + + return ans; + +} + +int Solution::solve(vector &A, vector > &B, int C) { + adj.assign(100005,vector()); + + int n=A.size(); + for(int i=0;i &a, vector &b, vector > &c) { + int i; + for(i=0;i<100001;i++) + dp[i]=i; + int n=a.size(); + int m=c.size(); + for(i=0;i>&mat ,vector>&dp,int N,int M) +{ + if(dp[i][j]==true) + return; + + if(mat[i][j]==0) + return; + dp[i][j]=true; + res++; + if((i-1)>=0 && !dp[i-1][j]) + dfs(i-1,j,mat,dp,N,M); + if((i+1)=0 && !dp[i][j-1]) + dfs(i,j-1,mat,dp,N,M); + if((j+1)=0 && (j-1)>=0 && !dp[i-1][j-1]) + dfs(i-1,j-1,mat,dp,N,M); + if((i-1)>=0 && (j+1)=0 && !dp[i+1][j-1]) + dfs(i+1,j-1,mat,dp,N,M); + if((i+1) >&A ) +{ + vector>vis(N,vector(M,false)); + int ans=0; + for(int i=0;i > &A) { + return findMaxArea(A.size(),A[0].size(),A); +} diff --git a/Graphs/Snake Ladder Problem!.cpp b/Graphs/Snake Ladder Problem!.cpp new file mode 100644 index 0000000..549f0f9 --- /dev/null +++ b/Graphs/Snake Ladder Problem!.cpp @@ -0,0 +1,67 @@ +unordered_map mp; + +int helper() +{ + bool *visited = new bool[100]; + for(int i = 0;i<100;i++) + { + visited[i] = false; + } + + queue q; + q.push(0); + visited[0] = true; + int cou = 0; + while(!q.empty()) + { + int x = q.size(); + while(x--) + { + int tt = q.front(); + q.pop(); + + if(tt == 99) + return cou; + + for(int i = 1;i<=6;i++) + { + if(tt + i < 100) + { + if(mp.count(i + tt) == 0) + { + if(!visited[i + tt]) + { + visited[i + tt] = true; + q.push(i + tt); + } + } + else + { + if(!visited[mp[i + tt]]) + { + visited[mp[i + tt]] = true; + q.push(mp[i + tt]); + } + } + } + } + + } + cou++; + } + return -1; +} + +int Solution::snakeLadder(vector > &A, vector > &B) { + mp.clear(); + for(int i = 0;i ii; +typedef vector vii; +typedef vector vi; +#define se second +#define fi first +#define pb push_back +int n,m,kk,s,t,pp; + +vector dist; +vector rdist; + +const int INF = 1e9; + +vi dists(100002,INF); //distance from source s to node +vi distt(100002,INF); //distance from node to target + +void dijkstras(int source, vector &v,vector &distance){ + priority_queue > pq; + pq.push(ii(0,source)); + distance[source]=0; + while(!pq.empty()){ + ii top = pq.top(); + pq.pop(); + int node = top.se; + int d = top.fi; + for(int i=0;id+neighbor.se){ + distance[neighbor.fi]=d+neighbor.se; + pq.push(ii(distance[neighbor.fi],neighbor.fi)); + } + } + } +} +int Solution::solve(int A, vector > &B, int C, int D, vector > &E) { + dists = vi(100002,INF); + distt = vi(100002, INF); + n=A; + dist = vector (n+1,vector()); + rdist = vector (n+1,vector()); + for(int i=0;i > &A, vector > &blue,vector > &red ){ + if(red[i][j]){ + ans++; + } + for(int next = 0; next<4; next++){ + + int x = i + dx[next]; + int y = j + dy[next]; + + if(x>=0 and y>=0 and x > &A) { + vector > blue(A.size(), vector(A[0].size(),0)); + vector > red(A.size(), vector(A[0].size(),0)); + ans = 0; + int N = A.size(); + int M = A[0].size(); + for(int i=0; i Date: Sat, 19 Sep 2020 01:06:48 +0530 Subject: [PATCH 47/61] Add files via upload --- Greedy/Disjoint Intervals.cpp | 32 ++++++++++++++++++++++++++++++++ Greedy/Largest Permutation.cpp | 17 +++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 Greedy/Disjoint Intervals.cpp create mode 100644 Greedy/Largest Permutation.cpp diff --git a/Greedy/Disjoint Intervals.cpp b/Greedy/Disjoint Intervals.cpp new file mode 100644 index 0000000..cd442e1 --- /dev/null +++ b/Greedy/Disjoint Intervals.cpp @@ -0,0 +1,32 @@ +int Solution::solve(vector > &A) { + + int i,j,k,n; + n = A.size(); + + pair p[n]; + + int ans=1; + + for(i=0;i pp; + pp.first=A[i][1]; + pp.second=A[i][0]; + p[i]=pp; + + } + + sort(p,p+n); + int last=p[0].first; + + + for(i=1;ilast){ + ans++; + last=p[i].first; + } + } + return ans; +} diff --git a/Greedy/Largest Permutation.cpp b/Greedy/Largest Permutation.cpp new file mode 100644 index 0000000..deda555 --- /dev/null +++ b/Greedy/Largest Permutation.cpp @@ -0,0 +1,17 @@ +vector Solution::solve(vector &C, int B) { + vector index(C); + for(int i=0;i Date: Sat, 19 Sep 2020 01:07:21 +0530 Subject: [PATCH 48/61] Add files via upload --- Hashing/An Increment Problem.cpp | 17 ++++++++++ Hashing/First Repeating element.cpp | 23 +++++++++++++ Hashing/Longest Subarray Length.cpp | 37 +++++++++++++++++++++ Hashing/Pairs With Given Xor.cpp | 15 +++++++++ Hashing/Subarray with B odd numbers.cpp | 12 +++++++ Hashing/Subarray with equal occurences!.cpp | 23 +++++++++++++ Hashing/Subarray with equal occurences.cpp | 23 +++++++++++++ Hashing/Subarray with given XOR.cpp | 18 ++++++++++ Hashing/Two out of Three.cpp | 18 ++++++++++ 9 files changed, 186 insertions(+) create mode 100644 Hashing/An Increment Problem.cpp create mode 100644 Hashing/First Repeating element.cpp create mode 100644 Hashing/Longest Subarray Length.cpp create mode 100644 Hashing/Pairs With Given Xor.cpp create mode 100644 Hashing/Subarray with B odd numbers.cpp create mode 100644 Hashing/Subarray with equal occurences!.cpp create mode 100644 Hashing/Subarray with equal occurences.cpp create mode 100644 Hashing/Subarray with given XOR.cpp create mode 100644 Hashing/Two out of Three.cpp diff --git a/Hashing/An Increment Problem.cpp b/Hashing/An Increment Problem.cpp new file mode 100644 index 0000000..b91d029 --- /dev/null +++ b/Hashing/An Increment Problem.cpp @@ -0,0 +1,17 @@ +vector Solution::solve(vector &A) { + vector res; + for(int i=0;i &A) { + + vector v(A); + unordered_set s; + + sort(A.begin(),A.end()); + int num,temp=0; + for(int i=0;i &A) { + int N = A.size(); + int arr[100001]; + int arro[100001]; + + for (int i = 0; i < 100001; i++) + arr[i] = 1000000; + + for (int i = 0; i < 100001; i++) + arro[i] = 1000000; + + int z = 0, o = 0, ans = 0; + + for (int i = 0; i < N; i++) { + if (A[i]) o++; + else z++; + if (z >= o) { + arr[z - o] = min(arr[z - o], i); + int x = z - o + 1; + if (arr[x] != 1000000) { + ans = max(ans, i - arr[x]); + } + } else if (z < o) { + if (o - z == 1) { + ans = max(ans, i + 1); + } else { + arro[o - z] = min(arro[o - z], i); + int x = o - z - 1; + if (arro[x] != 1000000) { + ans = max(ans, i - arro[x]); + } + } + } + } + + return ans; +} diff --git a/Hashing/Pairs With Given Xor.cpp b/Hashing/Pairs With Given Xor.cpp new file mode 100644 index 0000000..e6a9cb4 --- /dev/null +++ b/Hashing/Pairs With Given Xor.cpp @@ -0,0 +1,15 @@ +int Solution::solve(vector &A, int B) { + unordered_mapm(A.size()); + int ans=0; + for(int i=0;i &A, int B) { + int n = A.size(), ans = 0, s = 0; + vector v(n+1, 0); + + for(int i=0; i=0) ans += v[s-B]; + } + + return ans; +} \ No newline at end of file diff --git a/Hashing/Subarray with equal occurences!.cpp b/Hashing/Subarray with equal occurences!.cpp new file mode 100644 index 0000000..3318b3a --- /dev/null +++ b/Hashing/Subarray with equal occurences!.cpp @@ -0,0 +1,23 @@ +#define ll long long +int Solution::solve(vector &A, int B, int C) { + map mp; + ll int b_count=0; + ll int c_count=0; + ll int n=A.size(); + ll int ans=0; + for(ll int i=0;isecond-1)*it->second/2; + } + return ans; +} \ No newline at end of file diff --git a/Hashing/Subarray with equal occurences.cpp b/Hashing/Subarray with equal occurences.cpp new file mode 100644 index 0000000..3318b3a --- /dev/null +++ b/Hashing/Subarray with equal occurences.cpp @@ -0,0 +1,23 @@ +#define ll long long +int Solution::solve(vector &A, int B, int C) { + map mp; + ll int b_count=0; + ll int c_count=0; + ll int n=A.size(); + ll int ans=0; + for(ll int i=0;isecond-1)*it->second/2; + } + return ans; +} \ No newline at end of file diff --git a/Hashing/Subarray with given XOR.cpp b/Hashing/Subarray with given XOR.cpp new file mode 100644 index 0000000..54340f5 --- /dev/null +++ b/Hashing/Subarray with given XOR.cpp @@ -0,0 +1,18 @@ +int Solution::solve(vector &a, int k) { + int n = a.size(); + unordered_map map; + map.reserve(n); + int temp, ans = 0, xorpre = a[0]; + if(a[0] == k) + ans = 1; + map[a[0]] = 1; + for(int i=1; i Solution::solve(vector &A, vector &B, vector &C) { + vector a(100001,false) , b(100001,false) , c(100001,false); + + for(auto i:A) + a[i]=true; + for(auto i:B) + b[i]=true; + for(auto i:C) + c[i]=true; + + vector res; + for(int i=1;i<100001;i++) + { + if((a[i]+b[i]+c[i]) > 1) + res.push_back(i); + } + return res; +} \ No newline at end of file From f5fb772b3a7e9e4d8bfc316f244002357bc74a86 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:07:53 +0530 Subject: [PATCH 49/61] Add files via upload --- Heaps and Maps/K Largest Elements.cpp | 38 +++++++++++++++++++++ Heaps and Maps/Maximum Sum Combinations.cpp | 17 +++++++++ Heaps and Maps/Merge K sorted arrays!.cpp | 14 ++++++++ Heaps and Maps/Profit Maximisation.cpp | 14 ++++++++ 4 files changed, 83 insertions(+) create mode 100644 Heaps and Maps/K Largest Elements.cpp create mode 100644 Heaps and Maps/Maximum Sum Combinations.cpp create mode 100644 Heaps and Maps/Merge K sorted arrays!.cpp create mode 100644 Heaps and Maps/Profit Maximisation.cpp diff --git a/Heaps and Maps/K Largest Elements.cpp b/Heaps and Maps/K Largest Elements.cpp new file mode 100644 index 0000000..d7f52be --- /dev/null +++ b/Heaps and Maps/K Largest Elements.cpp @@ -0,0 +1,38 @@ +int partition(vector &A,int l,int r){ + + int pivot=A[r]; + int i=l-1; + + for(int j=l;j<=r;j++){ + + if(A[j] &A,int l,int r, int ind){ + + if(l>r) return ; + int pivot=partition(A,l,r); + + if(pivot < ind) f(A,pivot+1,r,ind); + else if(pivot > ind) f(A,l,pivot-1,ind); +} + +vector Solution::solve(vector &A, int k) { + + int n=A.size(); + f(A,0,n-1,n-k); + vector ans; + for(int i=n-k;i Solution::solve(vector &A, vector &B, int C) { + sort(A.rbegin(),A.rend()); + sort(B.rbegin(),B.rend()); + vector ans; + priority_queue > > q; + q.push({A[0]+B[0],{0,0}}); + while(C-->0){ + pair > p=q.top();q.pop(); + ans.push_back(p.first); + if(p.second.first+1 Solution::solve(vector > &A) { + int k=A.size(),n=A[0].size(); + vector ans; + for(int i=0;i &A, int B) { + priority_queue p; + for(int i:A){ + p.push(i); + } + int ans=0; + for(int i=0;i Date: Sat, 19 Sep 2020 01:08:19 +0530 Subject: [PATCH 50/61] Add files via upload --- Even Reverse.cpp | 33 +++++++++++++++++++++++++++++++++ Kth Node From Middle.cpp | 26 ++++++++++++++++++++++++++ Reverse Alternate K Nodes.cpp | 28 ++++++++++++++++++++++++++++ Sort Binary Linked List.cpp | 22 ++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 Even Reverse.cpp create mode 100644 Kth Node From Middle.cpp create mode 100644 Reverse Alternate K Nodes.cpp create mode 100644 Sort Binary Linked List.cpp diff --git a/Even Reverse.cpp b/Even Reverse.cpp new file mode 100644 index 0000000..03bc5bd --- /dev/null +++ b/Even Reverse.cpp @@ -0,0 +1,33 @@ +ListNode* Solution::solve(ListNode* A) { + ListNode* p=A; + vectorev,od; + int count=0; + while(p!=NULL) + { + if(count%2!=0) + ev.push_back(p->val); + else + od.push_back(p->val); + count++; + p=p->next; + } + reverse(ev.begin(),ev.end()); + int i=0,p1=0,p2=0; + p=A; + while(p!=NULL) + { + if(i%2!=0) + { + p->val=ev[p1]; + p1++; + } + else + { + p->val=od[p2]; + p2++; + } + p=p->next; + i++; + } + return A; +} \ No newline at end of file diff --git a/Kth Node From Middle.cpp b/Kth Node From Middle.cpp new file mode 100644 index 0000000..789b04c --- /dev/null +++ b/Kth Node From Middle.cpp @@ -0,0 +1,26 @@ +int Solution::solve(ListNode* A, int B) { + if(!A) + return -1; + ListNode* f = A, *s = A, *mid = NULL; + int k = 0; + while(f && f->next) + { + k++; + s = s->next; + f = f->next->next; + } + if(B > k) + return -1; + mid = s; + f = A, s = A; + for(int i = 0; i < B; i++) + { + f = f->next; + } + while(f != mid) + { + s = s->next; + f = f->next; + } + return s->val; +} diff --git a/Reverse Alternate K Nodes.cpp b/Reverse Alternate K Nodes.cpp new file mode 100644 index 0000000..0a1e579 --- /dev/null +++ b/Reverse Alternate K Nodes.cpp @@ -0,0 +1,28 @@ +ListNode* Solution::solve(ListNode* A, int B) { + ListNode* start = A; + ListNode* end = A; + int n = 1,c = 1; + while(start && end){ + stack st; + n = B; + if(c % 2 == 1){ + while(n--){ + st.push(end->val); + end = end->next; + } + while(!st.empty()){ + start->val = st.top(); + st.pop(); + start = start->next; + } + } + else{ + while(n--){ + start = start->next; + end = end->next; + } + } + c++; + } + return A; +} \ No newline at end of file diff --git a/Sort Binary Linked List.cpp b/Sort Binary Linked List.cpp new file mode 100644 index 0000000..00e2c49 --- /dev/null +++ b/Sort Binary Linked List.cpp @@ -0,0 +1,22 @@ +ListNode* Solution::solve(ListNode* head) { + ListNode dummy1(0); + ListNode* zero = &dummy1; + ListNode dummy2(0); + ListNode* one = &dummy2; + + while(head){ + if(head->val == 0){ + zero->next = head; + zero = zero->next; + head = head->next; + } + else if(head->val == 1){ + one->next = head; + one = one->next; + head = head->next; + } + } + one->next = NULL; + zero->next = dummy2.next; + return dummy1.next; +} \ No newline at end of file From a26e2192700153503323e88cbc8e870da0c5a1f5 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:08:50 +0530 Subject: [PATCH 51/61] Add files via upload --- Math/Distribute in Circle!.cpp | 4 ++ Math/Find Nth Fibonacci.cpp | 58 +++++++++++++++++++++++++ Math/Is Rectangle.cpp | 20 +++++++++ Math/Next Similar Number.cpp | 41 ++++++++++++++++++ Math/Next Smallest Palindrome!.cpp | 68 ++++++++++++++++++++++++++++++ Math/Step by Step.cpp | 13 ++++++ Math/Total Moves For Bishop!.cpp | 10 +++++ 7 files changed, 214 insertions(+) create mode 100644 Math/Distribute in Circle!.cpp create mode 100644 Math/Find Nth Fibonacci.cpp create mode 100644 Math/Is Rectangle.cpp create mode 100644 Math/Next Similar Number.cpp create mode 100644 Math/Next Smallest Palindrome!.cpp create mode 100644 Math/Step by Step.cpp create mode 100644 Math/Total Moves For Bishop!.cpp diff --git a/Math/Distribute in Circle!.cpp b/Math/Distribute in Circle!.cpp new file mode 100644 index 0000000..2a5dfcb --- /dev/null +++ b/Math/Distribute in Circle!.cpp @@ -0,0 +1,4 @@ +int Solution::solve(int A, int B, int C) { + + return (A%B+C%B)%B-1; +} \ No newline at end of file diff --git a/Math/Find Nth Fibonacci.cpp b/Math/Find Nth Fibonacci.cpp new file mode 100644 index 0000000..b463df3 --- /dev/null +++ b/Math/Find Nth Fibonacci.cpp @@ -0,0 +1,58 @@ +#define ll long long +#define M 1000000007 +struct mat +{ + ll a,b,c,d; +}; +mat po(ll n) +{ + if(n==1) + { + mat in; + in.a=1; + in.b=1; + in.c=1; + in.d=0; + return in; + } + if(n==2) + { + mat in; + in.a=2; + in.b=1; + in.c=1; + in.d=1; + return in; + } + else + { + mat a=po(n/2); + mat b; + b.a=(a.a)*(a.a)+(a.b)*(a.c); + b.b=(a.a)*(a.b)+(a.b)*(a.d); + b.c=(a.a)*(a.c)+(a.c)*(a.d); + b.d=(a.b)*(a.c)+(a.d)*(a.d); + b.a%=M; + b.b%=M; + b.c%=M; + b.d%=M; + if(n%2==1) + { + mat c; + c.a=b.a+b.b; + c.b=b.a; + c.c=b.c+b.d; + c.d=b.c; + c.a%=M; + c.b%=M; + c.c%=M; + c.d%=M; + return c; + } + return b; + } +} +int Solution::solve(int A) { + mat re=po(A); + return re.c; +} \ No newline at end of file diff --git a/Math/Is Rectangle.cpp b/Math/Is Rectangle.cpp new file mode 100644 index 0000000..f2bd0ed --- /dev/null +++ b/Math/Is Rectangle.cpp @@ -0,0 +1,20 @@ +int Solution::solve(int A, int B, int C, int D) { + map m; + m[A]++; + m[B]++; + m[C]++; + m[D]++; + + if(m.size()==2) + { + for(auto it: m) + { + if(it.second!=2){return 0;} + } + return 1; + } + else + { + return 0; + } +} diff --git a/Math/Next Similar Number.cpp b/Math/Next Similar Number.cpp new file mode 100644 index 0000000..89882a2 --- /dev/null +++ b/Math/Next Similar Number.cpp @@ -0,0 +1,41 @@ +int num(char c) +{ + return c - '0'; +} +void reverse(string& A,int i,int j) +{ + while(i=0;i--) + { + if(num(A[i])i;j--) + { + if(num(A[j])>num(A[i])) + break; + } + // swap with this number + swap(A[j],A[i]); + + // remaining numbers from A[i+1] to A[j] // all are greater than 3 and sholud ber reversed + reverse(A,i+1,n-1); + return A; +} \ No newline at end of file diff --git a/Math/Next Smallest Palindrome!.cpp b/Math/Next Smallest Palindrome!.cpp new file mode 100644 index 0000000..f185c04 --- /dev/null +++ b/Math/Next Smallest Palindrome!.cpp @@ -0,0 +1,68 @@ +bool allNine(string s) +{ + for(int i=0; i=0 && s[i]==s[j]) + { + i--; + j++; + } + if(i<0 || s[i]-'0'=0) + { + s[j]=s[i]; + i--; + j++; + } + if(leftSmaller==true) + { + i=mid-1; + int carry=1; + if(n%2==1) + { + int x=(s[mid]-'0')+carry; + carry=x/10; + x=x%10; + s[mid]=x+'0'; + j=mid+1; + } + else + j=mid; + while(i>=0) + { + int x=(s[i]-'0')+carry; + carry=x/10; + x=x%10; + s[i]=x+'0'; + s[j++]=s[i--]; + } + } + return s; + } +} diff --git a/Math/Step by Step.cpp b/Math/Step by Step.cpp new file mode 100644 index 0000000..16eff1f --- /dev/null +++ b/Math/Step by Step.cpp @@ -0,0 +1,13 @@ +int Solution::solve(int A) { +int target = abs(A); +int n = ceil((-1.0 + sqrt(1 + 8.0 * target)) / 2); +int sum = n * (n + 1) / 2; +if (sum == target) +return n; + +int d = sum - target; +if ((d & 1) == 0) + return n; +else + return n + ((n & 1) ? 2 : 1); +} \ No newline at end of file diff --git a/Math/Total Moves For Bishop!.cpp b/Math/Total Moves For Bishop!.cpp new file mode 100644 index 0000000..2c01286 --- /dev/null +++ b/Math/Total Moves For Bishop!.cpp @@ -0,0 +1,10 @@ +int Solution::solve(int A, int B) { + int ans = 0; + + ans += min(A-1,B-1); + ans += min(A-1,8-B); + ans += min(8-A,B-1); + ans += min(8-A,8-B); + + return ans; +} \ No newline at end of file From 8c74adafd435fd73673f0b0c8b0259fef3f72831 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:09:12 +0530 Subject: [PATCH 52/61] Add files via upload --- Stacks and Queues/Balanced Parantheses!.cpp | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Stacks and Queues/Balanced Parantheses!.cpp diff --git a/Stacks and Queues/Balanced Parantheses!.cpp b/Stacks and Queues/Balanced Parantheses!.cpp new file mode 100644 index 0000000..392c1ab --- /dev/null +++ b/Stacks and Queues/Balanced Parantheses!.cpp @@ -0,0 +1,24 @@ +int Solution::solve(string str) +{ + int n=str.length(),i; + if(n==1) + return 0; + stacks; + for(i=0;i Date: Sat, 19 Sep 2020 01:09:42 +0530 Subject: [PATCH 53/61] Add files via upload --- Strings/Convert to Palindrome.cpp | 35 +++++++++++++++++++++ Strings/Minimum Appends for Palindrome!.cpp | 25 +++++++++++++++ Strings/Minimum Parantheses!.cpp | 27 ++++++++++++++++ Strings/Remove Consecutive Characters.cpp | 20 ++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 Strings/Convert to Palindrome.cpp create mode 100644 Strings/Minimum Appends for Palindrome!.cpp create mode 100644 Strings/Minimum Parantheses!.cpp create mode 100644 Strings/Remove Consecutive Characters.cpp diff --git a/Strings/Convert to Palindrome.cpp b/Strings/Convert to Palindrome.cpp new file mode 100644 index 0000000..379b457 --- /dev/null +++ b/Strings/Convert to Palindrome.cpp @@ -0,0 +1,35 @@ +bool palindrome(string ::iterator itr,string :: iterator itr1) +{ + while(itr Date: Sat, 19 Sep 2020 01:10:12 +0530 Subject: [PATCH 54/61] Add files via upload --- Trees/Burn a Tree.cpp | 27 ++++++++++++ Trees/Maximum Edge Removal.cpp | 39 +++++++++++++++++ Trees/Maximum level sum.cpp | 22 ++++++++++ Trees/Merge two Binary Tree.cpp | 10 +++++ Trees/Reverse Level Order.cpp | 17 ++++++++ Trees/Xor Between Two Arrays!.cpp | 69 +++++++++++++++++++++++++++++++ 6 files changed, 184 insertions(+) create mode 100644 Trees/Burn a Tree.cpp create mode 100644 Trees/Maximum Edge Removal.cpp create mode 100644 Trees/Maximum level sum.cpp create mode 100644 Trees/Merge two Binary Tree.cpp create mode 100644 Trees/Reverse Level Order.cpp create mode 100644 Trees/Xor Between Two Arrays!.cpp diff --git a/Trees/Burn a Tree.cpp b/Trees/Burn a Tree.cpp new file mode 100644 index 0000000..9d2a58b --- /dev/null +++ b/Trees/Burn a Tree.cpp @@ -0,0 +1,27 @@ +bool sol(TreeNode*root,int &h,int &ans,int B) +{ + if(root==NULL) + return false; + int hl=0,hr=0; + bool lf=sol(root->left,hl,ans,B); + bool rf=sol(root->right,hr,ans,B); + if(!lf&&!rf) + h=1+max(hl,hr); + else + if(lf) + h=hl+1; + else + h=hr+1; + if(B==root->val||lf||rf) + { + ans=max(ans,hl+hr+1); + return true; + } + else + return false; +} +int Solution::solve(TreeNode* A, int B) { + int ans=0,h=0; + sol(A,h,ans,B); + return ans-1; +} diff --git a/Trees/Maximum Edge Removal.cpp b/Trees/Maximum Edge Removal.cpp new file mode 100644 index 0000000..d28f67b --- /dev/null +++ b/Trees/Maximum Edge Removal.cpp @@ -0,0 +1,39 @@ +vectoradjList[100005]; +bool visit[100005]; +int count = 0; + +int dfs(int u, int& res) +{ + visit[u] = true; + int currComponentNode = 0; + + for (int i = 0; i < adjList[u].size(); i++) + { + int v = adjList[u][i]; + + int subtreeNodeCount = dfs(v, res); + if (subtreeNodeCount % 2 == 0) + res++; + else + currComponentNode += subtreeNodeCount; + + } + + return (currComponentNode + 1); +} + +int Solution::solve(int A, vector > &B) { + + for(int i = 0; i <= A; i++) + adjList[i].clear(); + + for(int i = 0; i < B.size(); i++) + adjList[B[i][0]].push_back(B[i][1]); + + for (int i = 0; i <= A; i++) + visit[i] = false; + + int res = 0; + dfs(1, res); + return res; +} \ No newline at end of file diff --git a/Trees/Maximum level sum.cpp b/Trees/Maximum level sum.cpp new file mode 100644 index 0000000..3da5c1c --- /dev/null +++ b/Trees/Maximum level sum.cpp @@ -0,0 +1,22 @@ +int Solution::solve(TreeNode* A) { + if(A==NULL) return 0; + int ans = A->val, sz,sum; + queue q; + q.push(A); + TreeNode* p; + + while(!q.empty()){ + sz=q.size(); + sum=0; + while(sz--){ + p=q.front(); + sum+=(p->val); + q.pop(); + if(p->left) q.push(p->left); + if(p->right) q.push(p->right); + } + ans = max(sum,ans); + } + + return ans; +} diff --git a/Trees/Merge two Binary Tree.cpp b/Trees/Merge two Binary Tree.cpp new file mode 100644 index 0000000..fd6d871 --- /dev/null +++ b/Trees/Merge two Binary Tree.cpp @@ -0,0 +1,10 @@ +TreeNode* Solution::solve(TreeNode* A, TreeNode* B) +{ + if (!A) return B; + else if (!B) return A; + + A->val += B->val; + A->left = solve(A->left,B->left); + A->right = solve(A->right,B->right); + return A; +} \ No newline at end of file diff --git a/Trees/Reverse Level Order.cpp b/Trees/Reverse Level Order.cpp new file mode 100644 index 0000000..86b6e64 --- /dev/null +++ b/Trees/Reverse Level Order.cpp @@ -0,0 +1,17 @@ +vector Solution::solve(TreeNode* A) { + vector G,i; + queue Q; + TreeNode* root = A; + Q.push(A); + while(!Q.empty()){ + root = Q.front(); + Q.pop(); + G.push_back(root->val); + if(root->right) Q.push(root->right); + if(root->left) Q.push(root->left); + } + for(int j = G.size()-1;j>=0;j--){ + cout<=0;i--){ + int bit=(n>>i)&1; + if(bit==0){ + if((temp->left)!=NULL)temp=temp->left; + else{ + trienode* nw = new trienode(); + temp->left=nw; + temp=nw; + } + } + else{ + if((temp->right)!=NULL)temp=temp->right; + else{ + trienode* nw = new trienode(); + temp->right = nw; + temp=nw; + } + } + } + } + void findmxor(int n){ + int ans=0; + trienode* temp=root; + for(int i=31;i>=0;i--){ + int bit = (n>>i)&1; + if(bit==0){ + if((temp->right) != NULL){ + ans+=(1<right; + } + else temp=temp->left; + } + else{ + if((temp->left) !=NULL){ + ans+=(1<left; + } + else temp=temp->right; + } + } + + if(ans>mxor)mxor=ans; + } +}; +int Solution::solve(vector &A, vector &B) { + Trie t; + for(int i=0;i Date: Sat, 19 Sep 2020 01:10:54 +0530 Subject: [PATCH 55/61] Add files via upload --- Two Pointers/Counting Subarrays!.cpp | 19 +++++++++++ .../Maximum Ones After Modification.cpp | 32 +++++++++++++++++++ Two Pointers/Pair With Given Difference.cpp | 18 +++++++++++ .../Subarrays with distinct integers!.cpp | 13 ++++++++ 4 files changed, 82 insertions(+) create mode 100644 Two Pointers/Counting Subarrays!.cpp create mode 100644 Two Pointers/Maximum Ones After Modification.cpp create mode 100644 Two Pointers/Pair With Given Difference.cpp create mode 100644 Two Pointers/Subarrays with distinct integers!.cpp diff --git a/Two Pointers/Counting Subarrays!.cpp b/Two Pointers/Counting Subarrays!.cpp new file mode 100644 index 0000000..3604b96 --- /dev/null +++ b/Two Pointers/Counting Subarrays!.cpp @@ -0,0 +1,19 @@ +int Solution::solve(vector &A, int B) { + int i=0,j=0,count=0; + int sum=0; + while(j &A, int B) { + + int count = 0; + int gap = 0; + int r = 0; + int l = 0; + + while(r < A.size()) + { + if(A[r] == 0) + { + count++; + } + if(count > B) + { + if(A[l] == 0) + { + count--; + } + l++; + } + if(count <= B) + { + if(r-l > gap) + { + gap = r-l; + } + } + r++; + } + return gap+1; +} \ No newline at end of file diff --git a/Two Pointers/Pair With Given Difference.cpp b/Two Pointers/Pair With Given Difference.cpp new file mode 100644 index 0000000..7450190 --- /dev/null +++ b/Two Pointers/Pair With Given Difference.cpp @@ -0,0 +1,18 @@ +int Solution::solve(vector &A, int B) { + if(A.empty()) + return 0; +sort(A.begin(), A.end()); + +int i=0; +int j=0; +while(i < A.size() && j < A.size()){ + int k = A[j] - A[i]; + if(k == B && i != j) + return 1; + else if(k < B) + j++; + else + i++; +} +return 0; +} diff --git a/Two Pointers/Subarrays with distinct integers!.cpp b/Two Pointers/Subarrays with distinct integers!.cpp new file mode 100644 index 0000000..3638698 --- /dev/null +++ b/Two Pointers/Subarrays with distinct integers!.cpp @@ -0,0 +1,13 @@ +int Solution::solve(vector &A, int B) { + + int res = 0; + int K = B; + vector m(A.size() + 1); + for(auto i = 0, j = 0, prefix = 0, cnt = 0; i < A.size(); ++i) { + if (m[A[i]]++ == 0) ++cnt; + if (cnt > K) --m[A[j++]], --cnt, prefix = 0; + while (m[A[j]] > 1) ++prefix, --m[A[j++]]; + if (cnt == K) res += prefix + 1; + } + return res; +} \ No newline at end of file From 972a1d93fcc26e202af6b41baa926956a4cc0f75 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:11:21 +0530 Subject: [PATCH 56/61] Add files via upload --- Linked Lists/Even Reverse.cpp | 33 ++++++++++++++++++++++ Linked Lists/Kth Node From Middle.cpp | 26 +++++++++++++++++ Linked Lists/Reverse Alternate K Nodes.cpp | 28 ++++++++++++++++++ Linked Lists/Sort Binary Linked List.cpp | 22 +++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 Linked Lists/Even Reverse.cpp create mode 100644 Linked Lists/Kth Node From Middle.cpp create mode 100644 Linked Lists/Reverse Alternate K Nodes.cpp create mode 100644 Linked Lists/Sort Binary Linked List.cpp diff --git a/Linked Lists/Even Reverse.cpp b/Linked Lists/Even Reverse.cpp new file mode 100644 index 0000000..03bc5bd --- /dev/null +++ b/Linked Lists/Even Reverse.cpp @@ -0,0 +1,33 @@ +ListNode* Solution::solve(ListNode* A) { + ListNode* p=A; + vectorev,od; + int count=0; + while(p!=NULL) + { + if(count%2!=0) + ev.push_back(p->val); + else + od.push_back(p->val); + count++; + p=p->next; + } + reverse(ev.begin(),ev.end()); + int i=0,p1=0,p2=0; + p=A; + while(p!=NULL) + { + if(i%2!=0) + { + p->val=ev[p1]; + p1++; + } + else + { + p->val=od[p2]; + p2++; + } + p=p->next; + i++; + } + return A; +} \ No newline at end of file diff --git a/Linked Lists/Kth Node From Middle.cpp b/Linked Lists/Kth Node From Middle.cpp new file mode 100644 index 0000000..789b04c --- /dev/null +++ b/Linked Lists/Kth Node From Middle.cpp @@ -0,0 +1,26 @@ +int Solution::solve(ListNode* A, int B) { + if(!A) + return -1; + ListNode* f = A, *s = A, *mid = NULL; + int k = 0; + while(f && f->next) + { + k++; + s = s->next; + f = f->next->next; + } + if(B > k) + return -1; + mid = s; + f = A, s = A; + for(int i = 0; i < B; i++) + { + f = f->next; + } + while(f != mid) + { + s = s->next; + f = f->next; + } + return s->val; +} diff --git a/Linked Lists/Reverse Alternate K Nodes.cpp b/Linked Lists/Reverse Alternate K Nodes.cpp new file mode 100644 index 0000000..0a1e579 --- /dev/null +++ b/Linked Lists/Reverse Alternate K Nodes.cpp @@ -0,0 +1,28 @@ +ListNode* Solution::solve(ListNode* A, int B) { + ListNode* start = A; + ListNode* end = A; + int n = 1,c = 1; + while(start && end){ + stack st; + n = B; + if(c % 2 == 1){ + while(n--){ + st.push(end->val); + end = end->next; + } + while(!st.empty()){ + start->val = st.top(); + st.pop(); + start = start->next; + } + } + else{ + while(n--){ + start = start->next; + end = end->next; + } + } + c++; + } + return A; +} \ No newline at end of file diff --git a/Linked Lists/Sort Binary Linked List.cpp b/Linked Lists/Sort Binary Linked List.cpp new file mode 100644 index 0000000..00e2c49 --- /dev/null +++ b/Linked Lists/Sort Binary Linked List.cpp @@ -0,0 +1,22 @@ +ListNode* Solution::solve(ListNode* head) { + ListNode dummy1(0); + ListNode* zero = &dummy1; + ListNode dummy2(0); + ListNode* one = &dummy2; + + while(head){ + if(head->val == 0){ + zero->next = head; + zero = zero->next; + head = head->next; + } + else if(head->val == 1){ + one->next = head; + one = one->next; + head = head->next; + } + } + one->next = NULL; + zero->next = dummy2.next; + return dummy1.next; +} \ No newline at end of file From 490eabfbbae725c6043b0315acf3148467bb5180 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:11:42 +0530 Subject: [PATCH 57/61] Delete Even Reverse.cpp --- Even Reverse.cpp | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 Even Reverse.cpp diff --git a/Even Reverse.cpp b/Even Reverse.cpp deleted file mode 100644 index 03bc5bd..0000000 --- a/Even Reverse.cpp +++ /dev/null @@ -1,33 +0,0 @@ -ListNode* Solution::solve(ListNode* A) { - ListNode* p=A; - vectorev,od; - int count=0; - while(p!=NULL) - { - if(count%2!=0) - ev.push_back(p->val); - else - od.push_back(p->val); - count++; - p=p->next; - } - reverse(ev.begin(),ev.end()); - int i=0,p1=0,p2=0; - p=A; - while(p!=NULL) - { - if(i%2!=0) - { - p->val=ev[p1]; - p1++; - } - else - { - p->val=od[p2]; - p2++; - } - p=p->next; - i++; - } - return A; -} \ No newline at end of file From ec9d65c5cb58cfd95a593c7da5e223dbcebeed25 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:11:55 +0530 Subject: [PATCH 58/61] Delete Kth Node From Middle.cpp --- Kth Node From Middle.cpp | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 Kth Node From Middle.cpp diff --git a/Kth Node From Middle.cpp b/Kth Node From Middle.cpp deleted file mode 100644 index 789b04c..0000000 --- a/Kth Node From Middle.cpp +++ /dev/null @@ -1,26 +0,0 @@ -int Solution::solve(ListNode* A, int B) { - if(!A) - return -1; - ListNode* f = A, *s = A, *mid = NULL; - int k = 0; - while(f && f->next) - { - k++; - s = s->next; - f = f->next->next; - } - if(B > k) - return -1; - mid = s; - f = A, s = A; - for(int i = 0; i < B; i++) - { - f = f->next; - } - while(f != mid) - { - s = s->next; - f = f->next; - } - return s->val; -} From d1d389a6d3424c1abfc15bf667aff270cad62983 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:12:08 +0530 Subject: [PATCH 59/61] Delete Reverse Alternate K Nodes.cpp --- Reverse Alternate K Nodes.cpp | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 Reverse Alternate K Nodes.cpp diff --git a/Reverse Alternate K Nodes.cpp b/Reverse Alternate K Nodes.cpp deleted file mode 100644 index 0a1e579..0000000 --- a/Reverse Alternate K Nodes.cpp +++ /dev/null @@ -1,28 +0,0 @@ -ListNode* Solution::solve(ListNode* A, int B) { - ListNode* start = A; - ListNode* end = A; - int n = 1,c = 1; - while(start && end){ - stack st; - n = B; - if(c % 2 == 1){ - while(n--){ - st.push(end->val); - end = end->next; - } - while(!st.empty()){ - start->val = st.top(); - st.pop(); - start = start->next; - } - } - else{ - while(n--){ - start = start->next; - end = end->next; - } - } - c++; - } - return A; -} \ No newline at end of file From dd00a8bc5105c561b3416cd9d6ad12a87798f226 Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 19 Sep 2020 01:12:20 +0530 Subject: [PATCH 60/61] Delete Sort Binary Linked List.cpp --- Sort Binary Linked List.cpp | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 Sort Binary Linked List.cpp diff --git a/Sort Binary Linked List.cpp b/Sort Binary Linked List.cpp deleted file mode 100644 index 00e2c49..0000000 --- a/Sort Binary Linked List.cpp +++ /dev/null @@ -1,22 +0,0 @@ -ListNode* Solution::solve(ListNode* head) { - ListNode dummy1(0); - ListNode* zero = &dummy1; - ListNode dummy2(0); - ListNode* one = &dummy2; - - while(head){ - if(head->val == 0){ - zero->next = head; - zero = zero->next; - head = head->next; - } - else if(head->val == 1){ - one->next = head; - one = one->next; - head = head->next; - } - } - one->next = NULL; - zero->next = dummy2.next; - return dummy1.next; -} \ No newline at end of file From 6cf8aaaa9b5eeecd6bb698527920288809f74d7f Mon Sep 17 00:00:00 2001 From: black-shadows <31254584+black-shadows@users.noreply.github.com> Date: Sat, 16 Jan 2021 18:16:14 +0530 Subject: [PATCH 61/61] Add files via upload --- Graphs/Two Teams.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Graphs/Two Teams.cpp diff --git a/Graphs/Two Teams.cpp b/Graphs/Two Teams.cpp new file mode 100644 index 0000000..d2c2845 --- /dev/null +++ b/Graphs/Two Teams.cpp @@ -0,0 +1,39 @@ +typedef vector vi; +const int mxSize=1e5+1; +vi adj[mxSize]; +bool dfs(int src,vector& vis,vi& grpId,int grpVal) +{ + grpId[src]=grpVal; + vis[src]=true; + int n=adj[src].size(); + for(int i=0;i &B) { + int m=B.size(); + vi grpId(A,-1); + vector vis(A,false); + for(int i=0;i