Given an integer N, The task is to construct a matrix mat[][] of size M x M (‘M’ is the number of digits in the given integer) such that each diagonal of the matrix contains the same digit, placed according to the position of the digits in the given integer and then again repeat the steps from the back.

Examples:
Input: N = 123
Output: {{1, 2, 3},
{2, 3, 2},
{3, 2, 1}}
Explanation: The desired matrix must be of size 3*3. The digits of N are 1, 2, and 3. Placing 1, 2 and 3 along the diagonals from the top left cell till the Nth diagonal, and 2, 1 just after the Nth diagonal till the bottom-most cell.
Input: N = 3219
Output: {{3, 2, 1, 9}, {2, 1, 9, 1}, {1, 9, 1, 2}, {9, 1, 2, 3}}
Approach: The task can be solved by traversing the matrix in a diagonal fashion and assigning the cell values according to the corresponding digit in the given number.
- Extract and store the digits of the given integer in a vector say v.
- Again store the digits in reverse order for 2nd half diagonal of the matrix.
- Assign the digits in the desired order.
- Print the matrix.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void constructMatrix( int n)
{
vector< int > v;
while (n > 0) {
v.push_back(n % 10);
n = n / 10;
}
reverse(v.begin(), v.end());
int N = v.size();
for ( int i = N - 2; i >= 0; i--) {
v.push_back(v[i]);
}
int mat[N][N];
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
mat[i][j] = v[i + j];
cout << mat[i][j] << " " ;
}
cout << endl;
}
}
int main()
{
int n = 3219;
constructMatrix(n);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
class GFG {
public static void constructMatrix( int n)
{
ArrayList<Integer> v = new ArrayList<Integer>();
while (n > 0 ) {
v.add(n % 10 );
n = n / 10 ;
}
Collections.reverse(v);
int N = v.size();
for ( int i = N - 2 ; i >= 0 ; i--) {
v.add(v.get(i));
}
int [][] mat = new int [N][N];
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < N; j++) {
mat[i][j] = v.get(i + j);
System.out.print(mat[i][j] + " " );
}
System.out.println( "" );
}
}
public static void main(String args[]) {
int n = 3219 ;
constructMatrix(n);
}
}
|
Python3
def constructMatrix(n):
v = []
while (n > 0 ):
v.append(n % 10 )
n = n / / 10
v.reverse()
N = len (v)
for i in range (N - 2 , - 1 , - 1 ):
v.append(v[i])
mat = [[ 0 for _ in range (N)] for _ in range (N)]
for i in range ( 0 , N):
for j in range ( 0 , N):
mat[i][j] = v[i + j]
print (mat[i][j], end = " " )
print ()
if __name__ = = "__main__" :
n = 3219
constructMatrix(n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void constructMatrix( int n)
{
List< int > v = new List< int >();
while (n > 0) {
v.Add(n % 10);
n = n / 10;
}
v.Reverse();
int N = v.Count;
for ( int i = N - 2; i >= 0; i--) {
v.Add(v[i]);
}
int [,] mat = new int [N, N];
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
mat[i, j] = v[i + j];
Console.Write(mat[i, j] + " " );
}
Console.WriteLine();
}
}
public static void Main()
{
int n = 3219;
constructMatrix(n);
}
}
|
Javascript
<script>
function constructMatrix(n)
{
let v = [];
while (n > 0) {
v.push(n % 10);
n = Math.floor(n / 10);
}
v.reverse();
let N = v.length;
for (let i = N - 2; i >= 0; i--) {
v.push(v[i]);
}
let mat = new Array(N);
for (let i = 0; i < mat.length; i++) {
mat[i] = new Array(N).fill(0);
}
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
mat[i][j] = v[i + j];
document.write(mat[i][j] + " " );
}
document.write( "<br>" )
}
}
let n = 3219;
constructMatrix(n);
</script>
|
Output: 3 2 1 9
2 1 9 1
1 9 1 2
9 1 2 3
Time Complexity: O(N2)
Auxiliary Space: O(1)