Skip to main content

Posts

Showing posts with the label Program

Triplet Sum in Array

Task: Find the triplet sum in given an array of N numbers with value x, Triplet sum means find three elements in an array which sum is exactly equal to x. If it present print 1 else print 0. Example: Input: 1 5 10 1 5 3 7 6 Output: 1 // 1 + 3 + 6 = 10. Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; int tripletSum(int *a,int n,int x) {     sort(a,a+n);     //1 2 3 4 6     for(int i=0;i<=n-3;i++)     {         int j=i+1,k=n-1;         while(j<k)         {             if(a[i]+a[j]+a[k]==x) return 1;             else if(a[i]+a[j]+a[k]<x) j++;             else if(a[i]+a[j]+a[k]>x) k--;         }     }     return 0; } int main()  { int t; cin>>t; while(t--) {     int n,x;     cin>>n>>x;     int a[n];     for(int i=0;i<n;i++)     {         cin>>a[i];     }     cout<<tripletSum(a,n,x)<<endl; } return 0; } Here is the Video:

Evaluation of Postfix

Task: Find the final value of given postfix expression. Example: Input: 1 123+*8- Output: -3 Here is the Code: Program in C++: #include <iostream> #include <bits/stdc++.h> using namespace std; int evalutionOfPostfix(string s) {     stack<int> st;     int a,b,c;     for(int i=0;i<s.length();i++)     {         if(isdigit(s[i]))         {             st.push((s[i] - '0'));         }         else         {             b=st.top();             st.pop();             a=st.top();             st.pop();             if(s[i]=='*')             {                 c=a*b;             }             else if(s[i]=='/')             {                 c=a/b;             }             else if(s[i]=='+')             {                 c=a+b;             }             else             {                 c=a-b;             }             st.push(c);         }     }     cout<<st.top()<<endl;     st.pop(); } int main()  { int t; cin>>t; while

Missing Number

Task: Find the missing number from given an array of N numbers from 1 to N. Example: Input: 1 3 1 4 3 Output: 2 Here is the Code: Program in C++: #include <iostream> using namespace std; int main() { //number of test cases int t; scanf("%d\n",&t); while(t--) {     int n;     scanf("%d\n",&n);     int a[n-1],sum=0;     for(int i=0;i<n-1;i++)     {         scanf("%d\n",&a[i]);         sum+=a[i];     }     int sn=(n*(n+1))/2;     cout<<sn-sum<<endl; } return 0; } Here is the Video:

First Repeating Element

Task: Find the first repeating element in given an array of N elements. If it is present then print that index else print -1. Example: Input: 1 1 2 2 3 1 Output: 1  Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;     int a[n];     unordered_map<int, int> m;     for(int i=0;i<n;i++)     {         cin>>a[i];         m[a[i]]++;     }     int c=0;     for(int i=0;i<n;i++)     {         if(m[a[i]]>1)         {             cout<<i+1<<endl;             c=1;             break;         }     }     if(c==0) cout<<-1<<endl; } return 0; } Here is the Video:

Smallest Distinct Window

