Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@avaiesshaik782 | 786 |

@arpitsatnalika | 777 |

@tujhekya | 602 |

@prakharxyz | 601 |

@code_it_ | 591 |

@iamronak | 505 |

@imnm | 500 |

@pradeepkumarjad | 488 |

@satyam0460 | 481 |

@nitesha125 | 476 |

@Rahul Jadhav 1 | 466 |

Complete Leaderboard |

Given a matrix of size R*C. Traverse the matrix in spiral form.

**Example 1:**

**Input**:
R = 4, C = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15,16}}
**Output**:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
**Explanation**:

**Example 2:**

**Input**:
R = 3, C = 4
matrix[][] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}
**Output**:
1 2 3 4 8 12 11 10 9 5 6 7
**Explanation**:
Applying same technique as shown above,
output for the 2nd testcase will be
1 2 3 4 8 12 11 10 9 5 6 7.

**Your Task:**

You dont need to read input or print anything. Complete the function **spirallyTraverse() **that takes **matrix, R and C **as input parameters** **and returns a list of integers denoting the spiral traversal of matrix.

**Expected Time Complexity:** O(R*C)

**Expected Auxiliary Space:** O(R*C)

**Constraints:**

2 <= R, C <= 100

0 <= matrix_{i} <= 100

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Spirally traversing a matrix

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.