Skip to main content

Posts

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