- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print an array with numbers having 1, 2 and 3 as a digit in ascending order
Here, the task is to print those number in an array having 1, 2 and 3 as digits in their numbers and if their is no such number than the output must be -1
Input : arr[] = {320,123,124,125,14532,126,340,123400,100032,13,32,3123,1100} Output : 123 3123 14532 100032 123400 Since the array have values with digits 1, 2 and 3 it wouldn’t return -1 and print 5 values that Contain 1, 2 and 3 in their respective numbers.
Algorithm
START Step 1 -> Declare array with elements of int type as arr Step 2 -> store size of array in int n Step 3 -> declare int variable as one, two, three Step 4 -> call sort functions with parameters as arr and arr+n Step 5 -> declare variable of type osrtingstream as st and string as num Step 6 -> Loop For i=0 and i<n and ++i Set one=two=three=1 Print arr[i] Set num=st.str() Set one=num.find("1") Set two=num.find("2") Set three=num.find("3") IF((one!=-1)&&(two!=-1)&&(three!=-1)) Print num End Call st.str(‘’”) end STOP
Example
#include <bits/stdc++.h> #include<string.h> #include<sstream> using namespace std; int main() { int arr[] = {320,123,124,125,14532,126,340,123400,100032,13,32,3123,1100}; int n = sizeof(arr)/sizeof(arr[0]); int one,two,three; sort(arr, arr+n); ostringstream st; string num; for (int i = 0; i < n; ++i) { one=two=three=-1; st << arr[i]; num=st.str(); one=num.find("1"); two=num.find("2"); three=num.find("3"); if((one!=-1)&&(two!=-1)&&(three!=-1)) { cout<<num<<" "; } st.str(""); } }
Output
if we run the above program then it will generate the following output
123 3123 14532 100032 123400
- Related Articles
- Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
- Count numbers having 0 as a digit in C++
- Sorting an associative array in ascending order - JavaScript
- C program to sort an array in an ascending order
- Write the following in ascending order:$frac{1}{2}, frac{4}{5}, frac{-2}{3}, frac{-1}{2}, frac{-5}{7}$.
- Write the following rational numbers in ascending order:$(i)$. $frac{-3}{5}, frac{-2}{5}, frac{-1}{5}$$(ii)$. $frac{1}{3}, frac{-2}{9}, frac{-4}{3}$$(iii)$. $frac{-3}{7}, frac{-3}{2}, frac{-3}{4}$
- 8086 program to sort an integer array in ascending order
- Java Program to Sort Array list in an Ascending Order
- Count n digit numbers not having a particular digit in C++
- Print numbers having first and last bits as the only set bits
- How do you sort an array in C# in ascending order?
- Print all 3 digit repeating numbers in a very large number in C++
- Print 2-D co-ordinate points in ascending order followed by their frequencies in C++
- Print prime numbers from 1 to N in reverse order
- C program to sort an array of ten elements in an ascending order

Advertisements