Skip to main content

Posts

Numbers containing 1,2 and 3

Task: Given an array of N numbers. Print only those numbers, which are consist of 1, 2 and 3. Example: Input: 1 4 1 2 3 4 Output: 1 2 3 Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; bool isValid(int n) {     int a;     if(n==0) return 0;     while(n!=0)     {         a=n%10;         if(a==1||a==2||a==3) n/=10;         else return 0;     }     return 1; } int numbers(int *a,int n) {     map<int,int> m;     for(int i=0;i<n;i++)     {         if(isValid(a[i]))         {             m[a[i]]++;         }     }     int c=0;     for(auto i=m.begin();i!=m.end();i++)     {         while(i->second--) cout<<i->first<<" ";         c=1;     }     if(c==0) cout<<-1; } 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];     }     numbers(a,n);     cout<<endl; } re

Longest Distinct Characters in String

Task: Given a string S, Find the longest sub string with all distinct characters. Example: Input: 1 abca Output: abc Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; int longestDistinctCharacters(string s) {     map<char,int> m;     int c=0,l=0;     for(int i=0;i<s.length();i++)     {         if(m.find(s[i])==m.end())         {             m[s[i]]=i;             c++;         }         else         {             if(l<c)             {                 l=c;             }             i=m[s[i]];             c=0;             m.clear();         }     }     if(l<c)     {         l=c;     }     return l; } int main()  { int t; cin>>t; while(t--) {     string s;     cin>>s;     cin.ignore();     cout<<longestDistinctCharacters(s)<<endl; } return 0; } Here is the Video:

Print Diagonally

Task: Given a N*N matrix, print the values of matrix in anti diagonal way. Example: Input: 1 1 2 3 4 5 6  7 8 9 Output: 1 2 4 5 6 7 6 8 9 // 1-2,4-3,5,7-6,8-9. 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 i,k;         for(int j=0;j<n;j++)         {             i=0,k=j;             while(k>=0)             {                 cout<<a[i++][k--]<<" ";             }         }         int j;         for(i=1;i<n;i++)         {             j=n-1,k=i;             while(k<n)             {                 cout<<a[k++][j--]<<" ";             }         }         cout<<endl; } return 0; } Here is the Video:

First Repeated Character

Task: Given a string S. Find the first repeated character in it. Example: Input: 1 geeksforgeeks Output: e // first repeated character and second time occurred character index is smallest. 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;     map<char,int> m;     int c=0;     for(int i=0;i<s.length();i++)     {         m[s[i]]++;         if(m[s[i]]==2)         {             cout<<s[i]<<endl;             c=1;             break;         }     }     if(c==0)     {         cout<<-1<<endl;     } } return 0; } Here is the Video:

Reverse Array in Groups

Task: Given an array arr[] of size N. Reverse every sub array of K elements. Example: Input: 1 5 3 1 2 3 4 5 Output: 3 2 1 5 4 Here is the Code: Program in C++: #include<iostream> #include<bits/stdc++.h> using namespace std; void reverseArrayInGroups(long long int *a,int n,int k) {     int t=(n+k-1)/k;     int i=0,j;     stack<long long int> st;     while(t--)     {         j=k;         int l=i;         while(j!=0&&i<n)         {             st.push(a[i]);             i++;             j--;         }         while(!st.empty())         {             a[l]=st.top();             l++;             st.pop();         }     }     for(i=0;i<n;i++)     {         cout<<a[i]<<" ";     }     return; } int main()  { int t; cin>>t; while(t--) {     int n,k;     cin>>n>>k;     long long int a[n];     for(int i=0;i<n;i++)     {         cin>>a[i];     }     reverseArrayInGroups(a,n,k);     cout<&l