Given an integer N. The task is to generate a symmetric matrix of order N*N having the following properties.
- Main diagonal should contain only 0’s
- The matrix should contain elements from 0 to N-1 only.
Examples:
Input: N = 4
Output:
0 2 3 1
2 0 1 3
3 1 0 2
1 3 2 0
Input: N = 5
Output:
0 2 3 4 1
2 0 4 1 3
3 4 0 2 1
4 1 2 0 3
1 3 1 3 0
Approach: Since the required matrix has to be a square matrix, we can generate a symmetric matrix containing an element from 1 to n-1, excluding 0. We will deal with the case of 0 later.
Take for example when N = 4:
We first generate a symmetric matrix, and it can be easily done by filling every row from 1 to n-1 in cyclic order, i.e. fill the first row by 1 2 3, and do this for all subsequent rows in cyclic order.
So, the final matrix will be,
1 2 3
2 3 1
3 1 2
Now, we have generated a symmetric matrix containing elements from 1 to n. Let’s discuss case 0. We will take the benefit of the above matrix being symmetrical, we will add a column of 0 and rows of 0 like this,
1 2 3 0
2 3 1 0
3 1 2 0
0 0 0 0
Now, we have to put all 0 in diagonal. For this, we will start with the first row till last-1 row and swap all the 0 with the number that is there in each row and will also make a change in the last row like this:
For row 1, we swap 0 and 1 and also put last row’s 1st element with the number we swapped i.e. 1.
0 2 3 1
2 3 1 0
3 1 2 0
1 0 0 0
For row 2 we swap 0 and 3, and make the second element of the last row also 3.
0 2 3 1
2 0 1 3
3 1 2 0
1 3 0 0
and so on…
The final matrix generated will be the required matrix.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;
// Function to generate the required matrix void solve( long long n)
{ long long initial_array[n - 1][n - 1], final_array[n][n];
for ( long long i = 0; i < n - 1; ++i)
initial_array[0][i] = i + 1;
// Form cyclic array of elements 1 to n-1
for ( long long i = 1; i < n - 1; ++i)
for ( long long j = 0; j < n - 1; ++j)
initial_array[i][j]
= initial_array[i - 1][(j + 1) % (n - 1)];
// Store initial array into final array
for ( long long i = 0; i < n - 1; ++i)
for ( long long j = 0; j < n - 1; ++j)
final_array[i][j] = initial_array[i][j];
// Fill the last row and column with 0's
for ( long long i = 0; i < n; ++i)
final_array[i][n - 1] = final_array[n - 1][i] = 0;
for ( long long i = 0; i < n; ++i) {
long long t0 = final_array[i][i];
long long t1 = final_array[i][n - 1];
// Swap 0 and the number present
// at the current indexed row
swap(final_array[i][i], final_array[i][n - 1]);
// Also make changes in the last row
// with the number we swapped
final_array[n - 1][i] = t0;
}
// Print the final array
for ( long long i = 0; i < n; ++i) {
for ( long long j = 0; j < n; ++j)
cout << final_array[i][j] << " " ;
cout << endl;
}
} // Driver code int main()
{ long long n = 5;
solve(n);
return 0;
} |
// Java implementation of the approach class GFG
{ // Function to generate the required matrix static void solve( long n)
{ long initial_array[][]= new long [( int )n - 1 ][( int )n - 1 ],
final_array[][]= new long [( int )n][( int )n];
for ( long i = 0 ; i < n - 1 ; ++i)
initial_array[ 0 ][( int )i] = i + 1 ;
// Form cyclic array of elements 1 to n-1
for ( long i = 1 ; i < n - 1 ; ++i)
for ( long j = 0 ; j < n - 1 ; ++j)
initial_array[( int )i][( int )j]
= initial_array[( int )i - 1 ][( int )(( int )j + 1 ) % (( int )n - 1 )];
// Store initial array into final array
for ( long i = 0 ; i < n - 1 ; ++i)
for ( long j = 0 ; j < n - 1 ; ++j)
final_array[( int )i][( int )j] = initial_array[( int )i][( int )j];
// Fill the last row and column with 0's
for ( long i = 0 ; i < n; ++i)
final_array[( int )i][( int )n - 1 ] = final_array[( int )n - 1 ][( int )i] = 0 ;
for ( long i = 0 ; i < n; ++i)
{
long t0 = final_array[( int )i][( int )i];
long t1 = final_array[( int )i][( int )n - 1 ];
// Swap 0 and the number present
// at the current indexed row
long s = final_array[( int )i][( int )i];
final_array[( int )i][( int )i]=final_array[( int )i][( int )n - 1 ];
final_array[( int )i][( int )n - 1 ]=s;
// Also make changes in the last row
// with the number we swapped
final_array[( int )n - 1 ][( int )i] = t0;
}
// Print the final array
for ( long i = 0 ; i < n; ++i)
{
for ( long j = 0 ; j < n; ++j)
System.out.print( final_array[( int )i][( int )j] + " " );
System.out.println();
}
} // Driver code public static void main(String args[])
{ long n = 5 ;
solve(n);
} } // This code is contributed by Arnab Kundu |
# Python 3 implementation of the approach # Function to generate the required matrix def solve(n):
initial_array = [[ 0 for i in range (n - 1 )] for j in range (n - 1 )]
final_array = [[ 0 for i in range (n)] for j in range (n)]
for i in range (n - 1 ):
initial_array[ 0 ][i] = i + 1
# Form cyclic array of elements 1 to n-1
for i in range ( 1 , n - 1 ):
for j in range (n - 1 ):
initial_array[i][j] = initial_array[i - 1 ][(j + 1 ) % (n - 1 )]
# Store initial array into final array
for i in range (n - 1 ):
for j in range (n - 1 ):
final_array[i][j] = initial_array[i][j]
# Fill the last row and column with 0's
for i in range (n):
final_array[i][n - 1 ] = final_array[n - 1 ][i] = 0
for i in range (n):
t0 = final_array[i][i]
t1 = final_array[i][n - 1 ]
# Swap 0 and the number present
# at the current indexed row
temp = final_array[i][i]
final_array[i][i] = final_array[i][n - 1 ]
final_array[i][n - 1 ] = temp
# Also make changes in the last row
# with the number we swapped
final_array[n - 1 ][i] = t0
# Print the final array
for i in range (n):
for j in range (n):
print (final_array[i][j],end = " " )
print ( "\n" ,end = "")
# Driver code if __name__ = = '__main__' :
n = 5
solve(n)
# This code is contributed by # Surendra_Gangwar |
// C# implementation of the approach using System;
class GFG
{ // Function to generate the required matrix static void solve( long n)
{ long [,]initial_array = new long [( int )n - 1,( int )n - 1];
long [,]final_array = new long [( int )n,( int )n];
for ( long i = 0; i < n - 1; ++i)
initial_array[0,( int )i] = i + 1;
// Form cyclic array of elements 1 to n-1
for ( long i = 1; i < n - 1; ++i)
for ( long j = 0; j < n - 1; ++j)
initial_array[( int )i,( int )j]
= initial_array[( int )i - 1,( int )(( int )j + 1) % (( int )n - 1)];
// Store initial array into final array
for ( long i = 0; i < n - 1; ++i)
for ( long j = 0; j < n - 1; ++j)
final_array[( int )i,( int )j] = initial_array[( int )i,( int )j];
// Fill the last row and column with 0's
for ( long i = 0; i < n; ++i)
final_array[( int )i,( int )n - 1] = final_array[( int )n - 1,( int )i] = 0;
for ( long i = 0; i < n; ++i)
{
long t0 = final_array[( int )i, ( int )i];
long t1 = final_array[( int )i, ( int )n - 1];
// Swap 0 and the number present
// at the current indexed row
long s = final_array[( int )i,( int )i];
final_array[( int )i,( int )i] = final_array[( int )i, ( int )n - 1];
final_array[( int )i,( int )n - 1] = s;
// Also make changes in the last row
// with the number we swapped
final_array[( int )n - 1,( int )i] = t0;
}
// Print the final array
for ( long i = 0; i < n; ++i)
{
for ( long j = 0; j < n; ++j)
Console.Write( final_array[( int )i,( int )j] + " " );
Console.WriteLine();
}
} // Driver code public static void Main(String []args)
{ long n = 5;
solve(n);
} } // This code contributed by Rajput-Ji |
<?php // Php implementation of the approach // Function to generate the required matrix function solve( $n )
{ $initial_array = array ( array ()) ;
$final_array = array ( array ()) ;
for ( $i = 0; $i < $n - 1; ++ $i )
$initial_array [0][ $i ] = $i + 1;
// Form cyclic array of elements 1 to n-1
for ( $i = 1; $i < $n - 1; ++ $i )
for ( $j = 0; $j < $n - 1; ++ $j )
$initial_array [ $i ][ $j ] =
$initial_array [ $i - 1][( $j + 1) % ( $n - 1)];
// Store initial array into final array
for ( $i = 0; $i < $n - 1; ++ $i )
for ( $j = 0; $j < $n - 1; ++ $j )
$final_array [ $i ][ $j ] = $initial_array [ $i ][ $j ];
// Fill the last row and column with 0's
for ( $i = 0; $i < $n ; ++ $i )
$final_array [ $i ][ $n - 1] = $final_array [ $n - 1][ $i ] = 0;
for ( $i = 0; $i < $n ; ++ $i )
{
$t0 = $final_array [ $i ][ $i ];
$t1 = $final_array [ $i ][ $n - 1];
// Swap 0 and the number present
// at the current indexed row
$temp = $final_array [ $i ][ $i ] ;
$final_array [ $i ][ $i ] = $final_array [ $i ][ $n - 1] ;
$final_array [ $i ][ $n - 1] = $temp ;
// Also make changes in the last row
// with the number we swapped
$final_array [ $n - 1][ $i ] = $t0 ;
}
// Print the final array
for ( $i = 0; $i < $n ; ++ $i )
{
for ( $j = 0; $j < $n ; ++ $j )
echo $final_array [ $i ][ $j ], " " ;
echo "\n" ;
}
} // Driver code
$n = 5;
solve( $n );
// This code is contributed by Ryuga ?> |
<script> // Javascript implementation of the approach // Function to generate the required matrix function solve(n)
{ let initial_array = new Array(n-1);
for ( var i = 0; i < initial_array.length; i++) {
initial_array[i] = new Array(2);
}
let final_array = new Array(n);
for ( var i = 0; i < final_array.length; i++) {
final_array[i] = new Array(2);
}
for (let i = 0; i < n - 1; ++i)
initial_array[0][i] = i + 1;
// Form cyclic array of elements 1 to n-1
for (let i = 1; i < n - 1; ++i)
for (let j = 0; j < n - 1; ++j)
initial_array[i][j]
= initial_array[i - 1][(j + 1) % (n - 1)];
// Store initial array into final array
for (let i = 0; i < n - 1; ++i)
for (let j = 0; j < n - 1; ++j)
final_array[i][j] = initial_array[i][j];
// Fill the last row and column with 0's
for (let i = 0; i < n; ++i)
final_array[i][n - 1] = final_array[n - 1][i] = 0;
for (let i = 0; i < n; ++i)
{
let t0 = final_array[i][i];
let t1 = final_array[i][n - 1];
// Swap 0 and the number present
// at the current indexed row
let s = final_array[i][i];
final_array[i][i]=final_array[i][n - 1];
final_array[i][n - 1]=s;
// Also make changes in the last row
// with the number we swapped
final_array[n - 1][i] = t0;
}
// Print the final array
for (let i = 0; i < n; ++i)
{
for (let j = 0; j < n; ++j)
document.write( final_array[i][j] + " " );
document.write( "<br/>" );
}
} // Driver Code let n = 5;
solve(n);
// This code is contributed by target_2.
</script> |
0 2 3 4 1 2 0 4 1 3 3 4 0 2 1 4 1 2 0 3 1 3 1 3 0
Time Complexity: O(N2)
Auxiliary Space: O(N2)