Dados dos números enteros N y M , la tarea es imprimir una array N x M tal que cada fila y columna contenga todas las vocales. Si es imposible hacerlo, imprima -1 .
Ejemplos:
Entrada: N = 5, M = 5
Salida:
aeiou
eioua
iouae
ouaei
uaeio
Entrada: N = 6, M = 2
Salida: -1
Enfoque: dado que el número de vocales es 5, necesitamos un mínimo de 5 filas y 5 columnas para generar una array válida. Se puede seguir un patrón llenando “aeiouaeiou..” en la primera fila, “eiouaeio..” en la segunda fila, y así sucesivamente y la array generada contendrá todas las vocales en cada fila y columna.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to print the required matrix void printMatrix(int n, int m) { // Impossible to generate // the required matrix if (n < 5 || m < 5) { cout << -1; return; } // Store all the vowels string s = "aeiou"; // Print the matrix for (int i = 0; i < n; i++) { // Print vowels for every index for (int j = 0; j < m; j++) { cout << s[j % 5] << " "; } cout << endl; char c = s[0]; // Shift the vowels by one for (int i = 0; i < 4; i++) { s[i] = s[i + 1]; } s[4] = c; } } // Driver code int main() { int n = 5, m = 5; printMatrix(n, m); return 0; }
Java
// Java implementation of the approach class GFG { // Function to print the required matrix static void printMatrix(int n, int m) { // Impossible to generate // the required matrix if (n < 5 || m < 5) { System.out.print(-1); return; } // Store all the vowels char[] s = "aeiou".toCharArray(); // Print the matrix for (int i = 0; i < n; i++) { // Print vowels for every index for (int j = 0; j < m; j++) { System.out.print(s[j % 5] + " "); } System.out.println(); char c = s[0]; // Shift the vowels by one for (int k = 0; k < 4; k++) { s[k] = s[k + 1]; } s[4] = c; } } // Driver code public static void main(String[] args) { int n = 5, m = 5; printMatrix(n, m); } } // This code has been contributed by 29AjayKumar
Python3
# Python3 implementation of the approach # Function to print the required matrix def printMatrix(n, m) : # Impossible to generate # the required matrix if (n < 5 or m < 5) : print(-1,end = " "); return; # Store all the vowels s = "aeiou"; s = list(s); # Print the matrix for i in range(n) : # Print vowels for every index for j in range(m) : print(s[j % 5],end= " "); print() c = s[0]; # Shift the vowels by one for i in range(4) : s[i] = s[i + 1]; s[4] = c; # Driver code if __name__ == "__main__" : n = 5; m = 5; printMatrix(n, m); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function to print the required matrix static void printMatrix(int n, int m) { // Impossible to generate // the required matrix if (n < 5 || m < 5) { Console.Write(-1); return; } // Store all the vowels char[] s = "aeiou".ToCharArray(); // Print the matrix for (int i = 0; i < n; i++) { // Print vowels for every index for (int j = 0; j < m; j++) { Console.Write(s[j % 5] + " "); } Console.WriteLine(); char c = s[0]; // Shift the vowels by one for (int k = 0; k < 4; k++) { s[k] = s[k + 1]; } s[4] = c; } } // Driver code public static void Main(String[] args) { int n = 5, m = 5; printMatrix(n, m); } } /* This code contributed by PrinciRaj1992 */
PHP
<?php // PHP implementation of the approach // Function to print the required matrix function printMatrix($n, $m) { // Impossible to generate // the required matrix if ($n < 5 || $m < 5) { echo -1; return; } // Store all the vowels $s = "aeiou"; // Print the matrix for ($i = 0; $i < $n; $i++) { // Print vowels for every index for ($j = 0; $j < $m; $j++) { echo $s[$j % 5] . " "; } echo "\n"; $c = $s[0]; // Shift the vowels by one for ($k = 0; $k < 4; $k++) { $s[$k] = $s[$k + 1]; } $s[4] = $c; } } // Driver code $n = 5; $m = 5; printMatrix($n, $m); return 0; // This code is contributed by ChitraNayal ?>
Javascript
<script> // JavaScript implementation of the approach // Function to print the required matrix function printMatrix(n , m) { // Impossible to generate // the required matrix if (n < 5 || m < 5) { document.write(-1); return; } // Store all the vowels var s = "aeiou"; // Print the matrix for (var i = 0; i < n; i++) { // Print vowels for every index for (j = 0; j < m; j++) { document.write(s[j % 5] + " "); } document.write("<br/>"); var c = s[0]; s = s.substring(1,s.length)+s.substring(0,1); s[4] = c; } } // Driver code var n = 5, m = 5; printMatrix(n, m); // This code contributed by Rajput-Ji </script>
a e i o u e i o u a i o u a e o u a e i u a e i o
Complejidad de tiempo : O (N * M), ya que estamos usando bucles anidados para atravesar N * M veces.
Espacio auxiliar : O (1) ya que no estamos usando ningún espacio adicional.