Given a 2D array, print it in counter-clock wise spiral form. See the following examples.
Examples :
Input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7
Input:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
Output:
1 7 13 14 15 16 17 18 12 6 5 4 3 2 8 9 10 11
Explanation :

Below is the implementation :
C++
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
void counterClockspiralPrint( int m,
int n,
int arr[R][C])
{
int i, k = 0, l = 0;
int cnt = 0;
int total = m * n;
while (k < m && l < n)
{
if (cnt == total)
break ;
for (i = k; i < m; ++i)
{
cout << arr[i][l] << " " ;
cnt++;
}
l++;
if (cnt == total)
break ;
for (i = l; i < n; ++i)
{
cout << arr[m - 1][i] << " " ;
cnt++;
}
m--;
if (cnt == total)
break ;
if (k < m)
{
for (i = m - 1; i >= k; --i)
{
cout << arr[i][n - 1] << " " ;
cnt++;
}
n--;
}
if (cnt == total)
break ;
if (l < n)
{
for (i = n - 1; i >= l; --i)
{
cout << arr[k][i] << " " ;
cnt++;
}
k++;
}
}
}
int main()
{
int arr[R][C] = {{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 }};
counterClockspiralPrint(R, C, arr);
return 0;
}
|
C
#include <stdio.h>
#define R 4
#define C 4
void counterClockspiralPrint( int m, int n, int arr[R][C])
{
int i, k = 0, l = 0;
int cnt = 0;
int total = m * n;
while (k < m && l < n)
{
if (cnt == total)
break ;
for (i = k; i < m; ++i)
{
printf ( "%d " ,arr[i][l]);
cnt++;
}
l++;
if (cnt == total)
break ;
for (i = l; i < n; ++i)
{
printf ( "%d " ,arr[m - 1][i]);
cnt++;
}
m--;
if (cnt == total)
break ;
if (k < m)
{
for (i = m - 1; i >= k; --i)
{
printf ( "%d " ,arr[i][n - 1]);
cnt++;
}
n--;
}
if (cnt == total)
break ;
if (l < n)
{
for (i = n - 1; i >= l; --i)
{
printf ( "%d " ,arr[k][i]);
cnt++;
}
k++;
}
}
}
int main()
{
int arr[R][C] = {{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 }};
counterClockspiralPrint(R, C, arr);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int R = 4 ;
static int C = 4 ;
static void counterClockspiralPrint( int m,
int n,
int arr[][])
{
int i, k = 0 , l = 0 ;
int cnt = 0 ;
int total = m * n;
while (k < m && l < n)
{
if (cnt == total)
break ;
for (i = k; i < m; ++i)
{
System.out.print(arr[i][l] + " " );
cnt++;
}
l++;
if (cnt == total)
break ;
for (i = l; i < n; ++i)
{
System.out.print(arr[m - 1 ][i] + " " );
cnt++;
}
m--;
if (cnt == total)
break ;
if (k < m)
{
for (i = m - 1 ; i >= k; --i)
{
System.out.print(arr[i][n - 1 ] + " " );
cnt++;
}
n--;
}
if (cnt == total)
break ;
if (l < n)
{
for (i = n - 1 ; i >= l; --i)
{
System.out.print(arr[k][i] + " " );
cnt++;
}
k++;
}
}
}
public static void main(String[] args)
{
int arr[][] = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 } };
counterClockspiralPrint(R, C, arr);
}
}
|
Python3
R = 4
C = 4
def counterClockspiralPrint(m, n, arr) :
k = 0 ; l = 0
cnt = 0
total = m * n
while (k < m and l < n) :
if (cnt = = total) :
break
for i in range (k, m) :
print (arr[i][l], end = " " )
cnt + = 1
l + = 1
if (cnt = = total) :
break
for i in range (l, n) :
print ( arr[m - 1 ][i], end = " " )
cnt + = 1
m - = 1
if (cnt = = total) :
break
if (k < m) :
for i in range (m - 1 , k - 1 , - 1 ) :
print (arr[i][n - 1 ], end = " " )
cnt + = 1
n - = 1
if (cnt = = total) :
break
if (l < n) :
for i in range (n - 1 , l - 1 , - 1 ) :
print ( arr[k][i], end = " " )
cnt + = 1
k + = 1
arr = [ [ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 16 ] ]
counterClockspiralPrint(R, C, arr)
|
C#
using System;
class GFG
{
static int R = 4;
static int C = 4;
static void counterClockspiralPrint( int m,
int n,
int [,] arr)
{
int i, k = 0, l = 0;
int cnt = 0;
int total = m * n;
while (k < m && l < n)
{
if (cnt == total)
break ;
for (i = k; i < m; ++i)
{
Console.Write(arr[i,l] + " " );
cnt++;
}
l++;
if (cnt == total)
break ;
for (i = l; i < n; ++i)
{
Console.Write(arr[m - 1, i] + " " );
cnt++;
}
m--;
if (cnt == total)
break ;
if (k < m) {
for (i = m - 1; i >= k; --i)
{
Console.Write(arr[i, n - 1] + " " );
cnt++;
}
n--;
}
if (cnt == total)
break ;
if (l < n)
{
for (i = n - 1; i >= l; --i)
{
Console.Write(arr[k, i] + " " );
cnt++;
}
k++;
}
}
}
public static void Main()
{
int [,] arr = new int [,] {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
counterClockspiralPrint(R, C, arr);
}
}
|
PHP
<?php
$R = 4;
$C = 4;
function counterClockspiralPrint( $m , $n ,
$arr )
{
$i ; $k = 0; $l = 0;
$cnt = 0;
$total = $m * $n ;
while ( $k < $m and $l < $n )
{
if ( $cnt == $total )
break ;
for ( $i = $k ; $i < $m ; ++ $i )
{
echo $arr [ $i ][ $l ] , " " ;
$cnt ++;
}
$l ++;
if ( $cnt == $total )
break ;
for ( $i = $l ; $i < $n ; ++ $i )
{
echo $arr [ $m - 1][ $i ] , " " ;
$cnt ++;
}
$m --;
if ( $cnt == $total )
break ;
if ( $k < $m )
{
for ( $i = $m - 1; $i >= $k ; -- $i )
{
echo $arr [ $i ][ $n - 1] , " " ;
$cnt ++;
}
$n --;
}
if ( $cnt == $total )
break ;
if ( $l < $n ) {
for ( $i = $n - 1; $i >= $l ; -- $i )
{
echo $arr [ $k ][ $i ] , " " ;
$cnt ++;
}
$k ++;
}
}
}
global $R , $C ;
$arr = array ( array ( 1, 2, 3, 4 ),
array ( 5, 6, 7, 8 ),
array ( 9, 10, 11, 12 ),
array ( 13, 14, 15, 16 ));
echo counterClockspiralPrint( $R , $C , $arr );
?>
|
Javascript
<script>
let R = 4;
let C = 4;
function counterClockspiralPrint(m, n, arr)
{
let i, k = 0, l = 0;
let cnt = 0;
let total = m * n;
while (k < m && l < n)
{
if (cnt == total)
break ;
for (i = k; i < m; ++i)
{
document.write(arr[i][l] + " " );
cnt++;
}
l++;
if (cnt == total)
break ;
for (i = l; i < n; ++i)
{
document.write(arr[m - 1][i] + " " );
cnt++;
}
m--;
if (cnt == total)
break ;
if (k < m)
{
for (i = m - 1; i >= k; --i)
{
document.write(arr[i][n - 1] + " " );
cnt++;
}
n--;
}
if (cnt == total)
break ;
if (l < n)
{
for (i = n - 1; i >= l; --i)
{
document.write(arr[k][i] + " " );
cnt++;
}
k++;
}
}
}
let arr = [[ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ]];
counterClockspiralPrint(R, C, arr);
</script>
|
Output :
1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7
Time Complexity : O(mn).
Auxiliary Space: O(1) because constant space has been used
Alternate Implementation :
C++
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
void counterClockspiralPrint( int Matrix[R][C])
{
int size = R;
int i = size, k = 0, flag = 0, j = 0;
while (i > 0) {
for (j = flag; j < i; j++) {
cout << Matrix[j][k] << " " ;
}
i = i - 1;
j = j - 1;
k = j;
if (i > 0) {
for (j = size - i; j < i + 1; j++)
cout << Matrix[k][j] << " " ;
for (j = k - 1; j > size - i - 2; j--)
cout << Matrix[j][k] << " " ;
}
else
break ;
j = j + 1;
k = j;
i = i - 1;
if (i > 0) {
for (j = i; j > size - i - 2; j--)
cout << Matrix[k][j] << " " ;
k = k + 1;
i = i + 1;
flag = flag + 1;
}
else
break ;
}
}
int main()
{
int Matrix[R][C] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
counterClockspiralPrint(Matrix);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int R = 4 ;
static int C = 4 ;
static void counterClockspiralPrint( int Matrix[][])
{
int size = R;
int i = size, k = 0 , flag = 0 , j = 0 ;
while (i > 0 ) {
for (j = flag; j < i; j++) {
System.out.print(Matrix[j][k]+ " " );
}
i = i - 1 ;
j = j - 1 ;
k = j;
if (i > 0 ) {
for (j = size - i; j < i + 1 ; j++)
System.out.print(Matrix[k][j]+ " " );
for (j = k - 1 ; j > size - i - 2 ; j--)
System.out.print(Matrix[j][k]+ " " );
}
else
break ;
j = j + 1 ;
k = j;
i = i - 1 ;
if (i > 0 ) {
for (j = i; j > size - i - 2 ; j--)
System.out.print(Matrix[k][j]+ " " );
k = k + 1 ;
i = i + 1 ;
flag = flag + 1 ;
}
else
break ;
}
}
public static void main(String[] args)
{
int Matrix[][] = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 } };
counterClockspiralPrint(Matrix);
}
}
|
Python
def counterClockspiralPrint(Matrix):
size = len (Matrix)
flag = 0
k, i = 0 , size
while (i > 0 ):
for j in range (flag,i):
print (Matrix[j][k], end = ' ' )
i = i - 1
k = j
if (i > 0 ):
for j in range (size - i,i + 1 ):
print (Matrix[k][j], end = ' ' )
for j in range (k - 1 ,size - i - 2 , - 1 ):
print (Matrix[j][k], end = ' ' )
else : break
k = j
i = i - 1
if (i > 0 ):
for j in range (i,size - i - 2 , - 1 ):
print (Matrix[k][j], end = ' ' )
k,i = k + 1 ,i + 1
flag = flag + 1
else : break
arr = [ [ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 16 ] ]
counterClockspiralPrint(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int R = 4;
static int C = 4;
static void counterClockspiralPrint( int [,] Matrix)
{
int size = R;
int i = size, k = 0, flag = 0, j = 0;
while (i > 0) {
for (j = flag; j < i; j++) {
Console.Write(Matrix[j, k]+ " " );
}
i = i - 1;
j = j - 1;
k = j;
if (i > 0) {
for (j = size - i; j < i + 1; j++)
Console.Write(Matrix[k, j]+ " " );
for (j = k - 1; j > size - i - 2; j--)
Console.Write(Matrix[j, k]+ " " );
}
else
break ;
j = j + 1;
k = j;
i = i - 1;
if (i > 0) {
for (j = i; j > size - i - 2; j--)
Console.Write(Matrix[k, j]+ " " );
k = k + 1;
i = i + 1;
flag = flag + 1;
}
else
break ;
}
}
public static void Main( string [] args)
{
int [,] Matrix = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
counterClockspiralPrint(Matrix);
}
}
|
Javascript
<script>
const R = 4
const C = 4
function counterClockspiralPrint(Matrix)
{
let size = R;
let i = size, k = 0, flag = 0, j = 0;
while (i > 0) {
for (j = flag; j < i; j++) {
document.write(Matrix[j][k] + " " );
}
i = i - 1;
j = j - 1;
k = j;
if (i > 0) {
for (j = size - i; j < i + 1; j++)
document.write(Matrix[k][j] + " " );
for (j = k - 1; j > size - i - 2; j--)
document.write(Matrix[j][k] + " " );
}
else
break ;
j = j + 1;
k = j;
i = i - 1;
if (i > 0) {
for (j = i; j > size - i - 2; j--)
document.write(Matrix[k][j] + " " );
k = k + 1;
i = i + 1;
flag = flag + 1;
}
else
break ;
}
}
let Matrix = [[1, 2, 3, 4 ],
[5, 6, 7, 8 ],
[9, 10, 11, 12 ],
[13, 14, 15, 16 ] ];
counterClockspiralPrint(Matrix);
</script>
|
Output1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7
Time Complexity: O(n2)
Auxiliary Space: O(1), since no extra space has been taken.