Write a code which inputs two numbers m and n and creates a matrix of size m x n (m rows and n columns) in which every elements is either X or 0. The Xs and 0s must be filled alternatively, the matrix should have outermost rectangle of Xs, then a rectangle of 0s, then a rectangle of Xs, and so on.
Examples:
Input: m = 3, n = 3
Output: Following matrix
X X X
X 0 X
X X X
Input: m = 4, n = 5
Output: Following matrix
X X X X X
X 0 0 0 X
X 0 0 0 X
X X X X X
Input: m = 5, n = 5
Output: Following matrix
X X X X X
X 0 0 0 X
X 0 X 0 X
X 0 0 0 X
X X X X X
Input: m = 6, n = 7
Output: Following matrix
X X X X X X X
X 0 0 0 0 0 X
X 0 X X X 0 X
X 0 X X X 0 X
X 0 0 0 0 0 X
X X X X X X X
We strongly recommend to minimize the browser and try this yourself first.
This question was asked in campus recruitment of Shreepartners Gurgaon. I followed the following approach.
- Use the code for Printing Matrix in Spiral form.
- Instead of printing the array, inserted the element ‘X’ or ‘0’ alternatively in the array.
Algorithm:
- Start with initializing variables i, k, and l to 0 and x to ‘X’.
- Create a 2D character array a with m rows and n columns.
- Store the values of m and n in variables r and c respectively for later use.
- Use a while loop to fill the array a with alternating rectangles of ‘0’ and ‘X’ characters in a spiral pattern.
- In the while loop, fill the first row of the remaining rows with ‘X’ character, starting from column l to n-1, and then increment k by 1.
- Fill the last column of the remaining columns with ‘X’ character, starting from row k to m-1, and then decrement n by 1.
- Fill the last row of the remaining rows with ‘X’ character, starting from column n-1 to l, and then decrement m by 1.
- Fill the first column of the remaining columns with ‘X’ character, starting from row m-1 to k, and then increment l by 1.
- After each iteration of the while loop, toggle the character x between ‘0’ and ‘X’.
- After the while loop is completed, print the filled matrix a row by row.
Following is implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void fill0X( int m, int n)
{
int i, k = 0, l = 0;
int r = m, c = n;
char a[m][n];
char x = 'X' ;
while (k < m && l < n)
{
for (i = l; i < n; ++i)
a[k][i] = x;
k++;
for (i = k; i < m; ++i)
a[i][n-1] = x;
n--;
if (k < m)
{
for (i = n-1; i >= l; --i)
a[m-1][i] = x;
m--;
}
if (l < n)
{
for (i = m-1; i >= k; --i)
a[i][l] = x;
l++;
}
x = (x == '0' )? 'X' : '0' ;
}
for (i = 0; i < r; i++)
{
for ( int j = 0; j < c; j++)
cout << " " << a[i][j];
cout << "\n" ;
}
}
int main()
{
puts ( "Output for m = 5, n = 6" );
fill0X(5, 6);
puts ( "\nOutput for m = 4, n = 4" );
fill0X(4, 4);
puts ( "\nOutput for m = 3, n = 4" );
fill0X(3, 4);
return 0;
}
|
C
#include <stdio.h>
void fill0X( int m, int n)
{
int i, k = 0, l = 0;
int r = m, c = n;
char a[m][n];
char x = 'X' ;
while (k < m && l < n)
{
for (i = l; i < n; ++i)
a[k][i] = x;
k++;
for (i = k; i < m; ++i)
a[i][n-1] = x;
n--;
if (k < m)
{
for (i = n-1; i >= l; --i)
a[m-1][i] = x;
m--;
}
if (l < n)
{
for (i = m-1; i >= k; --i)
a[i][l] = x;
l++;
}
x = (x == '0' )? 'X' : '0' ;
}
for (i = 0; i < r; i++)
{
for ( int j = 0; j < c; j++)
printf ( "%c " , a[i][j]);
printf ( "\n" );
}
}
int main()
{
puts ( "Output for m = 5, n = 6" );
fill0X(5, 6);
puts ( "\nOutput for m = 4, n = 4" );
fill0X(4, 4);
puts ( "\nOutput for m = 3, n = 4" );
fill0X(3, 4);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void fill0X( int m, int n)
{
int i, k = 0 , l = 0 ;
int r = m, c = n;
char a[][] = new char [m][n];
char x = 'X' ;
while (k < m && l < n)
{
for (i = l; i < n; ++i)
a[k][i] = x;
k++;
for (i = k; i < m; ++i)
a[i][n- 1 ] = x;
n--;
if (k < m)
{
for (i = n- 1 ; i >= l; --i)
a[m- 1 ][i] = x;
m--;
}
if (l < n)
{
for (i = m- 1 ; i >= k; --i)
a[i][l] = x;
l++;
}
x = (x == '0' )? 'X' : '0' ;
}
for (i = 0 ; i < r; i++)
{
for ( int j = 0 ; j < c; j++)
System.out.print(a[i][j] + " " );
System.out.println();
}
}
public static void main (String[] args) {
System.out.println( "Output for m = 5, n = 6" );
fill0X( 5 , 6 );
System.out.println( "Output for m = 4, n = 4" );
fill0X( 4 , 4 );
System.out.println( "Output for m = 3, n = 4" );
fill0X( 3 , 4 );
}
}
|
Python3
def fill0X(m, n):
i, k, l = 0 , 0 , 0
r = m
c = n
a = [[ None ] * n for i in range (m)]
x = 'X'
while k < m and l < n:
for i in range (l, n):
a[k][i] = x
k + = 1
for i in range (k, m):
a[i][n - 1 ] = x
n - = 1
if k < m:
for i in range (n - 1 , l - 1 , - 1 ):
a[m - 1 ][i] = x
m - = 1
if l < n:
for i in range (m - 1 , k - 1 , - 1 ):
a[i][l] = x
l + = 1
x = 'X' if x = = '0' else '0'
for i in range (r):
for j in range (c):
print (a[i][j], end = " " )
print ()
if __name__ = = '__main__' :
print ( "Output for m = 5, n = 6" )
fill0X( 5 , 6 )
print ( "Output for m = 4, n = 4" )
fill0X( 4 , 4 )
print ( "Output for m = 3, n = 4" )
fill0X( 3 , 4 )
|
C#
using System;
class GFG {
static void fill0X( int m, int n)
{
int i, k = 0, l = 0;
int r = m, c = n;
char [,]a = new char [m,n];
char x = 'X' ;
while (k < m && l < n)
{
for (i = l; i < n; ++i)
a[k,i] = x;
k++;
for (i = k; i < m; ++i)
a[i,n-1] = x;
n--;
if (k < m)
{
for (i = n-1; i >= l; --i)
a[m-1,i] = x;
m--;
}
if (l < n)
{
for (i = m-1; i >= k; --i)
a[i,l] = x;
l++;
}
x = (x == '0' )? 'X' : '0' ;
}
for (i = 0; i < r; i++)
{
for ( int j = 0; j < c; j++)
Console.Write(a[i,j] + " " );
Console.WriteLine();
}
}
public static void Main ()
{
Console.WriteLine( "Output for"
+ " m = 5, n = 6" );
fill0X(5, 6);
Console.WriteLine( "Output for"
+ " m = 4, n = 4" );
fill0X(4, 4);
Console.WriteLine( "Output for"
+ " m = 3, n = 4" );
fill0X(3, 4);
}
}
|
PHP
<?php
function fill0X( $m , $n )
{
$k = 0;
$l = 0;
$r = $m ;
$c = $n ;
$x = 'X' ;
while ( $k < $m && $l < $n )
{
for ( $i = $l ; $i < $n ; ++ $i )
$a [ $k ][ $i ] = $x ;
$k ++;
for ( $i = $k ; $i < $m ; ++ $i )
$a [ $i ][ $n - 1] = $x ;
$n --;
if ( $k < $m )
{
for ( $i = $n - 1; $i >= $l ; -- $i )
$a [ $m - 1][ $i ] = $x ;
$m --;
}
if ( $l < $n )
{
for ( $i = $m - 1; $i >= $k ; -- $i )
$a [ $i ][ $l ] = $x ;
$l ++;
}
$x = ( $x == '0' )? 'X' : '0' ;
}
for ( $i = 0; $i < $r ; $i ++)
{
for ( $j = 0; $j < $c ; $j ++)
echo ( $a [ $i ][ $j ]. " " );
echo "\n" ;
}
}
echo "Output for m = 5, n = 6\n" ;
fill0X(5, 6);
echo "\nOutput for m = 4, n = 4\n" ;
fill0X(4, 4);
echo "\nOutput for m = 3, n = 4\n" ;
fill0X(3, 4);
?>
|
Javascript
<script>
function fill0X(m, n)
{
let i, k = 0, l = 0;
let r = m, c = n;
let a = new Array(m);
for (let i = 0; i < m; i++)
{
a[i] = new Array(n);
}
let x = 'X' ;
while (k < m && l < n)
{
for (i = l; i < n; ++i)
{
a[k][i] = x;
}
k++;
for (i = k; i < m; ++i)
{
a[i][n - 1] = x;
}
n--;
if (k < m)
{
for (i = n - 1; i >= l; --i)
a[m - 1][i] = x;
m--;
}
if (l < n)
{
for (i = m - 1; i >= k; --i)
{
a[i][l] = x;
}
l++;
}
x = (x == '0' ) ? 'X' : '0' ;
}
for (i = 0; i < r; i++)
{
for (let j = 0; j < c; j++)
{
document.write(a[i][j] + " " );
}
document.write( "<br>" );
}
}
document.write( "Output for m = 5, n = 6 <br>" );
fill0X(5, 6);
document.write( "Output for m = 4, n = 4<br>" );
fill0X(4, 4);
document.write( "Output for m = 3, n = 4<br>" );
fill0X(3, 4);
</script>
|
OutputOutput for m = 5, n = 6
X X X X X X
X 0 0 0 0 X
X 0 X X 0 X
X 0 0 0 0 X
X X X X X X
Output for m = 4, n = 4
X X X X
X 0 0 X
X 0 0 X
X X X X
Output for m = 3, n = 4
X X X X
X 0 0 X
X X X X
Time Complexity: O(mn)
Auxiliary Space: O(mn)