Skip to main content

Posts

Path in Matrix

Task: Find the maximum path sum from given N*N matrix, Start from any column in row 0, move in three paths from current position Matrix[r][c] to Matrix[r+1][c] or Matrix[r+1][c-1] or Matrix[r+1][c+1] up to row N-1.  Example: Input: 1 2  348 391 618 193  Output: 1009 // 391 + 618  Here is the Code: Program in C++: #include<iostream> using namespace std; int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;     int a[n][n];     for(int i=0;i<n;i++)     {         for(int j=0;j<n;j++)         {             cin>>a[i][j];         }     }     int dp[n][n];     for(int i=0;i<n;i++)     {         dp[0][i]=a[0][i];     }     for(int i=1;i<n;i++)     {         for(int j=0;j<n;j++)         {             if(j==0)             {                 dp[i][j]=a[i][j]+max(dp[i-1][j],dp[i-1][j+1]);             }             else if(j==n-1)             {                 dp[i][j]=a[i][j]+max(dp[i-1][j-

Total Decoding Messages

Task:  Number of decoding messages from given number(in the form of string), The decoding way is 1 with A, 2 with B ... 26 with Z. Example: Input: 1 123 Output: 3 // ABC,LC and AW. Here is the Code: Program in C++: #include<iostream> using namespace std; int totalNumberOfWays(char *a,int n) {     if(isalpha(n)) return 1;     if(a[0]=='0') return 0;     int p=1,q=1,res=1;     for(int i=1;i<n;i++)     {         int t = (a[i-1]-'0')*10+a[i]-'0';         if(t>=10&&t<=26)         {             if(t==10||t==20) res=p;             else res=p+q;         }         else         {             if(t%10==0) return 0;             else res=q;         }         p=q;         q=res;     }     return res; } int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;     char a[n];     cin>>a;     int now = totalNumberOfWays(a,n);     cout<<now<<endl; } return 0; } Here is the Video:

Pythagorean Triplet

Task: Given an array of integers, If a, b, c are integers in an array and satisfies the Pythagorean formulae : a^2 + b^2 = c^2 then print Yes else print No. Example: Input: 1 5 3 2 4 1 5 Output: Yes // 3,4, and 5 are Pythagorean Triplets. Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; string pythagoreanTriplet(int *a,int n) {     set<int> s;     for(int i=0;i<n;i++)     {         a[i]=a[i]*a[i];         s.insert(a[i]);     }     sort(a,a+n);     // 4 9 16 25 36     for(int i=n-1;i>=2;i--)     {         int j=0;         while(j<i)         {             if(s.find(a[i]-a[j])!=s.end()) return "Yes";             auto k = s.find(a[i]-a[j]);             if(*k+a[j]>a[i]) break;             j++;         }     }     return "No"; } int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;     int a[n];     for(int i=0;i<n;i++)     {         cin>>a[i]

Adding One

Task: Find the result after adding 1 to given an array of N non negative elements. Example: Input: 1 3 9 9 9 Output: 1 0 0 0 Here is the Code: Program in C++: #include<iostream>  using namespace std; void addingOne(int *a,int n) {     int sum,carry=1;     for(int i=n-1;i>=0;i--)     {         sum=a[i]+carry;         if(sum>9)         {             a[i]=sum%10;             carry=sum/10;         }         else         {             a[i]=sum;             carry=0;             break;         }             }     if(carry!=0)     {         cout<<carry<<" ";     }     for(int i=0;i<n;i++)     {         cout<<a[i]<<" ";     } } int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;     int a[n];     for(int i=0;i<n;i++)     {         cin>>a[i];     }     addingOne(a,n);     cout<<endl; } return 0; } /* // For updated version  class Solution{ public:     vector<int>

Minimum Platforms

Task: Find the number platforms required to a railway station, by the list of trains arrival and departure times, with those platforms no train need not to be wait. Example: Input: 3 0900 1100 1235 1000 1200 1240 Output: 1 Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; int minimumPlatforms(int *a,int *d,int n) {     multimap<int,int> m;     for(int i=0;i<n;i++)     {         m.insert({a[i],d[i]});     }     int k=0;     for(auto i=m.begin();i!=m.end();i++)     {         a[k]=i->first;         d[k]=i->second;         k++;     }     /*sort(a,a+n);     sort(d,d+n);* Execution Time : 0.11 */     int pf=1,c,j;     for(int i=1;i<n;i++)     {         j=0,c=1;         while(j<i)         {             if(a[j]<=a[i]&&a[i]<=d[j]) c++;             j++;         }         if(pf<c) pf = c;     }     return pf; } int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;