Task: Find the length of smallest distinct window of given string, which is having every letter in the string at least one time. Example: Input: 1 aab Output: 2 Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; int main()  { int t; cin>>t; while(t--) {     string s;     cin>>s;     int vis[128];     memset(vis, 0, sizeof(vis));     string t,sdw;     // Implementation of Logic     for(int i=0;i<s.length();i++)     {         vis[s[i]]++;         t=t+s[i];         if(vis[s[i]]==1)         {             sdw=t;         }         char c;         while(1)         {             c=t[0];             vis[c]--;             if(vis[c]!=0)             {                 t.erase(t.begin()+0);             }             else             {                 vis[c]++;                 break;             }         }         if(t.length()<sdw.length())         {  

Pattern Searching

Task :  Find the pattern from the text. If the pattern is present print found else print not found. Example: Input:  2 hellofriends hello asdfghjkl ask Output: found not found Here is the Code : Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; int main()  { int t; cin>>t; while(t--) {     string s1,s2;     cin>>s1;     cin>>s2;     int n=s1.length(),m=s2.length();     // Implementation of logic     if(n<m)     {         cout<<"not found"<<endl;     }     else     {         int c = 0;         for(int i=0;i<=n-m;i++)         {             string t = s1.substr(i,m);             if(t == s2)             {                 cout<<"found"<<endl;                 c=1;                 break;             }         }         if(c==0) cout<<"not found"<<endl;     } } return 0; } Here is the Video:

Rotating an Array

Task: Rotate an array of size N by d elements, where d <= N. Example: Input: 1 5  1 2 3 4 5 Output: 3 4 5 1 2 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];     for(int i=0;i<n;i++)     {         cin>>a[i];     }     int d;     cin>>d;     // code logic     for(int i=0;i<d;i++)     {         int j=0,temp=a[0];         while(j<n-1)         {             a[j]=a[j+1];             j++;         }         a[j]=temp;     }     for(int i=0;i<n;i++)     {         cout<<a[i]<<" ";     }     cout<<endl; } return 0; } Here is the Video:

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;

Longest Palindrome in a String

Task: Find the longest palindromic sub string in given string.  Example: Input: 1 aabba Output: abba Here is the Code: Program in C++: #include<iostream> using namespace std; string longestPalindrome(string s) {     string str="",rstr="",lps="";     int p=0,q=0;     for(int i=0;i<s.length();i++)     {         str=str+s[i];         rstr=s[i]+rstr;         if(str==rstr)         {             if(lps.length()<str.length())             {                 lps=str;             }         }         else         {             if(p!=0&&s[p-1]==s[i])             {                 str=s[p-1]+str;                 rstr=rstr+s[p-1];                 if(lps.length()<str.length())                 {                     lps=str;                 }                 p--;             }             else             {                 i=++q;                 str.clear();                 rstr.clear();                 str=str+s[i];                 rstr=s[i]+rstr;  

First Non Repeating Character in a Stream

Task: Find the first non repeating character in a stream, The steam is inserted one character at a time from given string. Print first non repeating character in every inserting time if no such character exist print -1. Example: Input: 1 3 a a c Output: a -1 c Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; map<char,int> m; queue<char> q; void fnrcInStream(char *a,int n) {     for(int i=0;i<n;i++)     {         if(m.find(a[i])==m.end())         {             q.push(a[i]);         }         m[a[i]]++;         if(m[q.front()]==1)         {             cout<<q.front()<<" ";         }         else         {             while(!q.empty())             {                 q.pop();                 if(m[q.front()]==1)                 {                     cout<<q.front()<<" ";                     break;                 }             }             if(q.empty())             {    

Parenthesis Checker

Task:  Find the given Parenthesis string is balanced on not. Example: Input: 1 {[()]} Output: balanced Here is the Code: Program in C++: #include <iostream> #include <bits/stdc++.h> using namespace std; string parenthesChecker(string s) {     stack<char> st;     if(s[0]==')'||s[0]==']'||s[0]=='}')     {         return "not balanced";     }     for(int i=0;i<s.length();i++)     {         if(s[i]=='('||s[i]=='['||s[i]=='{')         {             st.push(s[i]);         }         else         {             if(st.empty()==false)             {                if((s[i]==')'&&st.top()=='(')||(s[i]==']'&&st.top()=='[')||(s[i]=='}'&&st.top()=='{'))                 {                     st.pop();                 }                 else                 {                     break;                 }             }         }     }     if(st.empty())     

Jumping Numbers

Task: Find the all jumping numbers smaller than or equal to given X, Jumping number means a number all adjacent digits differ by 1. Example: Input: 1 50 Output: 0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; void jumpingNumbers(int *jd,int n) {     queue<int> q;     jd[0]=0;     int i;     for(i=1;i<10;i++)     {         jd[i]=i;         q.push(i);     }     int num;     while(q.empty()==false&&i<=n)     {         num=q.front();         int t1=num%10;         if(t1==0)         {             jd[i++]=num*10+t1+1;             q.push(jd[i-1]);         }         else if(t1==9)         {             jd[i++]=num*10+t1-1;             q.push(jd[i-1]);         }         else         {             jd[i++]=num*10+t1-1;             q.push(jd[i-1]);             jd[i++]=num*10+t1+1;             q.push(jd[i-1]);         }         q.pop();     } } int main()  { int t; cin&g

Flood Fill Algorithm

Task: Given a 2D screen, location of a pixel in the screen ie(x, y) and a color K, Replace color of the given pixel and all adjacent ( excluding diagonally adjacent ) same colored pixels with the given color K. Example: Input: 1 2 2 1 1 1 1 0 1 8 Output: 8 8 8 8 Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; int main()  { int t; cin>>t; while(t--) {     int n,m;     cin>>n>>m;     int a[n][m];     for(int i=0;i<n;i++)     {         for(int j=0;j<m;j++)         {             cin>>a[i][j];         }     }     int x,y,k;     cin>>x>>y>>k;     queue<pair<int,int>> q;     q.push(make_pair(x,y));     int t1 = a[x][y];     a[x][y]=k;     while(q.empty()==false)     {         pair<int,int> t2 = q.front();         int i = t2.first, j = t2.second;         if(a[i-1][j]==t1&&i-1>=0)         {             q.

Save Iron Man

Task: Jarvis is week in computing palindromes for Alphanumeric characters. While Ironman is busy fighting Thanos, he needs to activate sonic punch but Jarvis is stuck in computing palindromes. Given a string S containing alphanumeric characters. Find out whether the string is a palindrome or not. If string is palindrome print "YES" else print "NO". Iron Man life is in your hands now. Example: Input: 1 Ab?/Ba Output: YES Here is the Code: Program in C++: #include<iostream> using namespace std; string saveIronMan(string s) {     int n = s.length();     for(int i=0;i<n;i++)     {         if(isalpha(s[i])) s[i]=toupper(s[i]);     }     int i=0,j=n-1;     while(i<j)     {         while(!isalpha(s[i])&&!isdigit(s[i]))         {             if(i<n) i++;         }         while(!isalpha(s[j])&&!isdigit(s[j]))         {             if(j>-1) j--;         }         if(s[i]!=s[j])         {             return "NO";         }         i

Stock Buy And Sell

Task: The price of stock in every day given in an array of size N, Find which day to buy and sell on stock to get the maximum profit. Example: Input: 2 5 100 180 260 310 40 3 100 50 30 Output: (0 3) No Profit // If no profit print "No Profit". Here is the Code: Program in C++: #include<iostream> using namespace std; void stockBuyAndSell(int *a,int n) {     int c=0;     for(int i=1;i<n;i++)     {         if(a[i-1]<a[i])         {             cout<<"("<<i-1<<" ";             i++;             while(i<n)             {                 if(a[i-1]<a[i])                 {                     i++;                 }                 else break;             }             cout<<i-1<<")"<<" ";             c++;         }     }     if(c==0) cout<<"No Profit"; } int main()  { int t; cin>>t; while(t--) {     int n;     cin>>n;     int a[n];     for(int i=0;i&

Longest Increasing Subsequence

Task: Given a sequence of an array of size N, find longest increasing sub-sequence from given sequence. The sub-sequence is need not to be continuous. Example: Input: 1 6 5 8 3 7 9 1 Output: 3 // 5 7 9 Here is the Code: Program in C++: #include<iostream> using namespace std; int longestIncreasingSubsequence(int *a,int n) {     int dp[n],lis=1;     dp[0]=1;     for(int i=1;i<n;i++)     {         int j=0,t=1;         while(j<i)         {             if(a[i]>a[j]&&t<=dp[j])             {                 t++;             }             j++;         }         dp[i]=t;         if(lis<dp[i]) lis=dp[i];     }     return lis; } 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];     }     cout<<longestIncreasingSubsequence(a,n)<<endl; } return 0; } Here is the Video: