Skip to main content

Posts

Showing posts from June, 2020

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:

Inversion of Array

Task: Given an array of size N, find the inversion count of an array. Example: Input: 1 5 2 4 1 3 5 Output: 3 Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; long long int c; void count(long long int *arr,int l,int m,int r) {      int n1=m-l+1;      int n2=r-m;      long long int a[n1],b[n2];      for(int i=0;i<n1;i++)      {          a[i]=arr[l+i];      }      for(int j=0;j<n2;j++)      {          b[j]=arr[m+1+j];      }      int i=0,j=0,k=l;      while(i<n1&&j<n2)      {          if(a[i]<=b[j])          {              arr[k]=a[i];              i++;          }          else          {              arr[k]=b[j];              j++;              c+=n1-i;          }          k++;      }      while(i<n1)      {          arr[k]=a[i];          k++,i++;      }      while(j<n2)      {          arr[k]=b[j];          k++,j++;      } } void inversionOfArray(long long int *a,int i,int j) {     if(i==j)     {