Dada una array de números, la tarea es imprimir esos números en orden ascendente separados por comas que tienen 1, 2 y 3 en sus dígitos. Si no hay ningún número que contenga los dígitos 1, 2 y 3, imprima -1. Ejemplos:
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
Preguntado en: Enfoque de Goldman Sachs : primero encuentre todos los números de la array que contiene 1, 2 y 3 , luego ordene el número de acuerdo con 1, 2 y 3 y luego imprímalo.
CPP
// CPP program to print all number containing // 1, 2 and 3 in any order. #include<bits/stdc++.h> using namespace std; // convert the number to string and find // if it contains 1, 2 & 3. 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); } // prints all the number containing 1, 2, 3 string printNumbers(int numbers[], int n) { vector<int> oneTwoThree; for (int i = 0; i < n; i++) { // check if the number contains 1, // 2 & 3 in any order if (findContainsOneTwoThree(numbers[i])) oneTwoThree.push_back(numbers[i]); } // sort all the numbers 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"; } // Driver Code 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; } // This code is contributed // by Sirjan13
Java
// Java program to print all number containing // 1, 2 and 3 in any order. import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; class GFG { // prints all the number containing 1, 2, 3 // in any order private static String printNumbers(int[] numbers) { ArrayList<Integer> array = new ArrayList<>(); for (int number : numbers) { // check if the number contains 1, // 2 & 3 in any order if (findContainsOneTwoThree(number)) array.add(number); } // sort all the numbers 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"; } // convert the number to string and find // if it contains 1, 2 & 3. 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
# Python program for printing # all numbers containing 1,2 and 3 def printNumbers(numbers): # convert all numbers # to strings numbers = map(str, numbers) result = [] for num in numbers: # check if each number # in the list has 1,2 and 3 if ('1' in num and '2' in num and '3' in num): result.append(num) # if there are no # valid numbers if not result: result = ['-1'] return sorted(result); # Driver Code numbers = [123, 1232, 456, 234, 32145] result = printNumbers(numbers) print ', '.join(num for num in result) # This code is contributed # by IshitaTripathi
C#
// C# program to print all number // containing 1, 2 and 3 in any order. using System; using System.Collections.Generic; using System.Text; class GFG { // prints all the number // containing 1, 2, 3 // in any order private static string printNumbers(int[] numbers) { List<int> array = new List<int>(); foreach (int number in numbers) { // check if the number contains 1, // 2 & 3 in any order if (findContainsOneTwoThree(number)) { array.Add(number); } } // sort all the numbers 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"; } // convert the number // to string and find // if it contains 1, 2 & 3. private static bool findContainsOneTwoThree(int number) { string str = Convert.ToString(number); return (str.Contains("1") && str.Contains("2") && str.Contains("3")); } // Driver Code public static void Main(string[] args) { int[] numbers = new int[] {123, 1232, 456, 234, 32145}; Console.WriteLine(printNumbers(numbers)); } } // This code is contributed by Shrikant13
Javascript
<script> // JavaScript program to print all number containing // 1, 2 and 3 in any order. // convert the number to string and find // if it contains 1, 2 & 3. 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); } // prints all the number containing 1, 2, 3 function printNumbers(numbers, n) { let oneTwoThree = []; for (let i = 0; i < n; i++) { // check if the number contains 1, // 2 & 3 in any order if (findContainsOneTwoThree(numbers[i])) oneTwoThree.push(numbers[i]); } // sort all the numbers 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"; } // Driver Code let numbers = [ 123, 1232, 456, 234, 32145 ]; let n = numbers.length; let result = printNumbers(numbers, n); document.write(result); // This code is contributed // by shinjanpatra </script>
123, 1232, 32145
Complejidad de tiempo: la complejidad de tiempo del enfoque anterior es O(n log(n)) .
Espacio Auxiliar: O(n)
Sugiera si alguien tiene una mejor solución que sea más eficiente en términos de espacio y tiempo.
Este artículo es una contribución de Aarti_Rathi . Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por MukeshRanjanKushwaha y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA