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;
- }
- return 0;
- }
Here is the Video:
Comments
Post a Comment