From 61fdd2904259cafca3ed993cbf75ac0b3ed1ca97 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 17 Nov 2022 00:18:32 +0530 Subject: [PATCH 01/83] Create 17 NOV Count of Subarrays Count of Subarrays code added --- 17 NOV Count of Subarrays | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 17 NOV Count of Subarrays diff --git a/17 NOV Count of Subarrays b/17 NOV Count of Subarrays new file mode 100644 index 00000000..2878e253 --- /dev/null +++ b/17 NOV Count of Subarrays @@ -0,0 +1,23 @@ +class Solution{ +public: + // #define ll long long + + ll countSubarray(int arr[], int n, int k) { + // code here + int l=-1, r=n; + long long ans=0; + + for(int i=0; i k) { + long long left = i-l; + long long right = r-i; + ans += left*right; + + l=i; + } + } + + return ans; + } + +}; From 6caa51ed65386e70c5b5fa9036bd50ef6a09cf8e Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 18 Nov 2022 23:17:00 +0530 Subject: [PATCH 02/83] Create 18 NOV Number Of Open Doors code added --- 18 NOV Number Of Open Doors | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 18 NOV Number Of Open Doors diff --git a/18 NOV Number Of Open Doors b/18 NOV Number Of Open Doors new file mode 100644 index 00000000..1c158929 --- /dev/null +++ b/18 NOV Number Of Open Doors @@ -0,0 +1,7 @@ +class Solution { + public: + int noOfOpenDoors(long long N) { + // code here + return floor(sqrt(N)); + } +}; From 393f2a4c17039479a1d8293087644789c1252ba8 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 19 Nov 2022 23:40:15 +0530 Subject: [PATCH 03/83] Create 19 November Check if all levels of two trees are anagrams or not 19 November Check if all levels of two trees are anagrams or not --- ...ll levels of two trees are anagrams or not | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 19 November Check if all levels of two trees are anagrams or not diff --git a/19 November Check if all levels of two trees are anagrams or not b/19 November Check if all levels of two trees are anagrams or not new file mode 100644 index 00000000..80c29b1c --- /dev/null +++ b/19 November Check if all levels of two trees are anagrams or not @@ -0,0 +1,33 @@ +class Solution{ + public: + void levelOrdertraversal(Node * root,vector> &ans){ + if(root==NULL)return; + queue q; + q.push(root); + while(!q.empty()){ + int n = q.size(); + vector temp; + for(int i =0;ileft != NULL)q.push(node->left); + if(node->right != NULL)q.push(node->right); + temp.push_back(node->data); + } + sort(temp.begin(),temp.end()); + ans.push_back(temp); + + } + } + bool areAnagrams(Node *root1, Node *root2) + { + vector> ans1; + vector> ans2; + levelOrdertraversal(root1,ans1); + levelOrdertraversal(root2,ans2); + + if(ans1.size()!=ans2.size())return false; + + for(int i =0;i Date: Sun, 20 Nov 2022 23:20:01 +0530 Subject: [PATCH 04/83] Create 20 November LCM Triplet 20 November LCM Triplet --- 20 November LCM Triplet | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 20 November LCM Triplet diff --git a/20 November LCM Triplet b/20 November LCM Triplet new file mode 100644 index 00000000..19da5dcb --- /dev/null +++ b/20 November LCM Triplet @@ -0,0 +1,9 @@ +class Solution { + public: + long long lcmTriplets(long long N) { + if(N < 3) return N; + else if(N % 2!=0) return (N*(N-1)*(N-2)); + else if(N%2==0 && N % 3!=0) return (N*(N-1)*(N-3)); + else if(N%2==0 && N%3==0) return ((N-1)*(N-2)*(N-3)); + } +}; From 32913e1106c6ed1468fdf61c15a86954bbf09de4 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 21 Nov 2022 23:34:35 +0530 Subject: [PATCH 05/83] Create 21 November Unique partitions 21 November Unique partitions --- 21 November Unique partitions | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 21 November Unique partitions diff --git a/21 November Unique partitions b/21 November Unique partitions new file mode 100644 index 00000000..1f8836fe --- /dev/null +++ b/21 November Unique partitions @@ -0,0 +1,23 @@ +class Solution{ + public: + void solve(int i,int n,int sum,vector&v,vectortemp,vector>&ans){ + if(sum>n)return; + if(i>=n){ + if(sum==n)ans.push_back(temp); + return; + } + + temp.push_back(v[i]); + solve(i,n,sum+v[i],v,temp,ans); + temp.pop_back(); + solve(i+1,n,sum,v,temp,ans); + } + vector> UniquePartitions(int n) { + vectorv; + for(int i=n;i>=1;i--)v.push_back(i); + vector>ans; + vectortemp; + solve(0,n,0,v,temp,ans); + return ans; + } +}; From 98b03e66b0f59daf7ff0c5269b16f4193e813332 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 23 Nov 2022 22:05:37 +0530 Subject: [PATCH 06/83] Create 23 NOV 23 November Maximum Sum LCM 23 NOV 23 November Maximum Sum LCM --- 23 NOV 23 November Maximum Sum LCM | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 23 NOV 23 November Maximum Sum LCM diff --git a/23 NOV 23 November Maximum Sum LCM b/23 NOV 23 November Maximum Sum LCM new file mode 100644 index 00000000..65b1b758 --- /dev/null +++ b/23 NOV 23 November Maximum Sum LCM @@ -0,0 +1,19 @@ +class Solution { + public: + long long int maxSumLCM(int n) + { + // code here + long long int res=0,i; + for(i=1;i<=sqrt(n);i++) + { + if(n%i==0 && n/i!=i) + { + res+=n/i; + res+=i; + } + else if(n%i==0 && n/i==i) + res+=i; + } + return res; + } +}; From d93b55c9b2bc55bda2662402ab392dd289a04556 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 24 Nov 2022 01:51:48 +0530 Subject: [PATCH 07/83] Create 4 November Longest Bitonic subsequence 4 November Longest Bitonic subsequence --- 4 November Longest Bitonic subsequence | 76 +++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 4 November Longest Bitonic subsequence diff --git a/4 November Longest Bitonic subsequence b/4 November Longest Bitonic subsequence new file mode 100644 index 00000000..2f13ad9b --- /dev/null +++ b/4 November Longest Bitonic subsequence @@ -0,0 +1,76 @@ +class Solution{ + public: + int LongestBitonicSequence(vectornums) + + { + + // code here + + int n=nums.size(); + + vectorlis(n); + + for(int i=0;ilds(n); + + reverse(nums.begin(),nums.end()); + + for(int i=0;idiff(n); + + for(int i=0;i Date: Mon, 28 Nov 2022 02:50:04 +0530 Subject: [PATCH 08/83] Create 28 NOV 28 November Maximum Sub Array 28 November Maximum Sub Array --- 28 NOV 28 November Maximum Sub Array | 56 +++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 28 NOV 28 November Maximum Sub Array diff --git a/28 NOV 28 November Maximum Sub Array b/28 NOV 28 November Maximum Sub Array new file mode 100644 index 00000000..2eada1a8 --- /dev/null +++ b/28 NOV 28 November Maximum Sub Array @@ -0,0 +1,56 @@ +class Solution{ +public: + vector findSubarray(int a[], int n) { + int s=0,e=0, ms=-1, me=-1,l=0; + long long sum = 0, maxs = 0; + for(int i=0;i= maxs){ + + //ms = s; + //me = i; + //maxs = sum; + + if(sum>maxs) + { + ms = s; + me = i; + + + } + else + { + if(i-s+1 > l) + ms = s; + me = i; + } + maxs = sum; + l=me-ms+1; + } + + } + } + + if(ms == -1 and me == -1) return {-1}; + + vector res; + + for(int i=ms;i<=me;i++){ + res.push_back(a[i]); + } + + + return res; + } +}; From 4a9047acbdfe25ccb31eef766bd2d211abde4801 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 3 Dec 2022 21:18:41 +0530 Subject: [PATCH 09/83] Create 03 December Aggressive Cows 03 December Aggressive Cows --- 03 December Aggressive Cows | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 03 December Aggressive Cows diff --git a/03 December Aggressive Cows b/03 December Aggressive Cows new file mode 100644 index 00000000..15b1d1a6 --- /dev/null +++ b/03 December Aggressive Cows @@ -0,0 +1,28 @@ +class Solution { +public: + + int solve(int n, int k, vector &stalls) { + sort(stalls.begin(),stalls.end()); + int l=0,r=stalls[n-1]-stalls[0]; + int ans=0; + while(l<=r){ + int mid=(l+r)/2; + int cow=1; + int last=stalls[0]; + for(int i=1;i=mid){ + cow++; + last=stalls[i]; + } + } + if(cow>=k){ + ans=mid; + l=mid+1; + } + else{ + r=mid-1; + } + } + return ans; + } +}; From e99820a337a9d443971bac0a76393363285259cf Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 4 Dec 2022 17:05:21 +0530 Subject: [PATCH 10/83] Create 4 DEC 04 December Alternate Vowel and Consonant String 04 December Alternate Vowel and Consonant String --- ...mber Alternate Vowel and Consonant String | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 4 DEC 04 December Alternate Vowel and Consonant String diff --git a/4 DEC 04 December Alternate Vowel and Consonant String b/4 DEC 04 December Alternate Vowel and Consonant String new file mode 100644 index 00000000..49f276bb --- /dev/null +++ b/4 DEC 04 December Alternate Vowel and Consonant String @@ -0,0 +1,116 @@ +class Solution{ + public: + + void SortingHelper(vector&v) + { + int count[26]={0}; + for(int i=0;i<(int)v.size();i++) + { + count[v[i]-'a']++; + } + v.clear(); + for(int i=0;i<26;i++) + { + while(count[i]) + { + v.push_back('a'+i); + count[i]--; + } + } + } + string rearrange (string s, int N) + { + vectorvowel,con; + unordered_setdict={'a','e','i','o','u'}; + for(int i=0;i1) + { + return "-1"; + } + SortingHelper(vowel); + SortingHelper(con); + string ans=""; + if((int)vowel.size()>(int)con.size()) + { + int i=0,j=0; + while(i Date: Mon, 5 Dec 2022 21:47:02 +0530 Subject: [PATCH 11/83] Create 05 December Shortest Path by Removing K walls 05 December Shortest Path by Removing K walls --- ...ecember Shortest Path by Removing K walls | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 05 December Shortest Path by Removing K walls diff --git a/05 December Shortest Path by Removing K walls b/05 December Shortest Path by Removing K walls new file mode 100644 index 00000000..5d1c0ce8 --- /dev/null +++ b/05 December Shortest Path by Removing K walls @@ -0,0 +1,35 @@ +class Solution { + public: + int shotestPath(vector> mat, int n, int m, int k) { + vector> lives(n, vector (m, -1)); + queue> q; + // q-> row, col, k, dist + q.push({0, 0, k, 0}); + + int dx[] = {-1, 0, 1, 0, -1}; + while(!q.empty()) { + vector temp = q.front(); + q.pop(); + + int row = temp[0]; + int col = temp[1]; + int clives = temp[2]; + int dist = temp[3]; + + if(row == n-1 && col == m-1) return dist; + + if(mat[row][col] == 1) clives--; + + for(int i=0; i<4; i++) { + int nrow = row + dx[i]; + int ncol = col + dx[i+1]; + + if(nrow >= 0 && nrow < n && ncol >= 0 && ncol < m && lives[nrow][ncol] < clives) { + q.push({nrow, ncol, clives, dist+1}); + lives[nrow][ncol] = clives; + } + } + } + return -1; + } +}; From d9de051a23dbf4a903bf81c47bf06be4a1c7e858 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 7 Dec 2022 00:23:32 +0530 Subject: [PATCH 12/83] 06 December Distance of nearest cell having 1 06 December Distance of nearest cell having 1 --- ...ecember Distance of nearest cell having 1 | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 06 December Distance of nearest cell having 1 diff --git a/06 December Distance of nearest cell having 1 b/06 December Distance of nearest cell having 1 new file mode 100644 index 00000000..7b429ca4 --- /dev/null +++ b/06 December Distance of nearest cell having 1 @@ -0,0 +1,45 @@ +class Solution +{ + public: + + bool isCellValid(vector> &grid, int i, int j){ + if(i>=0 and i=0 and j>nearest(vector>grid) + { + queue>q; + for(int i = 0;i Date: Thu, 8 Dec 2022 23:40:59 +0530 Subject: [PATCH 13/83] Create 08 December 3 Divisors 08 December 3 Divisors --- 08 December 3 Divisors | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 08 December 3 Divisors diff --git a/08 December 3 Divisors b/08 December 3 Divisors new file mode 100644 index 00000000..a3846cb8 --- /dev/null +++ b/08 December 3 Divisors @@ -0,0 +1,41 @@ +class Solution +{ +public: + vector threeDivisors(vector query, int q) + { vector ret; + long long maxele=LONG_LONG_MIN; + + for(int j=0;jmaxele) + maxele=query[j]; + } + bool prime[(int)sqrt(maxele)+1]; + + prime[0]=0; + prime[1]=0; + for(long long i=2;i<=sqrt(maxele);i++){ + if(check_prime(i)){ + prime[i]=true; + }else{ + prime[i]=false; + } + } + for(int j=0;j Date: Fri, 9 Dec 2022 22:32:20 +0530 Subject: [PATCH 14/83] Create 09 December Black and White 09 December Black and White --- 09 December Black and White | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 09 December Black and White diff --git a/09 December Black and White b/09 December Black and White new file mode 100644 index 00000000..b52a1e2d --- /dev/null +++ b/09 December Black and White @@ -0,0 +1,51 @@ +int requiredMove(int i,int j,int n,int m) { + int ans=0; + if(i-1>0) { + if(j-2>0) { + ans++; + } + if(j+2<=m) { + ans++; + } + } + if(i-2>0) { + if(j-1>0) { + ans++; + } + if(j+1<=m) { + ans++; + } + } + if(i+1<=n) { + if(j-2>0) { + ans++; + } + if(j+2<=m) { + ans++; + } + } + if(i+2<=n) { + if(j-1>0) { + ans++; + } + if(j+1<=m) { + ans++; + } + } + return ans; +} + +long long numOfWays(int N, int M) +{ + // write code here + int mod = 1000000007; + int ans =0; + int totalPosition = N*M; + for(int i=1;i<=N;i++) { + for(int j=1;j<=M;j++) { + ans+=((totalPosition-1)-requiredMove(i,j,N,M)); + ans%=mod; + } + } + return ans; +} From d5f895c64e6b8efc9f8c89934b81447b4f44079b Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 10 Dec 2022 01:23:56 +0530 Subject: [PATCH 15/83] Create 10 December Build the smallest 10 December Build the smallest --- 10 December Build the smallest | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 10 December Build the smallest diff --git a/10 December Build the smallest b/10 December Build the smallest new file mode 100644 index 00000000..0063d2a2 --- /dev/null +++ b/10 December Build the smallest @@ -0,0 +1,57 @@ +string buildLowestNumber(string num, int k) { + + stack st; + + st.push(num[0]); + + int n = num.length(); + + for(int itr = 1; itr < n;++itr) { + + while(!st.empty() && st.top() > num[itr] && k > 0) { + + st.pop(); + + --k; + + } + + st.push(num[itr]); + + } + + while(k > 0) { + + st.pop(); + + --k; + + } + + string ans = ""; + + while(!st.empty()) { + + ans=st.top()+ans; + + st.pop(); + + } + + int leadingIndex = -1; + + for(int itr = 0; itr < ans.length(); ++itr) { + + if(ans[itr] != '0') break; + + leadingIndex = itr; + + } + + if(leadingIndex == ans.length()-1) return "0"; + + if(leadingIndex >= 0) return ans.substr(leadingIndex+1); + + return ans; + +} From b6a0b3d6e49c3cfd3da7347312a9b10c31be4662 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 11 Dec 2022 02:00:53 +0530 Subject: [PATCH 16/83] Create 11 December Array Pair Sum Divisibility Problem 11 December Array Pair Sum Divisibility Problem --- ...ember Array Pair Sum Divisibility Problem | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 11 December Array Pair Sum Divisibility Problem diff --git a/11 December Array Pair Sum Divisibility Problem b/11 December Array Pair Sum Divisibility Problem new file mode 100644 index 00000000..858fd4fc --- /dev/null +++ b/11 December Array Pair Sum Divisibility Problem @@ -0,0 +1,24 @@ +class Solution { + public: + bool canPair(vector nums, int k) { + + if(nums.size()&1) + + return false; + + unordered_map freq; + + for(auto x: nums) + + freq[x%k]++; + + for(int i=1;i Date: Mon, 12 Dec 2022 20:36:10 +0530 Subject: [PATCH 17/83] Create 12 December Articulation Point - I 12 December Articulation Point - I --- 12 December Articulation Point - I | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 12 December Articulation Point - I diff --git a/12 December Articulation Point - I b/12 December Articulation Point - I new file mode 100644 index 00000000..bf336280 --- /dev/null +++ b/12 December Articulation Point - I @@ -0,0 +1,69 @@ +class Solution { + public: + + void dfs(int node, int parent, int& timer, vector adj[], vector& vis, vector& tin, vector& low, vector& isarti){ + + + vis[node] = 1; + tin[node] = low[node] = timer++; + + int child = 0; + + for(auto &it : adj[node]){ + + if(it == parent) continue; + + if(!vis[it]){ + + dfs(it, node, timer, adj, vis, tin, low, isarti); + low[node] = min(low[node], low[it]); + + if(low[it] >= tin[node] && parent != -1){ + + isarti[node] = 1; + } + child++; + } + else{ + + low[node] = min(low[node], tin[it]); + } + } + + if(parent == -1 && child > 1) + isarti[node] = 1; + + } + + + + vector articulationPoints(int V, vectoradj[]) { + // Code here + + vector res; + vector vis(V, 0); + vector tin(V, -1); + vector low(V, -1); + vector isarti(V, 0); + + int timer = 0; + + for(int i = 0; i < V; i++){ + + if(!vis[i]){ + + dfs(i, -1, timer, adj, vis, tin, low, isarti); + } + } + + for(int i = 0; i < V; i++){ + + if(isarti[i]) res.push_back(i); + } + + if(res.size() == 0) res.push_back(-1); + + return res; + + } +}; From 6783dd7978deaa09ff6a9f914edba10a8454f551 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 13 Dec 2022 23:48:10 +0530 Subject: [PATCH 18/83] Create 13 December Split Array Largest Sum 13 December Split Array Largest Sum --- 13 December Split Array Largest Sum | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 13 December Split Array Largest Sum diff --git a/13 December Split Array Largest Sum b/13 December Split Array Largest Sum new file mode 100644 index 00000000..a1afe596 --- /dev/null +++ b/13 December Split Array Largest Sum @@ -0,0 +1,37 @@ +class Solution { + public: + bool check(int arr[],int n,int k,int t){ + int sum = 0; + for(int i = 0;it){ + sum = arr[i]; + if(arr[i]>t) return false; + k--; + } + } + if(sum!=0 and k == 0) return false; + return true; + } + int splitArray(int arr[] ,int N, int K) { + int l = 1; + int r = accumulate(arr,arr+N,0ll); + int ans; + while(l<=r){ + int mid = l+(r-l)/2; + if(check(arr,N,K,mid)){ + ans = mid; + r = mid-1; + } + else{ + l = mid+1; + } + } + return ans; + } +}; From ed84c1698b50927e47598a70d0c0e7cf805f98fe Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 14 Dec 2022 22:28:58 +0530 Subject: [PATCH 19/83] Create 14 December Complement 14 December Complement --- 14 December Complement | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 14 December Complement diff --git a/14 December Complement b/14 December Complement new file mode 100644 index 00000000..7cd3642c --- /dev/null +++ b/14 December Complement @@ -0,0 +1,27 @@ +class Solution{ +public: + vector findRange(string str, int n) { + int l = 0, local_max = 0, + global_max = 0, left = 0, right = -1; + for(int i=0; i global_max){ + global_max = local_max; + left = l; + right = i; + } + } + if(right == -1) + return {-1}; + return {left+1, right+1}; + } +}; From b6a6706b1646c46d626278e3c291439959d82d93 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 15 Dec 2022 23:48:16 +0530 Subject: [PATCH 20/83] Create 15 December Balanced string 15 December Balanced string --- 15 December Balanced string | 84 ++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 15 December Balanced string diff --git a/15 December Balanced string b/15 December Balanced string new file mode 100644 index 00000000..7deba034 --- /dev/null +++ b/15 December Balanced string @@ -0,0 +1,84 @@ +class Solution { + public: + + int digitsum(int n) + { + int totalsum = 0 ; + while(n >0 ) + { + int ld = n % 10 ; + totalsum += ld ; + n = n / 10 ; + } + return totalsum ; + } + string BalancedString(int N) { + string sol , sol1 ; + string str1 ="abcdefghijklmnopqrstuvwxyz" ; + int m = N ; + while( N > 26) { + sol1.append(str1) ; + N -= 26 ; + } + bool check = false ; + if(m % 2 == 0 ) + { + while(sol.size() != N ) + { + // add from front + if(check == false ) + { + sol.append(str1, 0, N/2 ); + check = true ; + } + else // add from the back + { + int x = 26 - (N /2 ) ; + sol.append(str1 , x, 26) ; + check = false ; + } + } + } + else + { + if( digitsum(m) %2 == 0 ) + { + while(sol.size() != N ) + { + // add from front + if(check == false ) + { + sol.append(str1, 0, (N + 1)/2 ); + check = true ; + } + else // add from the back + { + int x = 26 - (N - 1 ) /2 ; + sol.append(str1 , x, 26) ; + check = false ; + } + } + } + else + { + while(sol.size() != N ) + { + // add from front + if(check == false ) + { + sol.append(str1, 0, (N - 1)/2 ); + check = true ; + } + else // add from the backs + { + int x = 26 - (N+1 ) /2 ; + sol.append(str1 , x, 26) ; + check = false ; + } + } + } + } + sol1.append(sol) ; + return sol1 ; + } +}; From b898ba1e11775df53c4c9dafe2898305eab55a11 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 16 Dec 2022 01:24:29 +0530 Subject: [PATCH 21/83] Create 16 December 2D Hopscotch 16 December 2D Hopscotch --- 16 December 2D Hopscotch | 99 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 16 December 2D Hopscotch diff --git a/16 December 2D Hopscotch b/16 December 2D Hopscotch new file mode 100644 index 00000000..138d9e0f --- /dev/null +++ b/16 December 2D Hopscotch @@ -0,0 +1,99 @@ +class Solution{ +public: + int hopscotch(int n, int m, vector> mat, int ty, int i, int j) + { + int sum=0; + if(ty==0) + { + if(i-1>=0) + sum+=mat[i-1][j]; + if(i+1=0) + sum+=mat[i][j-1]; + if(j+1=0) + sum+=mat[i+1][j-1]; + if(j+1=0) + { + if(j-1>=0) + sum+=mat[i-1][j-1]; + if(j+1=0) + sum+=mat[i-2][j]; + if(i+2=0) + sum+=mat[i][j-2]; + if(j+2=0) + { + if(j-2>=0) + sum+=mat[i-1][j-2]; + if(j+2=0) + sum+=mat[i+1][j-2]; + if(j+2=0) + { + if(j-1>=0) + sum+=mat[i-1][j-1]; + if(j+1=0) + sum+=mat[i+2][j-1]; + if(j+1=0) + sum+=mat[i+1][j-1]; + if(j+1=0) + { + if(j-1>=0) + sum+=mat[i-2][j-1]; + if(j+1 Date: Sat, 17 Dec 2022 01:42:39 +0530 Subject: [PATCH 22/83] Create 17 December Break a number 17 December Break a number --- 17 December Break a number | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 17 December Break a number diff --git a/17 December Break a number b/17 December Break a number new file mode 100644 index 00000000..19ae00a5 --- /dev/null +++ b/17 December Break a number @@ -0,0 +1,7 @@ +class Solution{ +public: + int waysToBreakNumber(int N){ + long long mod=1e9+7,n=N; + return ((n+2)*(n+1)/2)%mod; + } +}; From 95ac68916c1f1886c8d26e18b2febf182ccb8a59 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 18 Dec 2022 22:59:55 +0530 Subject: [PATCH 23/83] Create 18 December A Game of LCM 18 December A Game of LCM --- 18 December A Game of LCM | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 18 December A Game of LCM diff --git a/18 December A Game of LCM b/18 December A Game of LCM new file mode 100644 index 00000000..c29b2efd --- /dev/null +++ b/18 December A Game of LCM @@ -0,0 +1,23 @@ +class Solution { + long maxGcd(long n) { + return Math.max(lcm(n),lcm(n-1)); + } + long lcm(long n) { + long ans = n*(n-1); + int cnt=0; + for(long i=n-2;i>=1;i--) { + long f = gcd(ans,i); + if(f==1) { + ans*=i; + cnt++; + } + if(cnt==2) return ans; + } + return ans; + } + long gcd(long a, long b) { + if (b == 0)return a; + return gcd(b, a % b); + + } +} From 7307095b17f51ba89dc3b0b2c7e3b813268cf477 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 19 Dec 2022 23:36:05 +0530 Subject: [PATCH 24/83] Create 19 December Alex Travelling 19 December Alex Travelling --- 19 December Alex Travelling | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 19 December Alex Travelling diff --git a/19 December Alex Travelling b/19 December Alex Travelling new file mode 100644 index 00000000..750a9d50 --- /dev/null +++ b/19 December Alex Travelling @@ -0,0 +1,55 @@ +class Solution { + public: + int minimumCost(vector>& flights, int n, int k) { + // code here + + //create adjacency list + vector>> graph(n+1); + + for(int i = 0 ; i < flights.size(); ++i) { + + int u = flights[i][0]; + int v = flights[i][1]; + int w = flights[i][2]; + + graph[u].push_back({v,w}); + } + + vector dist(n+1, -1); + queue> q; + dist[k] = 0; + q.push({k,dist[k]}); /* Node | cost */ + int ans = INT_MIN; + + while(!q.empty()) { + vector frontNode = q.front(); + q.pop(); + + int node = frontNode[0]; + int cost = frontNode[1]; + + for(auto x: graph[node]) { + int node_x = x[0]; + int cost_x = x[1]; + + if(dist[node_x] == -1) { + dist[node_x] = cost + cost_x; + q.push({node_x, dist[node_x]}); + } + + else if(dist[node_x] > cost + cost_x) { + dist[node_x] = cost + cost_x; + q.push({node_x, dist[node_x]}); + } + } + } + + for(int i = 1 ; i < dist.size(); ++i) { + if(dist[i] != -1) { + ans = max(ans, dist[i]); + } + else return -1; + } + return ans; + } +}; From 0fcfb850d615795edf0e7cadc17c92c49ce770fc Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 20 Dec 2022 23:52:09 +0530 Subject: [PATCH 25/83] Create 20 December A difference of values and indexes 20 December A difference of values and indexes --- 20 December A difference of values and indexes | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 20 December A difference of values and indexes diff --git a/20 December A difference of values and indexes b/20 December A difference of values and indexes new file mode 100644 index 00000000..da7c16c0 --- /dev/null +++ b/20 December A difference of values and indexes @@ -0,0 +1,16 @@ +class Solution{ + public: + int maxDistance(int arr[], int n) + { + int mx1 = INT_MIN, mn1 = INT_MAX, mx2 = INT_MIN, mn2 = INT_MAX; + + for(int i = 0; i < n; i++) + { + mx1 = max(mx1, arr[i] + i); + mn1 = min(mn1, arr[i] + i); + mx2 = max(mx2, -arr[i] + i); + mn2 = min(mn2, -arr[i] + i); + } + return max((mx1 - mn1), (mx2 - mn2)); + } +}; From 35db67311c9b35d220fbf7c08a36b50a258d20b3 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 21 Dec 2022 23:46:47 +0530 Subject: [PATCH 26/83] Create 21 December Absolute List Sorting 21 December Absolute List Sorting --- 21 December Absolute List Sorting | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 21 December Absolute List Sorting diff --git a/21 December Absolute List Sorting b/21 December Absolute List Sorting new file mode 100644 index 00000000..cfb4a9d9 --- /dev/null +++ b/21 December Absolute List Sorting @@ -0,0 +1,20 @@ +class Solution{ + +public: + Node* sortList(Node* head) + { + // Your Code Here + Node *curr = head; + Node *temp; + while(curr->next){ + if(curr->next->data<0){ + temp=curr->next; + curr->next=curr->next->next; + temp->next=head; + head=temp; + } + else curr=curr->next; + } + return head; + } +}; From 2e1b215b6dc1f56dcf034d34161e2750bc6a0f4b Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 22 Dec 2022 23:19:41 +0530 Subject: [PATCH 27/83] Create 22 December Zero Sum Subarrays 22 December Zero Sum Subarrays --- 22 December Zero Sum Subarrays | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 22 December Zero Sum Subarrays diff --git a/22 December Zero Sum Subarrays b/22 December Zero Sum Subarrays new file mode 100644 index 00000000..899aab82 --- /dev/null +++ b/22 December Zero Sum Subarrays @@ -0,0 +1,15 @@ +class Solution{ +public: + //Function to count subarrays with sum equal to 0. + long long int findSubarray(vector &arr, int n ) { + unordered_mapm; + long long count=0,sum=0; + for(int i=0;i Date: Fri, 23 Dec 2022 23:49:11 +0530 Subject: [PATCH 28/83] Create 23 December Burst Balloons 23 December Burst Balloons --- 23 December Burst Balloons | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 23 December Burst Balloons diff --git a/23 December Burst Balloons b/23 December Burst Balloons new file mode 100644 index 00000000..cbd16ae0 --- /dev/null +++ b/23 December Burst Balloons @@ -0,0 +1,23 @@ +class Solution { +public: + int maxCoins(int N, vector &arr) { + // code here + int n = arr.size(); + vector>dp(n+2, vector(n+1, 0)); + arr.push_back(1); + arr.insert(arr.begin(), 1); + + for(int i=n; i>= 1; i--){ + for(int j=1; j<=n; j++){ + if(i > j) continue; + int maxi = INT_MIN; + for(int k=i;k<=j;k++){ + int ans = arr[i-1]*arr[k]*arr[j+1] + dp[i][k-1] + dp[k+1][j]; + if(maxi < ans) maxi = ans; + } + dp[i][j] = maxi; + } + } + return dp[1][n]; + } +}; From 66ac19299b8ae1f554c316d3d772a9bf14af1117 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 24 Dec 2022 22:52:33 +0530 Subject: [PATCH 29/83] Create 24 December Wine Buying and Selling 24 December Wine Buying and Selling --- 24 December Wine Buying and Selling | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 24 December Wine Buying and Selling diff --git a/24 December Wine Buying and Selling b/24 December Wine Buying and Selling new file mode 100644 index 00000000..febda6e2 --- /dev/null +++ b/24 December Wine Buying and Selling @@ -0,0 +1,24 @@ +class Solution{ + public: + long long int wineSelling(vector& Arr, int N){ + + vector > buy; + vector > sell; + for(int i=0;i0) buy.push_back({Arr[i],i}); + else sell.push_back({Arr[i],i}); + } + long long int ans = 0; + int i=0,j=0; + while(i Date: Sun, 25 Dec 2022 02:14:30 +0530 Subject: [PATCH 30/83] Create 25 December Missing number in matrix 25 December Missing number in matrix --- 25 December Missing number in matrix | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 25 December Missing number in matrix diff --git a/25 December Missing number in matrix b/25 December Missing number in matrix new file mode 100644 index 00000000..944f1f15 --- /dev/null +++ b/25 December Missing number in matrix @@ -0,0 +1,59 @@ +class Solution { +public: + long long int MissingNo(vector >& matrix) { + setnonzero, zero; + + // Interating through rows + for(int i = 0; i < matrix.size(); i++){ + bool f = true; + long long int sum = 0; + for(int j = 0; j < matrix[0].size(); j++){ + sum += matrix[i][j]; + if(!matrix[i][j]) + f = false; + } + if(f)nonzero.insert(sum); + if(!f)zero.insert(sum); + } + // Interating through coloumns + for(int j = 0; j < matrix[0].size(); j++){ + bool f = true; + long long int sum = 0; + for(int i = 0; i < matrix.size(); i++){ + sum += matrix[i][j]; + if(!matrix[i][j]) + f = false; + } + if(f)nonzero.insert(sum); + if(!f)zero.insert(sum); + } + // Interating through diagonals + bool f = true; + long long int sum = 0; + for(int i = 0; i < matrix.size(); i++){ + sum += matrix[i][i]; + if(!matrix[i][i]) + f = false; + } + if(f)nonzero.insert(sum); + if(!f)zero.insert(sum); + // Interating through diagonals + f = true; + sum = 0; + for(int i = 0, j = matrix.size() - 1; j >= 0; i++,j--){ + sum += matrix[i][j]; + if(!matrix[i][j]) + f = false; + } + if(f)nonzero.insert(sum); + if(!f)zero.insert(sum); + if(nonzero.size() == 1 and zero.size() == 1){ + long long int x = *nonzero.begin(); + long long int y = *zero.begin(); + if(x <= y) + return -1; + return x - y; + } + return -1; + } +}; From 8db7447069d03f03ecd6a0644726df40f4fddc30 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 27 Dec 2022 03:01:56 +0530 Subject: [PATCH 31/83] Create 27 December Container With Most Water 27 December Container With Most Water --- 27 December Container With Most Water | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 27 December Container With Most Water diff --git a/27 December Container With Most Water b/27 December Container With Most Water new file mode 100644 index 00000000..79108a16 --- /dev/null +++ b/27 December Container With Most Water @@ -0,0 +1,13 @@ +long long maxArea(long long A[], int len) +{ + int l = 0, r=len-1, h =0; + long long ans = 0; + while(l Date: Wed, 28 Dec 2022 23:55:58 +0530 Subject: [PATCH 32/83] Create 28 December Largest subtree sum in a tree 28 December Largest subtree sum in a tree --- ...28 December Largest subtree sum in a tree | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 class Solution { public:/28 December Largest subtree sum in a tree diff --git a/class Solution { public:/28 December Largest subtree sum in a tree b/class Solution { public:/28 December Largest subtree sum in a tree new file mode 100644 index 00000000..71d32003 --- /dev/null +++ b/class Solution { public:/28 December Largest subtree sum in a tree @@ -0,0 +1,22 @@ +class Solution { + public: + // Function to find largest subtree sum. + void sum(Node* root,unordered_map&helper){ + if(root==NULL)return; + sum(root->left,helper); + sum(root->right,helper); + if(root->left!=NULL && root->right!=NULL)helper[root]=root->data+helper[root->left]+helper[root->right]; + else if(root->left==NULL && root->right!=NULL)helper[root]=root->data+helper[root->right]; + else if(root->left!=NULL && root->right==NULL)helper[root]=root->data+helper[root->left]; + else helper[root]=root->data; + } + int findLargestSubtreeSum(Node* root) + { + if(root==NULL)return 0; + unordered_map helper; + sum(root,helper); + int maxi=INT_MIN; + for(auto it:helper)maxi=max(maxi,it.second); + return maxi; + } +}; From cb735b2a27f7fbb7744c567c5b0536cfe97097be Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 30 Dec 2022 23:46:22 +0530 Subject: [PATCH 33/83] Create 2nd 30 December Single valued subtree 30 December Single valued subtree --- 2nd 30 December Single valued subtree | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2nd 30 December Single valued subtree diff --git a/2nd 30 December Single valued subtree b/2nd 30 December Single valued subtree new file mode 100644 index 00000000..b75f8c83 --- /dev/null +++ b/2nd 30 December Single valued subtree @@ -0,0 +1,38 @@ +class Solution +{ + public: + public: + int ans=0; + + bool func(Node *root){ + if(root->left==NULL && root->right==NULL){ + ans++; + return true; + } + + // Now we have to see the values + int ans1=1,ans2=1; + if(root->left!=NULL){ + ans1&=func(root->left); + if(root->left->data!=root->data)ans2=0; + } + if(root->right!=NULL){ + ans1&=func(root->right); + if(root->right->data!=root->data)ans2=0; + } + + if(ans1 && ans2)ans++; + return ans1 && ans2; + } + + int singlevalued(Node *root) + { + //code here + //Go in two directions and find the best possible + //first check if two sizesa are true or not and then check the value + if(root==NULL)return 0; + func(root); + return ans; + } + +}; From e6223dd4a55686e5ccc66370b5650935d53756be Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 30 Dec 2022 23:47:30 +0530 Subject: [PATCH 34/83] Create 1st 30 December Single valued subtree 1st 30 December Single valued subtree --- 1st 30 December Single valued subtree | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1st 30 December Single valued subtree diff --git a/1st 30 December Single valued subtree b/1st 30 December Single valued subtree new file mode 100644 index 00000000..0bb1ac29 --- /dev/null +++ b/1st 30 December Single valued subtree @@ -0,0 +1,32 @@ +class Solution +{ + + public: + int c=0; + int p=0; + int fun1(Node *r,int val){ + if(r==NULL) + return 0; + + if(r->data==val) + p++; + + return 1+fun1(r->left,val)+fun1(r->right,val); + } + void fun(Node *r){ + if(r==NULL) + return ; + p=0; + if(fun1(r,r->data)==p) + c++; + + fun(r->left); + fun(r->right); + } + int singlevalued(Node *root) + { + fun(root); + return c; + } + +}; From c222e638b42f069551ce07ed9c91c193a211b725 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 31 Dec 2022 04:00:50 +0530 Subject: [PATCH 35/83] Create 31 December Find minimum number of Laptops required 31 December Find minimum number of Laptops required --- ...r Find minimum number of Laptops required | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 31 December Find minimum number of Laptops required diff --git a/31 December Find minimum number of Laptops required b/31 December Find minimum number of Laptops required new file mode 100644 index 00000000..2c819a6a --- /dev/null +++ b/31 December Find minimum number of Laptops required @@ -0,0 +1,22 @@ +class Solution { + public: + int minLaptops(int N, int start[], int end[]) { + // Code here + vector>v; + for(int i=0;i Date: Sun, 1 Jan 2023 23:36:55 +0530 Subject: [PATCH 36/83] Create 01 January Count even length(T) 01 January Count even length --- 01 January Count even length(T) | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 01 January Count even length(T) diff --git a/01 January Count even length(T) b/01 January Count even length(T) new file mode 100644 index 00000000..f329a667 --- /dev/null +++ b/01 January Count even length(T) @@ -0,0 +1,43 @@ +class Solution{ + +public: + +int inv(int n, int m, int mod=1000000007){ + +if(m==0) return 1; + +if(m&1) return (n*1ll*inv(n, m-1))%mod; + +int tmp=inv(n, m/2); + +return (tmp*1ll*tmp)%mod; + +} + +int compute_value(int n) + +{ + +// Code here + +int m = 1000000007; + +long long fact2N=1, factN; + +for(int i=1; i<=2*n; i++){ + +fact2N *= i; + +fact2N %= m; + +if(i==n)factN=fact2N; + +} + +factN = (factN*factN)%m; + +return (fact2N * 1ll * inv(factN, m-2))%m; + +} + +}; From ebcfca2b9272a7c7ea39630b2eb8eb4b1769031f Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 2 Jan 2023 22:09:21 +0530 Subject: [PATCH 37/83] Create 02 January Maximum Value 02 January Maximum Value --- 02 January Maximum Value | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 02 January Maximum Value diff --git a/02 January Maximum Value b/02 January Maximum Value new file mode 100644 index 00000000..4451e7cc --- /dev/null +++ b/02 January Maximum Value @@ -0,0 +1,43 @@ +class Solution { + public: + void traverse(Node* node, vector& ans, int level ){ + if(node == NULL ) + return; + if(ans.size()>=level){ + ans[level-1] = max(node->data, ans[level-1]); + } + else{ + ans.push_back(node->data); + } + traverse(node->left, ans, level+1); + traverse(node->right, ans, level+1); + } + // Takes O(n) Time complexity and O(H) space complexity + void dfs(Node* node, vector&ans){ + if (node==NULL) + return; + stack> s; + s.push({node, 1}); + while(!s.empty()){ + auto it = s.top(); + s.pop(); + if(ans.size()>=it.second){ + ans[it.second-1] = max(it.first->data, ans[it.second-1]); + } + else{ + ans.push_back(it.first->data); + } + if(it.first->left != NULL ) + s.push({it.first->left, it.second+1}); + if(it.first->right != NULL ) + s.push({it.first->right, it.second+1}); + } + } + vector maximumValue(Node* node) { + vector ans; + // int level = 1; + // traverse(node, ans, level); + dfs(node, ans); + return ans; + } +}; From caadaca4d923157db879b70ec1796061bbd28706 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 3 Jan 2023 07:45:11 +0530 Subject: [PATCH 38/83] Create 03 January Minimize number of Students to be removed 03 January Minimize number of Students to be removed --- ... Minimize number of Students to be removed | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 03 January Minimize number of Students to be removed diff --git a/03 January Minimize number of Students to be removed b/03 January Minimize number of Students to be removed new file mode 100644 index 00000000..135c4e02 --- /dev/null +++ b/03 January Minimize number of Students to be removed @@ -0,0 +1,41 @@ +class Solution { + public: + // Helper function to find LIS + int LongestIncreasingSubsequenceLength(vector& v) + { + if (v.size() == 0) + return 0; + + vector tail(v.size(), 0); + int length = 1; // always points empty slot in tail + + tail[0] = v[0]; + + for (int i = 1; i < v.size(); i++) { + + // Do binary search for the element in + // the range from begin to begin + length + auto b = tail.begin(), e = tail.begin() + length; + auto it = lower_bound(b, e, v[i]); + + // If not present change the tail element to v[i] + if (it == tail.begin() + length) + tail[length++] = v[i]; + else + *it = v[i]; + } + + return length; + } + + int removeStudents(int H[], int N) { + + vector arr; + for(int i=0; i Date: Wed, 4 Jan 2023 04:24:48 +0530 Subject: [PATCH 39/83] Create 04 January Maximum Profit By Choosing A Subset Of Intervals 04 January Maximum Profit By Choosing A Subset Of Intervals --- ...m Profit By Choosing A Subset Of Intervals | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 04 January Maximum Profit By Choosing A Subset Of Intervals diff --git a/04 January Maximum Profit By Choosing A Subset Of Intervals b/04 January Maximum Profit By Choosing A Subset Of Intervals new file mode 100644 index 00000000..b9d97f96 --- /dev/null +++ b/04 January Maximum Profit By Choosing A Subset Of Intervals @@ -0,0 +1,30 @@ +class Solution { + public: + vector>job; + int p=0; + int solve(int i,vector&dp,vector&startTime){ + if(i>=job.size())return 0; + if(dp[i]!=-1)return dp[i]; + int next=lower_bound(startTime.begin(),startTime.end(),job[i][1])-startTime.begin(); + return dp[i]=max(solve(i+1,dp,startTime),job[i][2]+solve(next,dp,startTime)); + } + int jobSched(vector& startTime, vector& endTime, vector& profit) { + for(int i=0;idp(job.size(),-1); + return solve(0,dp,startTime); + } + int maximum_profit(int n, vector> &in) { + // Write your code here. + vectors,e,p; + for(int i=0;i Date: Thu, 5 Jan 2023 21:45:33 +0530 Subject: [PATCH 40/83] Create 05 January Find the longest string 05 January Find the longest string --- 05 January Find the longest string | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 05 January Find the longest string diff --git a/05 January Find the longest string b/05 January Find the longest string new file mode 100644 index 00000000..1a7a5b8e --- /dev/null +++ b/05 January Find the longest string @@ -0,0 +1,39 @@ +class Solution +{ +public: + string longestString(vector &words) + { + sort(words.begin(),words.end(),[&](string &a,string &b){ + return a.size()s(words.begin(),words.end()); + for(int i=0;ia) + ans=a; + } + } + } + return ans; + } +}; From 359eea2ca68df6e6d717f4fdcd9d73972443579f Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 7 Jan 2023 16:36:24 +0530 Subject: [PATCH 41/83] Create 07 January Flattening a Linked List 07 January Flattening a Linked List --- 07 January Flattening a Linked List | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 07 January Flattening a Linked List diff --git a/07 January Flattening a Linked List b/07 January Flattening a Linked List new file mode 100644 index 00000000..ed09b133 --- /dev/null +++ b/07 January Flattening a Linked List @@ -0,0 +1,34 @@ +class Solution { + Node* merge( Node* a, Node* b ) + { + + if (a == NULL) + return b; + + + if (b == NULL) + return a; + + Node* result; + if (a->data <= b->data) + { + result = a; + result->bottom = merge( a->bottom, b ); + } + else + { + result = b; + result->bottom = merge( a, b->bottom ); + } + + return result; + } +public: + Node* flatten (Node* root) + { + // Base cases + if (root == NULL || root->next == NULL) + return root; + return merge( root, flatten(root->next) ); + } +}; From dfca6ad33c4039b9395592f5e488449788402e89 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 8 Jan 2023 06:05:48 +0530 Subject: [PATCH 42/83] Create 08 January Absolute difference divisible by K 08 January Absolute difference divisible by K --- ...anuary Absolute difference divisible by K | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 08 January Absolute difference divisible by K diff --git a/08 January Absolute difference divisible by K b/08 January Absolute difference divisible by K new file mode 100644 index 00000000..d9779aa0 --- /dev/null +++ b/08 January Absolute difference divisible by K @@ -0,0 +1,28 @@ +class Solution { + public: + long long countPairs(int n, int arr[], int k) { + + map mp; + + for(int i=0;i Date: Tue, 10 Jan 2023 04:03:25 +0530 Subject: [PATCH 43/83] Create 10 January Transform to Sum Tree 10 January Transform to Sum Tree --- 10 January Transform to Sum Tree | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 10 January Transform to Sum Tree diff --git a/10 January Transform to Sum Tree b/10 January Transform to Sum Tree new file mode 100644 index 00000000..baa4dfe8 --- /dev/null +++ b/10 January Transform to Sum Tree @@ -0,0 +1,22 @@ +class Solution { + public: + + // Convert a given tree to a tree where every node contains sum of values of + // nodes in left and right subtrees in the original tree + int check(Node* root){ + if(root==NULL){ + return 0; + } + int left=check(root->left); + int right=check(root->right); + int x= root->data; + root->data=left+right; + return root->data+x; + + + } + void toSumTree(Node *node) + { + check(node); // Your code here + } +}; From b09b759768e3f57ea01d2a6c53d768a4f1372c3c Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 11 Jan 2023 04:06:43 +0530 Subject: [PATCH 44/83] Create 11 January Make array elements unique 11 January Make array elements unique --- 11 January Make array elements unique | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 11 January Make array elements unique diff --git a/11 January Make array elements unique b/11 January Make array elements unique new file mode 100644 index 00000000..58703a9e --- /dev/null +++ b/11 January Make array elements unique @@ -0,0 +1,20 @@ +class Solution { + public: + long long int minIncrements(vector arr, int N) { + sort(arr.begin(),arr.end()); + int pre = arr[0]; + long long int minIncrease=0; + + for(int i=1;i Date: Fri, 13 Jan 2023 15:42:21 +0530 Subject: [PATCH 45/83] Create 13 January Count Lucky Permutations 13 January Count Lucky Permutations --- 13 January Count Lucky Permutations | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 13 January Count Lucky Permutations diff --git a/13 January Count Lucky Permutations b/13 January Count Lucky Permutations new file mode 100644 index 00000000..f37dba5c --- /dev/null +++ b/13 January Count Lucky Permutations @@ -0,0 +1,35 @@ +class Solution { + public: + long long int luckyPermutations(int N, int M, vector arr, + vector> graph) { + + vector> dp(N, vector(1 << N, 0)); + + for (int i = 0; i < N; i++) { + dp[i][1 << i] = 1; + } + vector> adj(N, vector(N, 0)); + for (vector edge : graph) { + adj[edge[0] - 1][edge[1] - 1] = 1; + adj[edge[1] - 1][edge[0] - 1] = 1; + } + for (int bitmask = 1; bitmask < (1 << N); bitmask++) { + for (int i = 0; i < N; i++) { + if ((1 & (bitmask >> i))) { + for (int j = 0; j < N; j++) { + if (j != i && arr[j] != arr[i] && + (1 & (bitmask >> j)) && adj[arr[i]-1][arr[j] - 1]) { + dp[i][bitmask] += dp[j][bitmask ^ (1 << i)]; + } + } + } + } + } + long long int ans = 0; + + for (int i = 0; i < N; i++) { + ans += dp[i][(1 << N) - 1]; + } + return ans; + } +}; From 300965590dcec00ee5bef764c7ee1bd1f41ad3a6 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 14 Jan 2023 19:31:48 +0530 Subject: [PATCH 46/83] Create 14 January Maximum Number of Toys 14 January Maximum Number of Toys --- 14 January Maximum Number of Toys | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 14 January Maximum Number of Toys diff --git a/14 January Maximum Number of Toys b/14 January Maximum Number of Toys new file mode 100644 index 00000000..8107efa0 --- /dev/null +++ b/14 January Maximum Number of Toys @@ -0,0 +1,51 @@ +class Solution{ +public: + vector maximumToys(int N,vector A,int Q,vector> Queries){ + // code here + vector ans(Q); + vector prefix(N); + vector> arr(N); + for (int i = 0; i < N; i++) + { + arr[i] = {A[i], i}; + } + sort(arr.begin(), arr.end()); + unordered_map mp; + for (int i = 0; i < N; i++) + { + mp[arr[i].second] = i; + } + long long sum = 0; + for (int i = 0; i < N; i++) + { + sum += arr[i].first; + prefix[i] = sum; + } + for (int m = 0; m < Q; m++) + { + int money = Queries[m][0]; + int k = Queries[m][1]; + int idx = upper_bound(prefix.begin(), prefix.end(), money) - prefix.begin(); + vector v(k); + for (int i = 0; i < k; i++) + { + v[i] = mp[Queries[m][i + 2] - 1]; + } + sort(v.begin(), v.end()); + int count = 0, j; + for (int i = 0; i < k; i++) + { + j = v[i]; + if (j < idx) + { + count++; + money += arr[j].first; + idx = upper_bound(prefix.begin(), prefix.end(), money) - prefix.begin(); + } + } + ans[m] = idx - count; + } + return ans; + } + +}; From a431f145e130b394e6aa15de79f88624cee0c5e9 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 15 Jan 2023 18:04:27 +0530 Subject: [PATCH 47/83] Create 15 January Count the Substring 15 January Count the Substring --- 15 January Count the Substring | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 15 January Count the Substring diff --git a/15 January Count the Substring b/15 January Count the Substring new file mode 100644 index 00000000..8900a242 --- /dev/null +++ b/15 January Count the Substring @@ -0,0 +1,69 @@ +class Solution{ +public: + void merging(vector &pre, int l, int m, int h, long long &inv) { + int i = l, j = m + 1, k = 0; + vector temp(h - l + 1); + + while(i <= m and j <= h) { + if(pre[i] <= pre[j]) + temp[k++] = pre[i++]; + else { + inv += (m - i + 1); + temp[k++] = pre[j++]; + } + } + + while(i <= m) temp[k++] = pre[i++]; + + while(j <= h) temp[k++] = pre[j++]; + + k = 0; + for(int c = l; c <= h; c++) + pre[c] = temp[k++]; + } + + void mergeSort(vector &pre, int l, int h, long long &inv) { + if(l < h) { + int mid = (l + h) >> 1; + mergeSort(pre, l, mid, inv); + mergeSort(pre, mid + 1, h, inv); + merging(pre, l, mid, h, inv); + } + } + + long long inversions(vector &pre) { + int n = pre.size(); + long long inv = 0; + + mergeSort(pre, 0, n - 1, inv); + + return inv; + } + + long long countSubstring(string s){ + int n = s.size(); + + vector nums(n); + for(int i = 0; i < n; i++) { + nums[i] = s[i] - '0'; + if(nums[i] == 0) + nums[i] = -1; + } + + vector pre_sum(n); + long long sum = 0; + for(int i = 0; i < n; i++) { + sum += nums[i]; + pre_sum[i] = sum; + } + + long long count = 0; // to store valid substrings; + for(int i = 0; i < n; i++) { + if(pre_sum[i] > 0) count++; + } + + reverse(pre_sum.begin(), pre_sum.end()); + + return count + inversions(pre_sum); + } +}; From 661563d0a0b3cb0e984f37c4b88d01e6dd533966 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 16 Jan 2023 21:01:11 +0530 Subject: [PATCH 48/83] Create 16 January Next Greater Element 16 January Next Greater Element --- 16 January Next Greater Element | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 16 January Next Greater Element diff --git a/16 January Next Greater Element b/16 January Next Greater Element new file mode 100644 index 00000000..3413d2f8 --- /dev/null +++ b/16 January Next Greater Element @@ -0,0 +1,27 @@ +class Solution { + public: + vector nextLargerElement(vector &arr, int n){ + // Your code here + vector ans(n); + stack s; + ans[n-1] = -1; + s.push(arr[n-1]); + for(int i=n-2;i>=0;i--){ + if(!s.empty()){ + if(s.top() <= arr[i]){ + s.pop(); + i++; + } + else{ + ans[i] = s.top(); + s.push(arr[i]); + } + } + else{ + ans[i] = -1; + s.push(arr[i]); + } + } + return ans; + } +}; From 8d8d0ad15b089875a3750a4d584362b0304c795c Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 17 Jan 2023 19:12:47 +0530 Subject: [PATCH 49/83] Create 17 jan OO 926. Flip String to Monotone Increasing 17 jan OO 926. Flip String to Monotone Increasing --- ...an OO 926. Flip String to Monotone Increasing | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 17 jan OO 926. Flip String to Monotone Increasing diff --git a/17 jan OO 926. Flip String to Monotone Increasing b/17 jan OO 926. Flip String to Monotone Increasing new file mode 100644 index 00000000..20747b41 --- /dev/null +++ b/17 jan OO 926. Flip String to Monotone Increasing @@ -0,0 +1,16 @@ +class Solution { +public: + int minFlipsMonoIncr(string s) { + int flips=0,count=0; + for (auto c:s) + { + if(c == '1') + count++; + else + flips++; + flips = min(flips,count); + } + return flips; + + } +}; From cadf39903e06d8acf5520acaf468df5e1e92ab1c Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 17 Jan 2023 19:29:57 +0530 Subject: [PATCH 50/83] Create 17 January Find the maximum GCD of the siblings of a Binary Tree 17 January Find the maximum GCD of the siblings of a Binary Tree --- ...ximum GCD of the siblings of a Binary Tree | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 17 January Find the maximum GCD of the siblings of a Binary Tree diff --git a/17 January Find the maximum GCD of the siblings of a Binary Tree b/17 January Find the maximum GCD of the siblings of a Binary Tree new file mode 100644 index 00000000..71baefc9 --- /dev/null +++ b/17 January Find the maximum GCD of the siblings of a Binary Tree @@ -0,0 +1,77 @@ +class Solution +{ +public: + int maxNode=0, maxGCD1=0; + + int GCD (int a, int b) + + { + + if(a==0) return b; + + else + + return GCD(b%a, a); + + } + + + + void Solve(Node* root) + + { + + if(root) + + { + + if(root->left && root->right) + + { + + int tempGCD=GCD(root->left->data, root->right->data ); + + if(tempGCD == maxGCD1 ) + + { + + maxNode=max(maxNode, root->data); + + } + + else if(tempGCD > maxGCD1) + + { + + maxGCD1=tempGCD; + + maxNode=root->data; + + } + + } + + + + if(root->left) Solve(root->left) ; + + if(root->right) Solve(root->right); + + } + + } + + + + int maxGCD( Node* root) + + { + + //may2021 + + Solve(root); + + return maxNode; + + } +}; From 562eb69748fa8bde2ff11d91dcbc004c3de46ffc Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 18 Jan 2023 21:27:24 +0530 Subject: [PATCH 51/83] Create 18 January Find the first node of loop in linked list 18 January Find the first node of loop in linked list --- ...Find the first node of loop in linked list | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 18 January Find the first node of loop in linked list diff --git a/18 January Find the first node of loop in linked list b/18 January Find the first node of loop in linked list new file mode 100644 index 00000000..4e7a2fe3 --- /dev/null +++ b/18 January Find the first node of loop in linked list @@ -0,0 +1,38 @@ +class Solution +{ + public: + //Function to find first node if the linked list has a loop. + int findFirstNode(Node* head) + { + if (!head) + { + return (-1); + } + Node *slow = head; + Node *fast = head; + + //floyd's algorithm + while(slow && fast && fast -> next) + { + slow = slow -> next; + fast = fast -> next -> next; + + if (fast == slow) + { + //loop detected + slow = head; + + //move both pointers at same pace + while(slow != fast) + { + slow = slow -> next; + fast = fast -> next; + } + return (slow -> data); + + } + } + // no loop + return (-1); + } +}; From b969de57e2c55d82e97d4ee0e1be1520bb34365a Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 19 Jan 2023 19:58:34 +0530 Subject: [PATCH 52/83] Create 19 January Carpet into Box 19 January Carpet into Box --- 19 January Carpet into Box | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 19 January Carpet into Box diff --git a/19 January Carpet into Box b/19 January Carpet into Box new file mode 100644 index 00000000..e9b14abb --- /dev/null +++ b/19 January Carpet into Box @@ -0,0 +1,45 @@ +class Solution{ + public: + int carpetBox(int A, int B, int C, int D){ + int a=min(A,B); + int b=max(A,B); + int c=min(C,D); + int d=max(C,D); + int ans=0; + + while(b>d){ + + b=b/2; + if(bc){ + + a=a/2; + + + + if(b Date: Fri, 20 Jan 2023 07:10:12 +0530 Subject: [PATCH 53/83] Create 20 January Maximum Weight Node 20 January Maximum Weight Node --- 20 January Maximum Weight Node | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 20 January Maximum Weight Node diff --git a/20 January Maximum Weight Node b/20 January Maximum Weight Node new file mode 100644 index 00000000..a00c1453 --- /dev/null +++ b/20 January Maximum Weight Node @@ -0,0 +1,57 @@ +class Solution +{ + public: + int maxWeightCell(int N, vector Edge) + { + vector adj[N]; + + //creating a graph + + for(int i = 0 ;i < N ; i++) + + { + + if(Edge[i] != -1) + + adj[Edge[i]].push_back(i); + + } + + int val = 0; + + int ans = 0; + + // checking sum of incoming all nodes on a given node + + for(int i = 0 ; i < N ; i++) + + { + + int sm =0; + + for(auto x : adj[i]) + + { + + sm = sm+x; + + } + + if(sm >= val) + + { + + val = sm; + + ans = i; + + } + + } + + return ans; + + + + } +}; From 6a41763625a2cebd546b0080a5640c0912654ec7 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 21 Jan 2023 16:47:18 +0530 Subject: [PATCH 54/83] Create 21 January Minimum X (xor) A 21 January Minimum X (xor) A --- 21 January Minimum X (xor) A | 72 +++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 21 January Minimum X (xor) A diff --git a/21 January Minimum X (xor) A b/21 January Minimum X (xor) A new file mode 100644 index 00000000..2fd50fe8 --- /dev/null +++ b/21 January Minimum X (xor) A @@ -0,0 +1,72 @@ +class Solution { + public: + int countset(int n) + + { + int c=0; + while(n>0) + + { + + if(n%2==1) + + { + + c++; + + } + + n=n/2; + + } + + return c; + + } + + + + int minVal(int a, int b) { + + int c1=countset(a); + int c2=countset(b); + if(c1==c2) + return a; + + else if(c1>c2) + + { + + int diff=c1-c2; + + while(diff) + + { + + a=a&(a-1); + + diff--; + + } + + return a; + + } + else if(c2>c1) + + { + + int diff=c2-c1; + while(diff) + + { + + a=a|(a+1); + diff--; + + } + return a; + + } + } +}; From 270bfbcc1a6a8961058303018f136f4dde9a211a Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 22 Jan 2023 16:43:00 +0530 Subject: [PATCH 55/83] Create 22 January GCD Array 22 January GCD Array --- 22 January GCD Array | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 22 January GCD Array diff --git a/22 January GCD Array b/22 January GCD Array new file mode 100644 index 00000000..9e6e18d0 --- /dev/null +++ b/22 January GCD Array @@ -0,0 +1,37 @@ +class Solution { + public: + void factor(vector& arr, int n) + { + for(int i = 1;i*i<=n;i++) + { + if(n%i == 0) + { + arr.push_back(i); + if((n/i) != i) + arr.push_back(n/i); + } + } + return; + } + + int solve(int n, int k, vector &arr) { + int tot = 0; + for(int i = 0;i nums; + factor(nums, tot); + int ans = 1; + for(int i = 0;i= k) ans = max(ans, temp); + } + return ans; + } +}; From 4615b78559fbaa794cdd2c954eb8824a261b3c64 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 23 Jan 2023 15:46:33 +0530 Subject: [PATCH 56/83] Create 23 January Geeks And The String 23 January Geeks And The String --- 23 January Geeks And The String | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 23 January Geeks And The String diff --git a/23 January Geeks And The String b/23 January Geeks And The String new file mode 100644 index 00000000..7c55c99f --- /dev/null +++ b/23 January Geeks And The String @@ -0,0 +1,25 @@ +class Solution { + + public: + + string removePair(string s) { + + string ans; + + for(int i=0;i Date: Tue, 24 Jan 2023 14:25:35 +0530 Subject: [PATCH 57/83] Create 24 January Convert an array to reduced form 24 January Convert an array to reduced form --- 24 January Convert an array to reduced form | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 24 January Convert an array to reduced form diff --git a/24 January Convert an array to reduced form b/24 January Convert an array to reduced form new file mode 100644 index 00000000..ffa87d5d --- /dev/null +++ b/24 January Convert an array to reduced form @@ -0,0 +1,21 @@ +class Solution{ +public: + // Converts arr[0..n-1] to reduced form. + void convert(int arr[], int n) { + //push the value and its index as pair into a vector + vector> v; + for(int i=0; i Date: Wed, 25 Jan 2023 14:11:01 +0530 Subject: [PATCH 58/83] Create 25 January Type it! 25 January Type it! --- 25 January Type it! | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 25 January Type it! diff --git a/25 January Type it! b/25 January Type it! new file mode 100644 index 00000000..8f90405d --- /dev/null +++ b/25 January Type it! @@ -0,0 +1,28 @@ +#include +using namespace std; + +// } Driver Code Ends +// User function template for C++ + +class Solution { + public: + int minOperation(string s) { + int n = s.size(); + for (int i = n / 2 - 1; i >= 0; --i) + if (s.substr(0, i + 1) == s.substr(i + 1, i + 1)) + return (i + 1) + 1 + (n - 2 * (i + 1)); + + return n; + } +}; + +//{ Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + string s; + cin >> s; + Solution ob; + cout << ob.minOperation(s) << "\n"; + } From ee77d8c2602e965c11495b7151a5dd70df9a58e1 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 26 Jan 2023 10:31:28 +0530 Subject: [PATCH 59/83] Create 26 January Case-specific Sorting of Strings 26 January Case-specific Sorting of Strings --- 26 January Case-specific Sorting of Strings | 65 ++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 26 January Case-specific Sorting of Strings diff --git a/26 January Case-specific Sorting of Strings b/26 January Case-specific Sorting of Strings new file mode 100644 index 00000000..51ff42bd --- /dev/null +++ b/26 January Case-specific Sorting of Strings @@ -0,0 +1,65 @@ +class Solution +{ + public: + //Function to perform case-specific sorting of strings. + string caseSort(string str, int n) + + { + + vector m1; + vector m2; + string res=""; + for(int i=0; i='a' && str[i]<='z') + + { + + m1.push_back(str[i]); + + } + + else + + { + + m2.push_back(str[i]); + + } + + } + + sort(m1.begin(),m1.end()); + sort(m2.begin(),m2.end()); + int j=0,k=0; + for(int i=0; i='a' && str[i]<='z') + + { + + res+=m1[j]; + + j++; + + } + + else + + { + + res+=m2[k]; + + k++; + + } + + } + + return res; + } +}; From 6a8729af805a1a52789cb2afbb5d000e094175c2 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 27 Jan 2023 22:32:11 +0530 Subject: [PATCH 60/83] Create v.i.p 27 January Total Decoding Messages 27 January Total Decoding Messages --- v.i.p 27 January Total Decoding Messages | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 v.i.p 27 January Total Decoding Messages diff --git a/v.i.p 27 January Total Decoding Messages b/v.i.p 27 January Total Decoding Messages new file mode 100644 index 00000000..545ded3b --- /dev/null +++ b/v.i.p 27 January Total Decoding Messages @@ -0,0 +1,30 @@ +class Solution { + public: + int mod=1e9+7; + vectordp; + bool valid(int n){ + return (n>=1&&n<=26); + } + int solve(int i,int n,string &s){ + if(i==n)return 1; + if(i>n)return 0; + if(dp[i]!=-1)return dp[i]%mod; + int choice1=0; + + int choice2=0; + if(s[i]!='0'){ + if(valid(s[i]-'0'))choice1=solve(i+1,n,s)%mod; + if(i(n+1,-1); + return solve(0,n,s); + } +}; From 12b1c31e7ebdae37c382e83f11819be313aa4d24 Mon Sep 17 00:00:00 2001 From: Michael-Calce <118388162+Michael-Calce@users.noreply.github.com> Date: Fri, 27 Jan 2023 22:34:31 +0530 Subject: [PATCH 61/83] Create v 27 January Total Decoding Messages 27 January Total Decoding Messages --- v 27 January Total Decoding Messages | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 v 27 January Total Decoding Messages diff --git a/v 27 January Total Decoding Messages b/v 27 January Total Decoding Messages new file mode 100644 index 00000000..be92cda1 --- /dev/null +++ b/v 27 January Total Decoding Messages @@ -0,0 +1,28 @@ +class Solution { + public: + const int mod = 1e9 + 7 ; + int dp[10001] ; + int dfs(string &s ,int i ){ + int n = s.size() ; + if(i >= n ) return 1 ; + if(dp[i] != -1 ) return dp[i] ; + int a = s[i] -'0' ; + if(a == 0 ) return 0 ; + int ans = 0 ; + if(i + 1 < n ) { + int b = s[i+1] - '0' ; + int f= a*10 + b ; + if(f <= 26 ) + ans = (ans + dfs(s , i+2 ) )%mod ; + } + ans = (ans + dfs(s , i +1))%mod ; + return dp[i] = ans ; + } + + int CountWays(string str){ + // Code here + memset(dp , -1 , sizeof(dp) ) ; + return dfs(str , 0 ) ; + } + +}; From 0c0b27ec735ba49d910a4352e27a20025a487b4b Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 28 Jan 2023 16:34:43 +0530 Subject: [PATCH 62/83] Create 28 January Scrambled String 28 January Scrambled String --- 28 January Scrambled String | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 28 January Scrambled String diff --git a/28 January Scrambled String b/28 January Scrambled String new file mode 100644 index 00000000..c56d781e --- /dev/null +++ b/28 January Scrambled String @@ -0,0 +1,28 @@ +class Solution{ + public: + mapdp; + bool f(string a,string b,int al,int bl){ + if(al!=bl || al<=0 )return 0; + string t = a+"-"+b; + if(dp.find(t) != dp.end())return dp[t]; + + if(al==1)return a==b; + for(int i=0;i Date: Sun, 29 Jan 2023 10:34:49 +0530 Subject: [PATCH 63/83] Create 29 January Min operations 29 January Min operations --- 29 January Min operations | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 29 January Min operations diff --git a/29 January Min operations b/29 January Min operations new file mode 100644 index 00000000..42bd05a0 --- /dev/null +++ b/29 January Min operations @@ -0,0 +1,8 @@ +class Solution { + public: + int solve(int a, int b) { + if(a==b) return 0; + else if((a|b)==a || (a|b)==b) return 1; + else return 2; + } +}; From ccf5f037994b69067f81cdf6137a1b7d59ef4d66 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 30 Jan 2023 13:55:05 +0530 Subject: [PATCH 64/83] Create 30 January Select Nodes 30 January Select Nodes --- 30 January Select Nodes | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 30 January Select Nodes diff --git a/30 January Select Nodes b/30 January Select Nodes new file mode 100644 index 00000000..edfc3bb3 --- /dev/null +++ b/30 January Select Nodes @@ -0,0 +1,29 @@ +class Solution{ + public: + void dfs(vector adj[],int src,int par,vector val[]){ + for(int v:adj[src]) + if(v!=par) + dfs(adj,v,src,val); + + for(int v:adj[src]){ + if(v!=par){ + val[src][0]+=val[v][1]; + val[src][1]+=min(val[v][1],val[v][0]); + } + } + } + int countVertex(int N, vector>edges){ + vector adj[N+1]; + for(int i=0;i val[N+1]; + for(int i=1;i<=N;i++){ + vector x={0,1}; + val[i]=x; + } + dfs(adj,1,1,val); + return min(val[1][0],val[1][1]); + } +}; From 8f3376cb77c30af5dc0749eb6a765f38d0e56e49 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 31 Jan 2023 15:43:20 +0530 Subject: [PATCH 65/83] Create 31 January Minimum times A has to be repeated such that B is a substring of it 31 January Minimum times A has to be repeated such that B is a substring of it --- ... repeated such that B is a substring of it | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 31 January Minimum times A has to be repeated such that B is a substring of it diff --git a/31 January Minimum times A has to be repeated such that B is a substring of it b/31 January Minimum times A has to be repeated such that B is a substring of it new file mode 100644 index 00000000..49a1a557 --- /dev/null +++ b/31 January Minimum times A has to be repeated such that B is a substring of it @@ -0,0 +1,20 @@ +class Solution { + public: + int minRepeats(string A, string B) { + string s = A; + int moves = 1; + while (A.size() < B.size()){ + A += s; + moves++; + } + if (A.find(B) != -1){ + return moves; + } + A += s; + moves++; + if (A.find(B) != -1){ + return moves; + } + return -1; + } +}; From 01bfe82c34b790c72193e6424a95fccd838e8b47 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 1 Feb 2023 20:13:08 +0530 Subject: [PATCH 66/83] Create 01 February Distinct Coloring 01 February Distinct Coloring --- 01 February Distinct Coloring | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 01 February Distinct Coloring diff --git a/01 February Distinct Coloring b/01 February Distinct Coloring new file mode 100644 index 00000000..544c30ce --- /dev/null +++ b/01 February Distinct Coloring @@ -0,0 +1,21 @@ +class Solution{ +public: + long long int distinctColoring(int N, int r[], int g[], int b[]){ + // code here + long long int dpr[N]; + long long int dpg[N]; + long long int dpb[N]; + dpr[0]=r[0]; + dpg[0]=g[0]; + dpb[0]=b[0]; + + for(int i=1;i Date: Thu, 2 Feb 2023 21:09:44 +0530 Subject: [PATCH 67/83] Create 02 February Largest Sum Cycle 02 February Largest Sum Cycle --- 02 February Largest Sum Cycle | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 02 February Largest Sum Cycle diff --git a/02 February Largest Sum Cycle b/02 February Largest Sum Cycle new file mode 100644 index 00000000..9f9c685a --- /dev/null +++ b/02 February Largest Sum Cycle @@ -0,0 +1,55 @@ +class Solution +{ + public: + vector> v; + vector vis,par,tmp; + long long dfs(int node,int p=-1){ + vis[node]=1; + par[node]=p; + tmp.push_back(node); + for(auto i:v[node]){ + if(vis[i]==0){ + long long z=dfs(i,node); + if(z!=-1){ + return z; + } + } + else if(vis[i]==1){ + long long sum=i; + while(node!=i){ + sum+=node; + node=par[node]; + } + if(node==i) + return sum; + return -1; + } + } + return -1; + } + long long largestSumCycle(int N, vector Edge) + { + long long ans=-1; + vis=vector(N); + v=vector>(N); + par=vector(N); + + for(int i=0;i Date: Fri, 3 Feb 2023 15:56:21 +0530 Subject: [PATCH 68/83] Create 03 February Last cell in a Matrix 03 February Last cell in a Matrix --- 03 February Last cell in a Matrix | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 03 February Last cell in a Matrix diff --git a/03 February Last cell in a Matrix b/03 February Last cell in a Matrix new file mode 100644 index 00000000..a7cd6617 --- /dev/null +++ b/03 February Last cell in a Matrix @@ -0,0 +1,24 @@ +class Solution{ + public: + pair endPoints(vector> matrix, int R, int C){ + //code here + int x=0; + int y=0; + int dir =0; + int dirx[] = {0,1,0,-1}; + int diry[] = {1,0,-1,0}; + while(x>=0 && y>=0 && x ans; + ans.first = x-dirx[dir]; + ans.second = y-diry[dir]; + return ans; + } +}; From d132137f0ee1fc734c160417fe2be8380970b6da Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 4 Feb 2023 13:31:41 +0530 Subject: [PATCH 69/83] Create 04 February Max Sum without Adjacents 04 February Max Sum without Adjacents --- 04 February Max Sum without Adjacents | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 04 February Max Sum without Adjacents diff --git a/04 February Max Sum without Adjacents b/04 February Max Sum without Adjacents new file mode 100644 index 00000000..fe25ef2c --- /dev/null +++ b/04 February Max Sum without Adjacents @@ -0,0 +1,19 @@ +class Solution{ +public: + // calculate the maximum sum with out adjacent + int findMaxSum(int *arr, int n) { + if(n==1) return arr[0]; + int a=arr[0]; + + int b=max(arr[0],arr[1]); + int curr=0; + for(int i=2;i Date: Sun, 5 Feb 2023 15:52:52 +0530 Subject: [PATCH 70/83] Create v.i.i 05 February Intersection Point in Y Shaped Linked Lists 05 February Intersection Point in Y Shaped Linked Lists --- ...y Intersection Point in Y Shaped Linked Lists | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 v.i.i 05 February Intersection Point in Y Shaped Linked Lists diff --git a/v.i.i 05 February Intersection Point in Y Shaped Linked Lists b/v.i.i 05 February Intersection Point in Y Shaped Linked Lists new file mode 100644 index 00000000..381accad --- /dev/null +++ b/v.i.i 05 February Intersection Point in Y Shaped Linked Lists @@ -0,0 +1,15 @@ +class Solution +{ +public: + int intersectPoint(Node* head1, Node* head2) + { + Node*x=head1,*y=head2; + while(x!=y){ + x=x->next; + y=y->next; + if(y==NULL) y=head1; + if(x==NULL) x=head2; + } + return x->data; + } +}; From 591fc839bd318ff8d6ad9432efb4e0a64a3fa74e Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 7 Feb 2023 20:05:03 +0530 Subject: [PATCH 71/83] Create 07 February Length of the longest subarray with positive product 07 February Length of the longest subarray with positive product --- ...the longest subarray with positive product | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 07 February Length of the longest subarray with positive product diff --git a/07 February Length of the longest subarray with positive product b/07 February Length of the longest subarray with positive product new file mode 100644 index 00000000..0a9647ad --- /dev/null +++ b/07 February Length of the longest subarray with positive product @@ -0,0 +1,28 @@ +class Solution { + public: + // Function to return the length of the + //longest subarray with ppositive product + int maxLength(vector &arr,int n){ + //code here + int poslen=0 , neglen=0 ; + int ans = 0; + for(int i=0;i0){ + poslen++ ; + if(neglen>0) neglen++ ; + ans = max(ans, poslen) ; + } + else{ + swap(poslen , neglen) ; + neglen++ ; + if(poslen>0) poslen++ ; + ans = max(ans, poslen) ; + } + } + return ans ; + } +}; From e0cc580682c902475f592804b328d84dcf476024 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 8 Feb 2023 16:42:50 +0530 Subject: [PATCH 72/83] Create 08 February Count number of free cell 08 February Count number of free cell --- 08 February Count number of free cell | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 08 February Count number of free cell diff --git a/08 February Count number of free cell b/08 February Count number of free cell new file mode 100644 index 00000000..13b13b0c --- /dev/null +++ b/08 February Count number of free cell @@ -0,0 +1,28 @@ +class Solution{ + public: + vector countZero(int n, int k, vector>& arr){ + long long int ans = n * 1LL * n; + vector row(n+1,false); + vector col(n+1,false); + int r = n, c = n; + vector v; + for(int i=0;i Date: Thu, 9 Feb 2023 18:49:55 +0530 Subject: [PATCH 73/83] Create 09 February Maximum Bipartite Matching 09 February Maximum Bipartite Matching --- 09 February Maximum Bipartite Matching | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 09 February Maximum Bipartite Matching diff --git a/09 February Maximum Bipartite Matching b/09 February Maximum Bipartite Matching new file mode 100644 index 00000000..e3038ad2 --- /dev/null +++ b/09 February Maximum Bipartite Matching @@ -0,0 +1,39 @@ +class Solution { +public: + int maximumMatch(vector>&G){ + + int m = G.size(); + + int n = G[0].size(); + + int match[n]; + + memset(match, -1, sizeof(match)); + + int res = 0; + + for (int i = 0; i < m; i++) { + + vector used(n, false); + + if (dfs(G, i, used, match)) res++; + + } + + return res; + +} + //Driver code +bool dfs(vector>& G, int u, vector& used, int match[]) { + for (int v = 0; v < G[0].size(); v++) { + if (G[u][v] == 1 && !used[v]) { + used[v] = true; + if (match[v] == -1 || dfs(G, match[v], used, match)) { + match[v] = u; + return true; + } + } + } + return false; +} +}; From 20924e7eb9e79b10ab6db5048634719cafa275ee Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 10 Feb 2023 21:58:23 +0530 Subject: [PATCH 74/83] Create 10 February Balloon Everywhere 10 February Balloon Everywhere --- 10 February Balloon Everywhere | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 10 February Balloon Everywhere diff --git a/10 February Balloon Everywhere b/10 February Balloon Everywhere new file mode 100644 index 00000000..c2c6c4db --- /dev/null +++ b/10 February Balloon Everywhere @@ -0,0 +1,11 @@ +class Solution{ +public: + int maxInstance(string s){ + vector v(26,0); + for(int i=0;i Date: Sat, 11 Feb 2023 20:48:59 +0530 Subject: [PATCH 75/83] Create 11 February Minimum Days 11 February Minimum Days --- 11 February Minimum Days | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 11 February Minimum Days diff --git a/11 February Minimum Days b/11 February Minimum Days new file mode 100644 index 00000000..f1849537 --- /dev/null +++ b/11 February Minimum Days @@ -0,0 +1,22 @@ +class Solution { + public: + int getMinimumDays(int N,string S, vector &P) { + // code here + int t=0,i,j; + for(i=0;i=0 && S[in-1]==S[in]) t--; + if(in+1 Date: Sun, 12 Feb 2023 14:52:12 +0530 Subject: [PATCH 76/83] Create 12 February Prime List 12 February Prime List --- 12 February Prime List | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 12 February Prime List diff --git a/12 February Prime List b/12 February Prime List new file mode 100644 index 00000000..bc78d5db --- /dev/null +++ b/12 February Prime List @@ -0,0 +1,58 @@ +class Solution + +{ + +public: + + Node *primeList(Node *head) + + { + + int a[10009] ={0}; + vectorv; + for (int i = 2; i <= 10008; i++) + + { + + if (a[i] == 0) + + { + + v.push_back(i); + + for (int j = i; j <= 10008; j += i) a[j] = 1; + + } + + } + + Node *f = head; + while (f != NULL) + + { + + auto r = lower_bound(v.begin(),v.end(),f->val); + + if(f->val==1) f->val=2; + + else{ + + if((*r)!=f->val){ + + int j=(f->val)-(*(--r)); + + int k=(*(++r))-(f->val); + + if(j>k) f->val=*r; + + else f->val=*(--r); + + }} + + f=f->next; + + } + + return head; } + +}; From b7bdcb22e07b824c1a69b50ec2e5dabe82db0e7e Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Mon, 13 Feb 2023 21:33:20 +0530 Subject: [PATCH 77/83] Create 13 February Arithmetic Number 13 February Arithmetic Number --- 13 February Arithmetic Number | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 13 February Arithmetic Number diff --git a/13 February Arithmetic Number b/13 February Arithmetic Number new file mode 100644 index 00000000..fb9fa315 --- /dev/null +++ b/13 February Arithmetic Number @@ -0,0 +1,6 @@ +class Solution{ +public: + int inSequence(int A, int B, int C){ + return (A==B) || (C!=0 && (B-A)%C==0 && (B-A)/C>0); + } +}; From 8aa17133ff5cd3f41100ec54647931ee2b496324 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 14 Feb 2023 15:14:15 +0530 Subject: [PATCH 78/83] Create 14 February Walls Coloring 14 February Walls Coloring --- 14 February Walls Coloring | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 14 February Walls Coloring diff --git a/14 February Walls Coloring b/14 February Walls Coloring new file mode 100644 index 00000000..9c4d342a --- /dev/null +++ b/14 February Walls Coloring @@ -0,0 +1,31 @@ +class Solution{ +public: + int cal(int i , int prev , int n , vector> &colors , vector>& dp) + { + if(i==n) + return 0; + + if(dp[i][prev+1]!=-1) + return dp[i][prev+1] ; + + int take=0 , mini =INT_MAX; + for(int j =0 ; j<3;j++) + { + if(prev==-1 || j!=prev) + { + take = colors[i][j] +cal(i+1 , j , n , colors , dp); + mini= min(mini , take ); + } + + } + + return dp[i][prev+1] = mini ; + } + + + int minCost(vector> &colors, int N) { + // Write your code here. + vector>dp(N+1, vector(4, -1 )) ; + return cal(0 , -1 ,N , colors , dp ) ; + } +}; From d9f281a10e13f594d1fc125bff40aeeb03a2f5b9 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 15 Feb 2023 23:37:46 +0530 Subject: [PATCH 79/83] Create 15 February Geeks Island 15 February Geeks Island --- 15 February Geeks Island | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 15 February Geeks Island diff --git a/15 February Geeks Island b/15 February Geeks Island new file mode 100644 index 00000000..4de31382 --- /dev/null +++ b/15 February Geeks Island @@ -0,0 +1,41 @@ +class Solution{ +public: + void dfs(vector> &mat,int i,int j,int n,int m,vector> &visited){ + if(i<0 || i>=n || j<0 || j>=m || visited[i][j]) + return; + visited[i][j] = true; + + if(j+1 < m && mat[i][j+1] >= mat[i][j] && !visited[i][j+1]) // right + dfs(mat,i,j+1,n,m,visited); + if(i+1 < n && mat[i+1][j] >= mat[i][j] && !visited[i+1][j]) // down + dfs(mat,i+1,j,n,m,visited); + if(j-1 >= 0 && mat[i][j-1] >= mat[i][j] && !visited[i][j-1]) //left + dfs(mat,i,j-1,n,m,visited); + if(i-1 >= 0 && mat[i-1][j] >= mat[i][j] && !visited[i-1][j])// up + dfs(mat,i-1,j,n,m,visited); + } + + int water_flow(vector> &mat,int N,int M){ + // Write your code here. + vector> indian(N,vector(M,false)); + vector> arabian(N,vector(M,false)); + + for(int j=0;j Date: Thu, 16 Feb 2023 20:39:56 +0530 Subject: [PATCH 80/83] Create 16 February Good Stones 16 February Good Stones --- 16 February Good Stones | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 16 February Good Stones diff --git a/16 February Good Stones b/16 February Good Stones new file mode 100644 index 00000000..11bc6ded --- /dev/null +++ b/16 February Good Stones @@ -0,0 +1,16 @@ +class Solution{ +public: + int solve(int idx,int n,vector &arr,vector &dp){ + if(idx>=n or idx<0) return 1; + if(dp[idx]!=-1) return dp[idx]; + dp[idx]=0; + return dp[idx]=solve(idx+arr[idx],n,arr,dp); + } + int goodStones(int n,vector &arr){ + vector dp(n,-1); + for(int i=0;i Date: Fri, 17 Feb 2023 20:19:56 +0530 Subject: [PATCH 81/83] Create 17 February Is it Fibonacci 17 February Is it Fibonacci --- 17 February Is it Fibonacci | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 17 February Is it Fibonacci diff --git a/17 February Is it Fibonacci b/17 February Is it Fibonacci new file mode 100644 index 00000000..bd9684b1 --- /dev/null +++ b/17 February Is it Fibonacci @@ -0,0 +1,29 @@ +class Solution { + + public: + + long long solve(int N, int K, vector GeekNum) { + long long sum = 0; + vector vec; + for (auto it : GeekNum) sum += it, vec.push_back(it); + int beg = 0; + + while (vec.size() < N) { + + vec.push_back(sum); + + sum -= vec[beg]; + + sum += vec.back(); + + beg++; + + } + + + + return vec[N - 1]; + + } + +}; From 5599833433d1e72574cb60fb8c011d7f5bdeaab6 Mon Sep 17 00:00:00 2001 From: Chayan Das <110921638+Chayandas07@users.noreply.github.com> Date: Sat, 18 Feb 2023 14:26:29 +0530 Subject: [PATCH 82/83] Create 18 February Apple Sequences 18 February Apple Sequences --- 18 February Apple Sequences | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 18 February Apple Sequences diff --git a/18 February Apple Sequences b/18 February Apple Sequences new file mode 100644 index 00000000..38c113b1 --- /dev/null +++ b/18 February Apple Sequences @@ -0,0 +1,24 @@ +class Solution{ +public: + int appleSequences(int n, int m, string arr){ + int start = 0, end = 0; + int curr=0; + int maxx=0; + while(endm){ + while(start Date: Fri, 26 May 2023 23:03:09 +0530 Subject: [PATCH 83/83] Create 26 May Express as sum of power of natural numbers --- ...Express as sum of power of natural numbers | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 26 May Express as sum of power of natural numbers diff --git a/26 May Express as sum of power of natural numbers b/26 May Express as sum of power of natural numbers new file mode 100644 index 00000000..f869fa2c --- /dev/null +++ b/26 May Express as sum of power of natural numbers @@ -0,0 +1,26 @@ +class Solution{ + public: + int mod=1e9+7; + int solve(int n,int x, int i,int max, vector>&dp){ + //base conditions + if(n==0)return 1; + if(i>max)return 0; + if(n<0)return 0; + + // checking if its done previosly or not + if(dp[n][i]!=-1)return dp[n][i]; + long long int t=pow(i,x); + + int pick=solve(n-t,x,i+1,max,dp); + int not_pick=solve(n,x,i+1,max,dp); + return dp[n][i]=(pick+not_pick)%mod; + } + + int numOfWays(int n, int x) + { + // code here + int maxLimit=pow(n,1.0/x); + vector> dp(n+1,vector(maxLimit+1,-1)); + return solve(n,x,1,maxLimit,dp); + } +};