If this is the case, we can follow this method: take variables 1. dir = 1 2. st_col=0 3. end_col = n-1 4. st_row = 1 5. end_row = n-1 6. row = 0 7. col = n-1 8. noOfCellsVisited = 0
dir are as follows: 1 - left ro right 2 - top to bottom 3 - right to left 4 - bottom to top row will keep account of the row we will traverse when dir=1 or 3 col will keep account of the column we will traverse when dir=2 or 4 st_col and end_col will keep account of leftmost and rightmost columns for traversing rows st_row and end_row will keep account of topmost and bottom rows for traversing columns while(noOfCellsVisited<mn) { if(dir == 1) { for i = st_col to end_col { print arr[row][i]; noOfCellsVisited++} dir++; end_col--; row = n-1-row; } if(dir==2) { for i = st_row to end_row { print arr[i][col]; noOfCellsVisited++} dir++; end_row--; col=n-1-col; } if(dir==3) { for i = end_col to st_col { print arr[row][i]; noOfCellsVisited++} dir++; row=n-row; st_col++; } if(dir==4) { for i = end_row to st_row { print arr[i][col; noOfCellsVisited++} } dir=1; col=n-1-(i-1); st_row++; } -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.