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 are 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 */

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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    
?>

chevron_right


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


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.