Print number in ascending order which contains 1, 2 and 3 in their digits.
Given an array of numbers, the task is to print those numbers in ascending order separated by commas that have 1, 2, and 3 in their digits. If no number containing digits 1, 2, and 3 is present then print -1.
Examples:
Input : numbers[] = {123, 1232, 456, 234, 32145}
Output : 123, 1232, 32145
Input : numbers[] = {9821, 627183, 12, 1234}
Output : 1234, 627183
Input : numbers[] = {12, 232, 456, 234}
Output : -1
First finding all the number in form of array which contains 1, 2 & 3 then sort the number according to 1, 2 and 3 and then print it.
Follow the steps below to implement the above idea:
- Iterate over the given array numbers[] and check if the number is valid or not.
- For checking the validity of a number:
- Take three variables countOnes, countTwo, countThee, and initialize to 0.
- Increment the value for the above variables accordingly, if found.
- Finally, Check if all the above three variables contain some value, then return true.
- Otherwise, return false.
- If the number is valid then push the number into array oneTwoThree[].
- Sort the array and print the result.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
bool findContainsOneTwoThree( int number)
{
string str = to_string(number);
int countOnes = 0, countTwo = 0, countThree = 0;
for ( int i = 0; i < str.length(); i++) {
if (str[i] == '1' )
countOnes++;
else if (str[i] == '2' )
countTwo++;
else if (str[i] == '3' )
countThree++;
}
return (countOnes && countTwo && countThree);
}
string printNumbers( int numbers[], int n)
{
vector< int > oneTwoThree;
for ( int i = 0; i < n; i++) {
if (findContainsOneTwoThree(numbers[i]))
oneTwoThree.push_back(numbers[i]);
}
sort(oneTwoThree.begin(), oneTwoThree.end());
string result = "" ;
for ( auto number : oneTwoThree) {
int value = number;
if (result.length() > 0)
result += ", " ;
result += to_string(value);
}
return (result.length() > 0) ? result : "-1" ;
}
int main()
{
int numbers[] = { 123, 1232, 456, 234, 32145 };
int n = sizeof (numbers) / sizeof (numbers[0]);
string result = printNumbers(numbers, n);
cout << result;
return 0;
}
|
Java
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
class GFG {
private static String printNumbers( int [] numbers)
{
ArrayList<Integer> array = new ArrayList<>();
for ( int number : numbers) {
if (findContainsOneTwoThree(number))
array.add(number);
}
Collections.sort(array);
StringBuffer strbuf = new StringBuffer();
Iterator it = array.iterator();
while (it.hasNext()) {
int value = ( int )it.next();
if (strbuf.length() > 0 )
strbuf.append( ", " );
strbuf.append(Integer.toString(value));
}
return (strbuf.length() > 0 ) ? strbuf.toString()
: "-1" ;
}
private static boolean
findContainsOneTwoThree( int number)
{
String str = Integer.toString(number);
return (str.contains( "1" ) && str.contains( "2" )
&& str.contains( "3" ));
}
public static void main(String[] args)
{
int [] numbers = { 123 , 1232 , 456 , 234 , 32145 };
System.out.println(printNumbers(numbers));
}
}
|
Python
def printNumbers(numbers):
numbers = map ( str , numbers)
result = []
for num in numbers:
if ( '1' in num and
'2' in num and
'3' in num):
result.append(num)
if not result:
result = [ '-1' ]
return sorted (result)
numbers = [ 123 , 1232 , 456 ,
234 , 32145 ]
result = printNumbers(numbers)
print ', ' .join(num for num in result)
|
C#
using System;
using System.Collections.Generic;
using System.Text;
class GFG {
private static string printNumbers( int [] numbers)
{
List< int > array = new List< int >();
foreach ( int number in numbers)
{
if (findContainsOneTwoThree(number)) {
array.Add(number);
}
}
array.Sort();
StringBuilder strbuf = new StringBuilder();
System.Collections.IEnumerator it
= array.GetEnumerator();
while (it.MoveNext()) {
int value = ( int )it.Current;
if (strbuf.Length > 0) {
strbuf.Append( ", " );
}
strbuf.Append(Convert.ToString(value));
}
return (strbuf.Length > 0) ? strbuf.ToString()
: "-1" ;
}
private static bool findContainsOneTwoThree( int number)
{
string str = Convert.ToString(number);
return (str.Contains( "1" ) && str.Contains( "2" )
&& str.Contains( "3" ));
}
public static void Main( string [] args)
{
int [] numbers
= new int [] { 123, 1232, 456, 234, 32145 };
Console.WriteLine(printNumbers(numbers));
}
}
|
Javascript
<script>
function findContainsOneTwoThree(number)
{
let str = number.toString();
let countOnes = 0, countTwo = 0, countThree = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] == '1' ) countOnes++;
else if (str[i] == '2' ) countTwo++;
else if (str[i] == '3' ) countThree++;
}
return (countOnes && countTwo && countThree);
}
function printNumbers(numbers, n)
{
let oneTwoThree = [];
for (let i = 0; i < n; i++)
{
if (findContainsOneTwoThree(numbers[i]))
oneTwoThree.push(numbers[i]);
}
oneTwoThree.sort();
let result = "" ;
for (let number of oneTwoThree)
{
let value = number;
if (result.length > 0)
result += ", " ;
result += value.toString();
}
return (result.length > 0) ? result : "-1" ;
}
let numbers = [ 123, 1232, 456, 234, 32145 ];
let n = numbers.length;
let result = printNumbers(numbers, n);
document.write(result);
</script>
|
Time Complexity: O(N log(N)), where N is the length of the given number.
Auxiliary Space: O(N)
Please suggest if someone has a better solution that is more efficient in terms of space and time.
Last Updated :
09 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...