Print an N x M matrix such that each row and column has all the vowels in it
Given two integers N and M, the task is to print an N x M matrix such that each row and column contain all the vowels in it. If it is impossible to do so, then print -1.
Examples:
Input: N = 5, M = 5
Output:
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
Input: N = 6, M = 2
Output: -1
Approach: Since the number of vowels is 5, hence we need a minimum of 5 rows and 5 columns in order to generate a valid matrix. A pattern can be followed by filling “aeiouaeiou..” in the first row, “eiouaeio..” in the second row, and so on and the generated matrix will contain all the vowels in every row and column.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printMatrix( int n, int m)
{
if (n < 5 || m < 5) {
cout << -1;
return ;
}
string s = "aeiou" ;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
cout << s[j % 5] << " " ;
}
cout << endl;
char c = s[0];
for ( int i = 0; i < 4; i++) {
s[i] = s[i + 1];
}
s[4] = c;
}
}
int main()
{
int n = 5, m = 5;
printMatrix(n, m);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static void printMatrix( int n, int m)
{
if (n < 5 || m < 5 )
{
System.out.print(- 1 );
return ;
}
char [] s = "aeiou" .toCharArray();
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
{
System.out.print(s[j % 5 ] + " " );
}
System.out.println();
char c = s[ 0 ];
for ( int k = 0 ; k < 4 ; k++)
{
s[k] = s[k + 1 ];
}
s[ 4 ] = c;
}
}
public static void main(String[] args)
{
int n = 5 , m = 5 ;
printMatrix(n, m);
}
}
|
Python3
def printMatrix(n, m) :
if (n < 5 or m < 5 ) :
print ( - 1 ,end = " " );
return ;
s = "aeiou" ;
s = list (s);
for i in range (n) :
for j in range (m) :
print (s[j % 5 ],end = " " );
print ()
c = s[ 0 ];
for i in range ( 4 ) :
s[i] = s[i + 1 ];
s[ 4 ] = c;
if __name__ = = "__main__" :
n = 5 ; m = 5 ;
printMatrix(n, m);
|
C#
using System;
class GFG
{
static void printMatrix( int n, int m)
{
if (n < 5 || m < 5)
{
Console.Write(-1);
return ;
}
char [] s = "aeiou" .ToCharArray();
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
Console.Write(s[j % 5] + " " );
}
Console.WriteLine();
char c = s[0];
for ( int k = 0; k < 4; k++)
{
s[k] = s[k + 1];
}
s[4] = c;
}
}
public static void Main(String[] args)
{
int n = 5, m = 5;
printMatrix(n, m);
}
}
|
PHP
<?php
function printMatrix( $n , $m )
{
if ( $n < 5 || $m < 5)
{
echo -1;
return ;
}
$s = "aeiou" ;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $m ; $j ++)
{
echo $s [ $j % 5] . " " ;
}
echo "\n" ;
$c = $s [0];
for ( $k = 0; $k < 4; $k ++)
{
$s [ $k ] = $s [ $k + 1];
}
$s [4] = $c ;
}
}
$n = 5;
$m = 5;
printMatrix( $n , $m );
return 0;
?>
|
Javascript
<script>
function printMatrix(n , m) {
if (n < 5 || m < 5) {
document.write(-1);
return ;
}
var s = "aeiou" ;
for ( var i = 0; i < n; i++) {
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;
}
}
var n = 5, m = 5;
printMatrix(n, m);
</script>
|
Output:
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
Time Complexity: O(N*M), as we are using nested loops to traverse N*M times.
Auxiliary Space: O(1) as we are not using any extra space.
Last Updated :
08 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...