Doubt in spiral print anticlockwise

i tries it for atleast 1.5 hour but ended getting no idea
please give me some hint to solve this question!!

Hello @shubham.mehla2000,

Observe the following figure:

image

Suppose:
k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
count- to record the no. of elements that have been traversed.
t- total number of elements in the matrix.

Algo:

Repeat till k < m && l < n:

  1. Check if count == t, then break.
  2. print the right column for i=k to m //first row
  3. increment l as you won’t traverse that column again.
  4. Check if count == t, then break.
  5. print the lower row for i=l to n //last row
  6. decrement m
  7. Check if count == t, then break.
  8. print left column for i=m to k //last column
  9. decrement n
  10. Check if count == t, then break.
  11. print upper row for i=n to l //first row
  12. increment l

Hope, this would help.
Give a like, if you are satisfied.

1 Like