Diamond shape in odd square matrix
The problem you're addressing involves printing a diamond shape pattern within an oddsized square matrix. The diamond pattern starts from the center of the matrix and extends outwards, forming a symmetrical pattern.
Problem Statement and Example
Given an oddsized square matrix, your task is to print a diamond shape pattern within it. For example, consider the following 7x7 matrix:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
The output pattern should be a diamond shape formed using the numbers from the matrix.
Idea to Solve
To solve this problem, you need to iterate through the matrix and print the elements in a way that forms the diamond pattern. You can divide the process into two main parts: printing spaces and printing the actual matrix elements.
Pseudocode
function diamond_view(matrix)
if size of matrix is not odd or matrix is not square
print "Not a valid perfect Odd square matrix"
else
col = size of matrix / 2
counter = col * 3
distance = 3
for i = 0 to size of matrix  1
print spaces to the left (counter spaces)
print elements of the matrix row i from col to (SIZE  col)
if i < size of matrix / 2
decrease counter by distance
decrease col by 1
else
increase col by 1
increase counter by distance
print newline
Algorithm Explanation
 Check if the size of the matrix is odd and if the matrix is square. If not, print an error message.
 Initialize
col
to the middle column index,counter
tocol * 3
, anddistance
to3
.  Loop through each row of the matrix:
 Print spaces to the left of the elements (using the value of
counter
).  Print the elements of the current row within the range of
col
to(SIZE  col)
.  Adjust
col
andcounter
based on the current row index.
 Print spaces to the left of the elements (using the value of
 After printing each row, move to the next line.
Code Solution

1) Diamond view of odd square matrix in java
2) Diamond view of odd square matrix in c++
3) Diamond view of odd square matrix in c
4) Diamond view of odd square matrix in golang
5) Diamond view of odd square matrix in c#
6) Diamond view of odd square matrix in vb.net
7) Diamond view of odd square matrix in php
8) Diamond view of odd square matrix in node js
9) Diamond view of odd square matrix in python
10) Diamond view of odd square matrix in ruby
11) Diamond view of odd square matrix in scala
12) Diamond view of odd square matrix in swift
13) Diamond view of odd square matrix in kotlin
Resultant Output Explanation
It calculates the necessary spacing and prints the elements of the matrix to create the diamond pattern. The output shows the diamond shape pattern formed by the numbers from the given matrix.
Time Complexity
The time complexity of this algorithm is O(n^2), where 'n' is the number of rows (or columns) in the matrix. This is because you are iterating through each element of the matrix once and performing a constant amount of work for each element.
Your code is clear and correctly produces the desired diamond pattern within an oddsized square matrix. The output explanation demonstrates that it works as expected for the given example matrix.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment