JSFiddle - React, Tailwind, and code Playground
by Neeraj
JavaScript
var arra = [
[1, 2, 3, 7],
[4, 5, 6, 2],
[7, 8, 9, 0],
[14, 15, 16, 5],
];
// clock wise direction
// [ 1,2,3,7,2,0,9,8,7,4,5,6]
function clockwiseDirection(arra, result) {
if (arra.length == 0) {
return result;
}
result = result.concat(arra.shift());
arra.forEach(function(rightEnd) {
result.push(rightEnd.pop());
});
result = result.concat(arra.pop().reverse());
var tmp = [];
arra.forEach(function(leftEnd) {
tmp.push(leftEnd.shift());
});
result = result.concat(tmp.reverse());
return clockwiseDirection(arra, result);
}
var result = clockwiseDirection(arra, []);
console.log('result', result);
// output (9) [1, 2, 3, 7, 2, 0, 5, 6, 9]
// O(n)
// O(n*n) // reverse
//