├── 2023_DEC └── 11dec.cpp ├── 2023_FEB ├── 01 February.cpp ├── 02 February.cpp ├── 03 February.cpp ├── 04 February.cpp ├── 05 February.cpp └── 06 February.cpp ├── 2023_MAR ├── 01 March(Update Queries).cpp ├── 02 March(Walls Coloring II).cpp ├── 03 March (Cutting Rectangles).cpp ├── 04 March(Best Node).cpp ├── 05 March(Avoid Explosion).java ├── 06 March(Geek hates too many 1s).cpp ├── 07 March(Max Level Sum in Binary Tree).java ├── 08 March(Max min Height).cpp ├── 09 March(Find anagrams in linked list).java ├── 10 March(Maximum Triplet product).cpp ├── 11 March(Yet another query problem).java ├── 12 March(Binary matrix having maximum number of 1s).java ├── 21 March(Taxi Booking).java ├── 22 March (String rp or pr).cpp └── 23 March (BST Maximum Difference).cpp ├── 2024_April ├── 1st.cpp ├── 2nd.cpp ├── 3rd.cpp ├── 4th.cpp └── 5th.cpp ├── 2024_FEB ├── 10th.cpp ├── 11th.cpp ├── 12th.cpp ├── 13th.cpp ├── 14th.cpp ├── 1st.cpp ├── 2nd.cpp ├── 3rd.cpp ├── 4th.cpp ├── 7th.cpp ├── 8th.cpp └── 9th.cpp ├── 2024_JAN ├── 25th.cpp ├── 26th.cpp ├── 27th.cpp ├── 28th.cpp ├── 29th.cpp ├── 30th.cpp └── 31st.cpp └── README.md /2023_DEC/11dec.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long maximumSumSubarray(int k, vector &arr, int n) { 4 | long sum = 0; 5 | long max_sum = INT_MIN; 6 | int i = 0, j = 0; 7 | 8 | while (j < n) { 9 | sum += arr[j]; 10 | 11 | if (j - i + 1 == k) { 12 | max_sum = max(max_sum, sum); 13 | sum -= arr[i]; 14 | i++; 15 | } 16 | 17 | j++; 18 | } 19 | 20 | return max_sum; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /2023_FEB/01 February.cpp: -------------------------------------------------------------------------------- 1 | //Let's Solve the question Together 2 | 3 | class Solution{ 4 | public: 5 | long long int dp[3][50005]; 6 | long long int distinctColoring(int n, int r[], int g[], int b[]){ 7 | dp[0][0] = min(g[0],b[0]); 8 | dp[1][0] = min(r[0],b[0]); 9 | dp[2][0] = min(r[0],g[0]); 10 | for(int i = 1; i < n; i++){ 11 | long long int x = r[i] + dp[0][i-1]; 12 | long long int y = g[i] + dp[1][i-1]; 13 | long long int z = b[i] + dp[2][i-1]; 14 | dp[0][i] = min(y,z); 15 | dp[1][i] = min(x,z); 16 | dp[2][i] = min(x,y); 17 | } 18 | long long int ans = 1e18; 19 | for(int i = 0; i< 3; i++){ 20 | ans = min(ans,dp[i][n-1]); 21 | } 22 | return ans; 23 | } 24 | }; 25 | 26 | -------------------------------------------------------------------------------- /2023_FEB/02 February.cpp: -------------------------------------------------------------------------------- 1 | //Let's Solve the question Together .................................................................................................................... 2 | 3 | class Solution 4 | 5 | { 6 | 7 | public: 8 | 9 | long long largestSumCycle(int n, vector edge) { 10 | 11 | // code here//Let's Solve the question Together 12 | vector indegree(n, 0); 13 | 14 | for(auto it: edge) { 15 | 16 | if(it == -1) continue; 17 | 18 | indegree[it]++; 19 | 20 | } 21 | 22 | 23 | 24 | queue q; 25 | 26 | vector vis(n, false); 27 | 28 | for(int i = 0; i < n; i++) { 29 | 30 | if(indegree[i] == 0) { 31 | 32 | vis[i] = true; 33 | 34 | q.push(i); 35 | 36 | } 37 | 38 | } 39 | 40 | 41 | 42 | while(q.empty() == false) { 43 | 44 | int curr = q.front(); q.pop(); 45 | 46 | int par = edge[curr]; 47 | 48 | if(par == -1) continue; 49 | 50 | 51 | 52 | if(--indegree[par] == 0) { 53 | 54 | q.push(par); 55 | 56 | vis[par] = true; 57 | 58 | } 59 | 60 | } 61 | 62 | 63 | 64 | int res = -1; 65 | 66 | for(int i = 0; i < n; i++) { 67 | 68 | if(vis[i]) continue; 69 | 70 | int val = 0; 71 | 72 | for(int st = i; vis[st] == false; st = edge[st]) { 73 | 74 | val += st; 75 | 76 | vis[st] = true; 77 | 78 | } 79 | 80 | res = max(res, val); 81 | 82 | } 83 | 84 | return res; 85 | 86 | } 87 | 88 | }; 89 | 90 | -------------------------------------------------------------------------------- /2023_FEB/03 February.cpp: -------------------------------------------------------------------------------- 1 | // Let's Solve the question Together 2 | 3 | 4 | 5 | 6 | class Solution{ 7 | public: 8 | bool check(int x,int y,int n,int m){ 9 | if(x>=n||y>=m||x<0||y<0)return true; 10 | return false; 11 | } 12 | pair endPoints(vector>&a){ 13 | int n=a.size(); 14 | int m=a[0].size(); 15 | pairans{-1,-1}; 16 | int x=0,y=0; 17 | map,pair>mp; 18 | mp[{-1,0}]={0,1};// up ---> right 19 | mp[{0,1}]={1,0};// right ->> down 20 | mp[{1,0}]={0,-1}; // down ---> left 21 | mp[{0,-1}]={-1,0};// left ----> up 22 | int dx=0,dy=1; 23 | while(true){ 24 | int nxtx,nxty; 25 | nxtx=x+dx; 26 | nxty=y+dy; 27 | if(a[x][y]==0){ 28 | if(check(nxtx,nxty,n,m)){ 29 | ans={x,y}; 30 | break;} 31 | x=nxtx; 32 | y=nxty; 33 | } 34 | if(a[x][y]){ 35 | a[x][y]=0; 36 | pairp=mp[{dx,dy}]; 37 | dx=p.first; 38 | dy=p.second; 39 | } 40 | } 41 | return ans; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /2023_FEB/04 February.cpp: -------------------------------------------------------------------------------- 1 | //Let's Solve the question Together 2 | 3 | 4 | class Solution{ 5 | public: 6 | // calculate the maximum sum with out adjacent 7 | int findMaxSum(int *arr, int n) { 8 | if (n == 1) return arr[0]; 9 | 10 | int t0 = arr[0], t1 = max(arr[0], arr[1]); 11 | for (int i=2; inext==NULL)pointer_to_L1=head2; 14 | else 15 | pointer_to_L1=pointer_to_L1->next; 16 | if(pointer_to_L2->next==NULL)pointer_to_L2=head1; 17 | else 18 | pointer_to_L2=pointer_to_L2->next; 19 | } 20 | return pointer_to_L1->data; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /2023_FEB/06 February.cpp: -------------------------------------------------------------------------------- 1 | //User function Template for C++ 2 | /* 3 | struct Node { 4 | int data; 5 | Node *left; 6 | Node *right; 7 | 8 | Node(int val) { 9 | data = val; 10 | left = right = NULL; 11 | } 12 | }; 13 | */ 14 | 15 | class Solution{ 16 | public: 17 | bool f(Node*root,Node* &t,int target) 18 | { 19 | if(root==NULL) 20 | return false; 21 | if(root->data > target) 22 | f(root->left,t,target); 23 | else if(root->data < target) 24 | f(root->right,t,target); 25 | 26 | else{ t = root; return true;} 27 | } 28 | void bakeSum(Node*root,int val ,int comp,int &ans) 29 | { 30 | if(root==NULL) 31 | return; 32 | if(val==comp) 33 | ans+=root->data; 34 | bakeSum(root->left,val-1,comp,ans); 35 | bakeSum(root->right,val+1,comp,ans); 36 | } 37 | long long int verticallyDownBST(Node *root,int target){ 38 | // Code here 39 | Node*t=NULL; 40 | int ans = 0; 41 | bool s =f(root,t,target); 42 | if(!s) return -1; 43 | 44 | bakeSum(t,0,0,ans); 45 | return ans-t->data; 46 | 47 | } 48 | }; 49 | -------------------------------------------------------------------------------- /2023_MAR/01 March(Update Queries).cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | vector updateQuery(int n,int q,vector> &u){ 4 | // code here 5 | vector> bit(32,vector(n+1,0)); 6 | for(int i=0;i fans(n); 23 | for(int j=0;j<32;j++){ 24 | for(int i=0;i> &costs) { 6 | 7 | // write your code here 8 | 9 | int n=costs.size(),m=costs[0].size(); 10 | 11 | if(n>1 and m==1) { 12 | 13 | return -1; 14 | 15 | } 16 | 17 | int f_min=INT_MAX, s_min=INT_MAX, f_ind=-1; 18 | 19 | for(int i=0; i<1; ++i) { 20 | 21 | for(int j=0; j minimumSquares(long long int L, long long int B) 4 | { 5 | vector ans; 6 | long long int g = __gcd(L,B), prod = L*B, n = prod/(g*g); 7 | ans.push_back(n); ans.push_back(g); 8 | return ans; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /2023_MAR/04 March(Best Node).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long bestNode(int N, vector &A, vector &P) { 4 | vector adj[N+1]; 5 | for(int i=1; i adj[], vector &A, bool f){ 18 | long long ans = LLONG_MIN; 19 | for(int child: adj[node]){ 20 | ans = max(ans, dfs(child, adj, A, !f)); 21 | } 22 | 23 | long long nodeVal = A[node-1]; 24 | if(f) 25 | nodeVal = -nodeVal; 26 | if(ans == LLONG_MIN) 27 | return nodeVal; 28 | return ans + nodeVal; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /2023_MAR/05 March(Avoid Explosion).java: -------------------------------------------------------------------------------- 1 | // User function Template for Java 2 | 3 | class Solution { 4 | 5 | int parent[]; 6 | 7 | ArrayList avoidExlosion(int mix[][], int n, int danger[][], int m) { 8 | 9 | // Code Here 10 | 11 | ArrayListans=new ArrayList<>(); 12 | 13 | parent =new int[n+1]; 14 | 15 | for(int i=0;i<=n;i++){ 16 | 17 | parent[i]=i; 18 | 19 | } 20 | 21 | for(int i=0;i=0 ; i--){ 8 | if(str[i] == '1'){ 9 | dec_num += (1<= 0; i--) { 19 | int k = n >> i; 20 | if (k & 1) 21 | s += "1"; 22 | else 23 | s += "0"; 24 | } 25 | return s; 26 | } 27 | public: 28 | int noConseBits(int n) { 29 | // code here 30 | string s = decToBinary(n); 31 | 32 | for(int i = 2; i < s.size(); i++) { 33 | if(s[i] == '1' and s[i-1] == '1' and s[i-2] == '1')s[i] = '0'; 34 | } 35 | 36 | return binaryToDecimal(s); 37 | 38 | 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /2023_MAR/07 March(Max Level Sum in Binary Tree).java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public int maxLevelSum(Node root) { 4 | 5 | // add code here. 6 | 7 | if(root==null) 8 | 9 | return 0; 10 | 11 | Queueq=new LinkedList<>(); 12 | 13 | q.offer(root); 14 | 15 | int maximum=Integer.MIN_VALUE; 16 | 17 | while(!q.isEmpty()) 18 | 19 | { 20 | 21 | int size=q.size(); 22 | 23 | int sum=0; 24 | 25 | for(int i=0;i &arr, int days, int cs, long long int h){ 4 | int n=arr.size(); 5 | 6 | vector waterSupply(n,0); 7 | 8 | if(arr[0]=cs){ 22 | actualHeight+=(waterSupply[i]-waterSupply[i-cs]); 23 | } 24 | else{ 25 | actualHeight+=waterSupply[i]; 26 | } 27 | 28 | if(actualHeight &a){ 40 | long long int res=-1; 41 | long long int mnHeight=*min_element(a.begin(),a.end()); 42 | long long int mxHeight=INT_MAX/2; 43 | while(mnHeight<=mxHeight){ 44 | int guessHeight=(mxHeight+mnHeight)/2; 45 | if(isOK(a,k,w,guessHeight)==true){ 46 | res=guessHeight; 47 | mnHeight=guessHeight+1; 48 | } 49 | else{ 50 | mxHeight=guessHeight-1; 51 | } 52 | } 53 | return res; 54 | } 55 | }; 56 | -------------------------------------------------------------------------------- /2023_MAR/09 March(Find anagrams in linked list).java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static Node createList(Node low, Node high){ 3 | Node newList=new Node(' '); 4 | Node newListHead=newList; 5 | 6 | Node temp=low; 7 | while(temp!=high){ 8 | char curr=temp.data; 9 | 10 | newList.next=new Node(curr); 11 | newList=newList.next; 12 | 13 | temp=temp.next; 14 | } 15 | 16 | return newListHead.next; 17 | } 18 | public static ArrayList findAnagrams(Node head, String s) { 19 | int savedTime=0; 20 | int time=1; 21 | int n=s.length(); 22 | Map map=new HashMap<>(); 23 | 24 | //store original string freq 25 | for(int i=0; i currMap=new HashMap<>(); 33 | ArrayList res=new ArrayList<>(); 34 | 35 | //store initial freq 36 | int i=0; 37 | while(high!=null && i=n)){ 50 | savedTime=time; 51 | //save 52 | Node list=createList(low, high); 53 | res.add(list); 54 | } 55 | 56 | //logic 57 | while(high!=null){ 58 | char prev=low.data; 59 | char curr=high.data; 60 | 61 | //acquire 62 | currMap.put(curr, currMap.getOrDefault(curr, 0) + 1); 63 | 64 | //release 65 | if(currMap.get(prev)==1 ){ 66 | currMap.remove(prev); 67 | } 68 | else{ 69 | currMap.put(prev, currMap.get(prev) - 1); 70 | } 71 | 72 | //inc ptrs 73 | low=low.next; 74 | high=high.next; 75 | time++; 76 | 77 | //check 78 | if(map.equals(currMap) && ((time-savedTime)>=n)){ 79 | savedTime=time; 80 | //save 81 | Node list=createList(low, high); 82 | res.add(list); 83 | } 84 | 85 | } 86 | 87 | return res; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /2023_MAR/10 March(Maximum Triplet product).cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | //naive approach O(nlogn) 5 | /* 6 | long long maxTripletProduct(long long arr[], int n) 7 | { 8 | sort(arr,arr+n); 9 | 10 | long long mn1=arr[0],mn2=arr[1],mx1=arr[n-1],mx2=arr[n-2],mx3=arr[n-3]; 11 | 12 | return max((mn1*mn2*mx1),(mx1*mx2*mx3)); 13 | } 14 | */ 15 | 16 | 17 | 18 | //Eureka approach O(n) 19 | long long maxTripletProduct(long long arr[], int n) 20 | { 21 | 22 | long long mn1=INT_MAX,mn2=INT_MAX,mx1=INT_MIN,mx2=INT_MIN,mx3=INT_MIN; 23 | 24 | for(int p=0;pmx1){ 39 | mx3=mx2; 40 | mx2=mx1; 41 | mx1=arr[p]; 42 | }else if(arr[p]>mx2){ 43 | mx3=mx2; 44 | mx2=arr[p]; 45 | }else if(arr[p]>mx3){ 46 | mx3=arr[p]; 47 | } 48 | } 49 | 50 | return max((mn1*mn2*mx1),(mx1*mx2*mx3)); 51 | } 52 | 53 | }; 54 | -------------------------------------------------------------------------------- /2023_MAR/11 March(Yet another query problem).java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public static ArrayList solveQueries(int n, int num, int[] arr, int[][] qrr) { 4 | 5 | ArrayList> al=new ArrayList<>(); 6 | 7 | for(int i=0; i()); 8 | 9 | for(int i=0; i ans=new ArrayList<>(); 12 | 13 | for(int i=0; i l=new ArrayList<>(); 4 | int ans=0; 5 | for(int i=0;i y) 64 | { 65 | pr(s, x, y, ans, true); 66 | } 67 | else 68 | { 69 | rp(s, x, y, ans, true); 70 | } 71 | return ans; 72 | } 73 | }; 74 | -------------------------------------------------------------------------------- /2023_MAR/23 March (BST Maximum Difference).cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int solve(Node* root){ 4 | if(!root) return 0; 5 | if(!root->left && !root->right){ 6 | return root->data; 7 | } 8 | int leftData=solve(root->left); 9 | int rightData=solve(root->right); 10 | if(!leftData) return root->data+rightData; 11 | if(!rightData) return root->data+leftData; 12 | return root->data+min(leftData,rightData); 13 | } 14 | int maxDifferenceBST(Node *root,int target){ 15 | Node* temp=root; 16 | bool find=false; 17 | int sum=0; 18 | while(temp){ 19 | if(temp->data!=target){ 20 | sum+=temp->data; 21 | } 22 | else{ 23 | find=true; 24 | break; 25 | } 26 | if(temp->data>target){ 27 | temp=temp->left; 28 | } 29 | else{ 30 | temp=temp->right; 31 | } 32 | } 33 | if(!find) return -1; 34 | int sum1; 35 | if(!solve(temp->right)) sum1=solve(temp->left); 36 | else if(!solve(temp->left)) sum1=solve(temp->right); 37 | else sum1=min(solve(temp->left),solve(temp->right)); 38 | return sum-sum1; 39 | } 40 | 41 | 42 | }; 43 | -------------------------------------------------------------------------------- /2024_April/1st.cpp: -------------------------------------------------------------------------------- 1 | // User function Template for C++ 2 | 3 | /*// A Tree node 4 | struct Node 5 | { 6 | int data; 7 | struct Node *left, *right; 8 | };*/ 9 | 10 | class Solution { 11 | public: 12 | void mergeArray(vector&v,int low,int mid,int high,int &c) 13 | { 14 | vectortemp; 15 | int left = low; 16 | int right = mid+1; 17 | while(left<=mid and right <= high) 18 | { 19 | if(v[left] <= v[right]) 20 | { 21 | 22 | temp.push_back(v[left++]); 23 | } 24 | else 25 | { c += mid-left+1; 26 | 27 | temp.push_back(v[right++]); 28 | } 29 | 30 | } 31 | while(left<=mid) 32 | { 33 | temp.push_back(v[left++]); 34 | // c++; 35 | } 36 | while(right<=high) 37 | { 38 | temp.push_back(v[right++]); 39 | // c++; 40 | } 41 | for(int i = low;i<=high;i++) 42 | { 43 | v[i] = temp[i-low]; 44 | } 45 | } 46 | void mergeSort(vector&v,int low,int high,int &c) 47 | { 48 | if(low >= high) 49 | return; 50 | int mid = (low+high) / 2; 51 | mergeSort(v,low,mid,c); 52 | mergeSort(v,mid+1,high,c); 53 | mergeArray(v,low,mid,high,c); 54 | } 55 | void inorder(Node* root,vector&v) 56 | { 57 | if(root==NULL) 58 | return; 59 | inorder(root->left,v); 60 | v.push_back(root->data); 61 | inorder(root->right,v); 62 | } 63 | /*You are required to complete below function */ 64 | int pairsViolatingBST(int n, Node *root) { 65 | // your code goes here 66 | vectorv; 67 | inorder(root,v); 68 | int ans = 0; 69 | mergeSort(v,0,n-1,ans); 70 | return ans; 71 | } 72 | }; -------------------------------------------------------------------------------- /2024_April/2nd.cpp: -------------------------------------------------------------------------------- 1 | /*The Node structure is defined as 2 | struct Node { 3 | int data; 4 | Node *left; 5 | Node *right; 6 | 7 | }; 8 | */ 9 | 10 | class Solution 11 | { 12 | public: 13 | void inorder(Node* node, Node*& prev, int& minDiff) { 14 | if (node == nullptr) return; 15 | 16 | inorder(node->left, prev, minDiff); 17 | 18 | if (prev != nullptr) { 19 | minDiff = min(minDiff, node->data - prev->data); 20 | } 21 | prev = node; 22 | 23 | inorder(node->right, prev, minDiff); 24 | } 25 | 26 | int absolute_diff(Node *root) 27 | { 28 | int minDiff = INT_MAX; 29 | Node* prev = nullptr; 30 | inorder(root, prev, minDiff); 31 | return minDiff; 32 | } 33 | }; -------------------------------------------------------------------------------- /2024_April/3rd.cpp: -------------------------------------------------------------------------------- 1 | //User function Template for C++ 2 | 3 | /*// A Tree node 4 | struct Node 5 | { 6 | int data; 7 | struct Node *left, *right; 8 | };*/ 9 | 10 | 11 | class Solution 12 | { 13 | public: 14 | 15 | int kthCommonAncestor(Node *root, int k,int x, int y) 16 | { 17 | vector arr; 18 | Node* t = root; 19 | int mn = min(x,y); 20 | int mx = max(x,y); 21 | while(t!=NULL) 22 | { 23 | if(t->data < mn) 24 | { 25 | arr.push_back(t->data); 26 | t=t->right; 27 | } 28 | else if(t->data > mx) 29 | { 30 | arr.push_back(t->data); 31 | t=t->left; 32 | } 33 | else 34 | { 35 | arr.push_back(t->data); 36 | if(k>arr.size()) return -1; 37 | return arr[arr.size()-k]; 38 | } 39 | } 40 | return -1; 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /2024_April/4th.cpp: -------------------------------------------------------------------------------- 1 | long long sumSubstrings(string s){ 2 | // long long int ans=0; 3 | long long int mod=1e9+7; 4 | long long int r=1,res=0; 5 | for(int i=s.size()-1;i>=0;i--){ 6 | // long long int 7 | res=(res+((s[i]-'0')*(i+1)*r)%mod)%mod; 8 | res%=mod; 9 | r=(r*10+1)%mod; 10 | r%=mod; 11 | 12 | } 13 | return(res); 14 | } -------------------------------------------------------------------------------- /2024_April/5th.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution{ 3 | private: 4 | int n; 5 | vector nums; 6 | map ,int> dp; 7 | public: 8 | int dfs(int i, int p){ 9 | if(i == n) 10 | return 0; 11 | int a = INT_MAX, b = INT_MAX; 12 | if(dp.find({i, p}) != dp.end()) 13 | return dp[{i, p}]; 14 | if(nums[i] > p) 15 | a = dfs(i+1, nums[i]); 16 | b = 1+dfs(i+1, p+1); 17 | return dp[{i, p}] = min(a, b); 18 | } 19 | int min_operations(vectornums){ 20 | n = nums.size(); 21 | this->nums = nums; 22 | return dfs(0, -10000000); 23 | } 24 | }; -------------------------------------------------------------------------------- /2024_FEB/10th.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | 5 | long long solver(int i, int j, int n, int k, vector> &arr, vector>> &dp) { 6 | 7 | if (i == n - 1 && j == n - 1) { 8 | if (k == arr[i][j]){ 9 | return 1; 10 | } 11 | else{ 12 | return 0; 13 | } 14 | } 15 | 16 | if (dp[i][j][k] != -1){ 17 | return dp[i][j][k]; 18 | } 19 | 20 | long long right = 0; 21 | long long down = 0; 22 | 23 | if (i + 1 <= n - 1 && k - arr[i][j] >= 0){ 24 | right = solver(i + 1, j, n, k - arr[i][j], arr, dp); 25 | } 26 | if (j + 1 <= n - 1 && k - arr[i][j] >= 0){ 27 | down = solver(i, j + 1, n, k - arr[i][j], arr, dp); 28 | } 29 | 30 | return dp[i][j][k] = right + down; 31 | 32 | } 33 | 34 | long long numberOfPath(int n, int k, vector> arr) { 35 | 36 | vector>> dp(n, vector>(n, vector(k + 1, -1))); 37 | return solver(0, 0, n, k, arr, dp); 38 | 39 | } 40 | 41 | 42 | }; 43 | -------------------------------------------------------------------------------- /2024_FEB/11th.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | vector recamanSequence(int n){ 4 | vector ans(n,0); 5 | set s; 6 | s.insert(0); 7 | for(int i=1;i0 && s.count(ans[i-1]-i)==0) 9 | ans[i]=ans[i-1]-i; 10 | else 11 | ans[i]=ans[i-1]+i; 12 | s.insert(ans[i]); 13 | } 14 | return ans; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /2024_FEB/12th.cpp: -------------------------------------------------------------------------------- 1 | #define mod 1000000007 2 | class Solution{ 3 | public: 4 | long long solve(int n){ 5 | 6 | if(n==1) 7 | return 1; 8 | 9 | int cnt= (n*(n-1))/2; 10 | long long prod=1; 11 | cnt++; 12 | for(int i=0;i q; 6 | map clone; 7 | 8 | clone[src] = new Node(src->val); 9 | q.push(src); 10 | 11 | Node *temp1; 12 | vector temp2; 13 | 14 | while(!q.empty()) 15 | { 16 | temp1 = q.front(); 17 | q.pop(); 18 | temp2 = temp1->neighbors; 19 | 20 | for(int i = 0; i < temp2.size();i++) 21 | { 22 | if(!clone[temp2[i]]) 23 | { 24 | clone[temp2[i]] = new Node(temp2[i]->val); 25 | q.push(temp2[i]); 26 | } 27 | clone[temp1]->neighbors.push_back(clone[temp2[i]]); // Important step 28 | } 29 | } 30 | return clone[src]; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /2024_FEB/14th.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int dfs(vector graph[], int i, vector& rank, vector>& res, int prev_rank, int n) { 5 | // increment the current node's rank 6 | rank[i] = prev_rank + 1; 7 | int t = INT_MAX; 8 | // traverse the neibours 9 | for(const auto& nbr: graph[i]) { 10 | if(rank[nbr] == -1) { 11 | // not visited 12 | int k = dfs(graph, nbr, rank, res, prev_rank+1, n); 13 | if( k > rank[i]) 14 | // problem statement to store edges in order 15 | res.push_back({min(i, nbr), max(i,nbr)}); 16 | t = min(t, k); 17 | 18 | } else if(rank[nbr] == n+1) 19 | continue; 20 | else if(rank[nbr] != prev_rank) 21 | t = min(t, rank[nbr]); 22 | } 23 | rank[i] = n + 1; 24 | return t; 25 | } 26 | vector>criticalConnections(int v, vector adj[]){ 27 | // Tarzan's Algorithm 28 | vector> res; 29 | vector rank(v, -1); 30 | 31 | int k = dfs(adj, 0, rank, res, 0, v); 32 | sort(res.begin(), res.end()); 33 | return res; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /2024_FEB/1st.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool checkPangram (string s) 5 | { 6 | int chk=0; 7 | for(int i=0;i='a'&&s[i]<='z') chk|=(1<<(s[i]-'a')); 10 | else if(s[i]>='A'&&s[i]<='Z') chk|=(1<<(s[i]-'A')); 11 | if(chk+1==(1<<26)) return true; 12 | } 13 | return false; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /2024_FEB/2nd.cpp: -------------------------------------------------------------------------------- 1 | int atoi(string str) { 2 | for(int i=0;i='a' && str[i]<='z')) 4 | return -1; 5 | if(i>0 && str[i] =='-') 6 | return -1; 7 | } 8 | int ans=0; 9 | bool flag=false; 10 | for(int i=0;idata; 12 | ans = ans%MOD; 13 | temp = temp->next; 14 | } 15 | return ans; 16 | // Your Code Here 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /2024_FEB/4th.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | Node *sortedInsert(Node* head, int data) // simply for checking if head is null or not 5 | { 6 | if(head == NULL) { 7 | Node* newNode = new Node(data); 8 | newNode->next = newNode; 9 | return newNode; 10 | } 11 | if(head->data>=data) // if element is to be inserted first 12 | { 13 | Node* dum=new Node(data); 14 | dum->next=head; 15 | Node* temp=head; 16 | while(temp->next!=head) 17 | { 18 | temp=temp->next; 19 | } 20 | temp->next=dum; 21 | return dum; 22 | } 23 | else if(head->datanext; 26 | Node* prev=head; 27 | while(temp->datanext; 31 | } 32 | if(temp==head) // if element is inserted at end 33 | { 34 | Node* dum3=new Node(data); 35 | prev->next=dum3; 36 | dum3->next=temp; 37 | return head; 38 | } 39 | else { // for simply inserting the element in between 40 | Node* dum2=new Node(data); 41 | prev->next=dum2; 42 | dum2->next=temp; 43 | return head; 44 | 45 | } 46 | 47 | 48 | } 49 | return NULL; 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /2024_FEB/7th.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to return count of nodes at a given distance from leaf nodes. 5 | int c = 0; 6 | 7 | int printKDistantfromLeaf(Node* root, int k) 8 | { 9 | vector v; 10 | distantfromLeaf(root, v, k, 0); 11 | return c; 12 | } 13 | 14 | void distantfromLeaf(Node* node, vector &v, int k, int d) 15 | { 16 | if(node == NULL) 17 | return; 18 | 19 | v.push_back(true); 20 | 21 | if(node->left == NULL && node->right == NULL && d >= k && v[d - k]) 22 | { 23 | v[d - k] = false; 24 | c++; 25 | } 26 | 27 | distantfromLeaf(node->left, v, k, d + 1); 28 | distantfromLeaf(node->right, v, k, d + 1); 29 | 30 | v.pop_back(); 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /2024_FEB/8th.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | bool check(Node *root) 4 | { 5 | int level = -1; 6 | queue> q; 7 | q.push({root, 0}); 8 | while(q.empty()==false){ 9 | Node* curr = q.front().first; 10 | int lvl = q.front().second; 11 | q.pop(); 12 | if(!curr->left && !curr->right){ 13 | if(level == -1){ 14 | level = lvl; 15 | } 16 | else if(level != lvl){ 17 | return false; 18 | } 19 | } 20 | if(curr->left) q.push({curr->left, lvl+1}); 21 | if(curr->right) q.push({curr->right, lvl+1}); 22 | } 23 | return true; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /2024_FEB/9th.cpp: -------------------------------------------------------------------------------- 1 | /*Complete the function below 2 | 3 | struct Node 4 | { 5 | int data; 6 | struct Node* left; 7 | struct Node* right; 8 | 9 | Node(int x){80 10 | data = x; 11 | left = right = NULL; 12 | } 13 | }; 14 | */ 15 | 16 | class Solution{ 17 | public: 18 | //Function to check whether all nodes of a tree have the value 19 | //equal to the sum of their child nodes. 20 | int isSumProperty(Node *root) 21 | { 22 | // Add your code here 23 | if(root==nullptr ||(root->left==NULL && root->right==NULL)){ 24 | return 1; 25 | } 26 | 27 | 28 | int left = 0, right = 0; 29 | 30 | if(root->left!=NULL){ 31 | left = root->left->data; 32 | } 33 | 34 | if(root->right!=NULL){ 35 | right = root->right->data; 36 | } 37 | 38 | if(root->data==left+right){ 39 | return isSumProperty(root->left) && isSumProperty(root->right); 40 | } else { 41 | return 0; 42 | } 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /2024_JAN/25th.cpp: -------------------------------------------------------------------------------- 1 | //PLEASE GIVE STAR IF YOU LIKE THE CODE 2 | class Solution{ 3 | private: 4 | bool isPrime(int n){ 5 | for(int i=2;i*i<=n;i++){ 6 | if(n%i==0) return 0; 7 | } 8 | return 1; 9 | } 10 | public: 11 | int solve(int Num1,int Num2) 12 | { 13 | unordered_set seen; // 14 | queue q; 15 | q.push(Num1); 16 | seen.insert(Num1); 17 | int levels=0; 18 | while(q.size()){ 19 | int sz=q.size(); 20 | while(sz--){ // process all nodes at the level in this iteration 21 | int num=q.front(); 22 | q.pop(); 23 | if(num==Num2) return levels; 24 | for(int i=0;i<4;i++){ // 3124 // pow(10,1) -> 10 25 | int factor=pow(10,i); // 26 | int temp=num; 27 | temp-=(factor*((temp/factor)%10)); 28 | 29 | for(int val=0;val<=9;val++){ 30 | temp+=(factor*val); 31 | if(temp>1000 && !seen.count(temp) && isPrime(temp)){ 32 | q.push(temp); 33 | seen.insert(temp); 34 | } 35 | temp-=(factor*val); 36 | // 60 37 | // 3164 -> 3104... 38 | 39 | } 40 | } 41 | } 42 | levels++; 43 | } 44 | return -1; 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /2024_JAN/26th.cpp: -------------------------------------------------------------------------------- 1 | //PLEASE GIVE STAR IF YOU LIKE THE CODE 2 | 3 | class Solution 4 | { 5 | public: 6 | //Function to get the maximum total value in the knapsack. 7 | double fractionalKnapsack(int W, Item arr[], int n) 8 | { 9 | // Your code here 10 | vector> v; 11 | for(int i=0; i dp[n][n] ; 6 | 7 | for(int gap = 1 ; gap cost){ 20 | dp[i][j].first = cost ; 21 | dp[i][j].second = "(" + dp[i][k].second + dp[k][j].second + ")" ; 22 | } 23 | } 24 | } 25 | } 26 | } 27 | 28 | return dp[0][n-1].second ; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /2024_JAN/28th.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long findNthNumber(int n, int k) { 4 | long long low = 0, high = 1e18; 5 | dp.resize(2, vector>(65, vector(65))); 6 | 7 | while (low <= high) { 8 | long long mid = low + (high - low) / 2; 9 | long long count = find(mid, k); 10 | if (count >= n) 11 | high = mid - 1; 12 | else 13 | low = mid + 1; 14 | } 15 | return low; 16 | } 17 | 18 | private: 19 | 20 | struct Triple { 21 | long long x, y, z; 22 | Triple(long long a, long long b, long long c) : x(a), y(b), z(c) {} 23 | }; 24 | 25 | vector>> dp; 26 | 27 | long long find(long long n, int k) { 28 | string s = bitset<64>(n).to_string(); 29 | reset(); 30 | return dpf(s, s.length(), 1, k); 31 | } 32 | 33 | long long dpf(string& s, int n, int tight, int k) { 34 | if (k < 0) 35 | return 0; 36 | if (n == 0) { 37 | return 1; 38 | } 39 | if (dp[tight][k][n] != -1) 40 | return dp[tight][k][n]; 41 | int ub = (tight == 1 ? s[s.length() - n] - '0' : 1); 42 | long long ans = 0; 43 | for (int dig = 0; dig <= ub; dig++) { 44 | if (dig == ub) 45 | ans += dpf(s, n - 1, tight, k - dig); 46 | else 47 | ans += dpf(s, n - 1, 0, k - dig); 48 | } 49 | return dp[tight][k][n] = ans; 50 | } 51 | 52 | void reset() { 53 | for (int i = 0; i < 65; i++) { 54 | for (int j = 0; j < 65; j++) { 55 | dp[0][i][j] = -1; 56 | dp[1][i][j] = -1; 57 | } 58 | } 59 | } 60 | }; 61 | -------------------------------------------------------------------------------- /2024_JAN/29th.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution{ 3 | public: 4 | // dp[ind][prevsum] 5 | int dp[101][902]; 6 | 7 | int recur(int ind, int sum, string& str){ 8 | if(ind>=str.size())return 1; 9 | if(dp[ind][sum]!=-1)return dp[ind][sum]; 10 | int ans=0; 11 | int sum1=0; 12 | for(int i=ind;i=sum)ans+=recur(i+1,sum1,str); 15 | } 16 | return dp[ind][sum]=ans; 17 | } 18 | 19 | int TotalCount(string str){ 20 | // Code here 21 | memset(dp, -1, sizeof(dp)); 22 | return recur(0,0,str); 23 | } 24 | 25 | }; 26 | -------------------------------------------------------------------------------- /2024_JAN/30th.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution 4 | { 5 | public: 6 | 7 | // Function to find the length of the longest common subsequence of three strings 8 | int LCSof3 (string A, string B, string C, int n1, int n2, int n3) 9 | { 10 | // Create a 3D dynamic programming table 11 | int dp[n1+1][n2+1][n3+1]; 12 | 13 | // Initialize the table with zeros 14 | memset(dp, 0, sizeof(dp)); 15 | 16 | // Iterate over the first string 17 | for(int i=1; i<=n1; i++) 18 | { 19 | // Iterate over the second string 20 | for(int j=1; j<=n2; j++) 21 | { 22 | // Iterate over the third string 23 | for(int k=1; k<=n3; k++) 24 | { 25 | // If the current characters of all three strings are equal 26 | if (A[i-1] == B[j-1] && B[j-1] == C[k-1]) 27 | { 28 | // Increment the length of the common subsequence 29 | dp[i][j][k]=dp[i-1][j-1][k-1]+1; 30 | } 31 | else 32 | { 33 | // If the current characters are not equal, take the maximum length of the subsequence found so far 34 | dp[i][j][k]=max(max(dp[i-1][j][k], dp[i][j-1][k]),dp[i][j][k-1]); 35 | } 36 | } 37 | } 38 | } 39 | 40 | // Return the length of the longest common subsequence 41 | return dp[n1][n2][n3]; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /2024_JAN/31st.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | 3 | { 4 | 5 | public: 6 | 7 | //Function to insert string into TRIE. 8 | 9 | void insert(struct TrieNode *root, string key) 10 | 11 | { 12 | 13 | // code here 14 | 15 | for (int i=0;ichildren[key[i]-'a']==NULL) 20 | 21 | {root->children[key[i]-'a']=new TrieNode();} 22 | 23 | root=root->children[key[i]-'a']; 24 | 25 | } 26 | 27 | root->isLeaf=true; 28 | 29 | } 30 | 31 | 32 | 33 | //Function to use TRIE data structure and search the given string. 34 | 35 | bool search(struct TrieNode *root, string key) 36 | 37 | { 38 | 39 | // code here 40 | 41 | for (int i=0;ichildren[key[i]-'a']==NULL) return false; 46 | 47 | root=root->children[key[i]-'a']; 48 | 49 | } 50 | 51 | if (root->isLeaf==true) return true; 52 | 53 | return false; 54 | 55 | } 56 | 57 | }; 58 | 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Banner 10 | 11 |

GFG-POTD: Practice of the Day

12 | 13 | 14 | 15 |

Welcome to GFG-POTD, a comprehensive repository where you can find solutions to all the daily problems posted on 16 | Geeks For Geeks. This repository is perfect for those who are looking to enhance their problem-solving skills 17 | and master various programming concepts.

18 | 19 |

🚀 Features

20 |
    21 |
  • Daily updated solutions
  • 22 |
  • Clear and concise code with comments
  • 23 |
  • Covers a wide range of topics
  • 24 |
  • Perfect for interview preparation
  • 25 |
26 |

📚 How to Use

27 |

To use this repository, you can either clone it to your local machine or browse the solutions directly on GitHub. 28 | Each solution is placed in its own directory with the problem statement included.

29 |

🛠️ Contributing

30 |

Contributions are always welcome! If you have solutions to any problem that's not currently in the repository, 31 | feel free to make a pull request.

32 |

📖 Languages Used

33 |
    34 |
  • Python
  • 35 |
  • Java
  • 36 |
  • C++
  • 37 |
  • JavaScript
  • 38 |
39 |

📈 Stats

40 | Repo Stats 42 |

📮 Contact

43 |

If you have any questions, feel free to reach out to me at your-Rai.ashutosh.pvt@example.com.

44 |

📃 License

45 |

This project is licensed under the MIT License - see the LICENSE.md file for details. 46 |

47 |

🙏 Acknowledgments

48 |
    49 |
  • Geeks For Geeks for providing daily problems
  • 50 |
  • All the contributors who help to make this repository better
  • 51 |
52 | 53 | 54 | 55 | --------------------------------------------------------------------------------