Given a positive integer n, consider a 4n * 4n matrix filled with integers from 1 to (4n) * (4n) in row-major order (left to right, top to bottom). Form two coils from the matrix:
- The first coil starts from the top-left cell (0, 0) and spirals inward.
- The second coil starts from the bottom-right cell (4n - 1, 4n - 1) and spirals inward in the opposite direction.
Return these two coils in the same order.
Examples:
Input: n = 1
Output: [[1, 5, 9, 13, 14, 15, 11, 7], [16, 12, 8, 4, 3, 2, 6, 10]]
Explanation: The matrix is:So, the two coils are as given in the Output.
Input: n = 2
Output:
[[1, 9, 17, 25, 33, 41, 49, 57, 58, 59, 60, 61, 62, 63, 55, 47, 39, 31, 23, 15, 14, 13, 12, 11, 19, 27, 35, 43, 44, 45, 37, 29], [64, 56, 48, 40, 32, 24, 16, 8, 7, 6, 5, 4, 3, 2, 10, 18, 26, 34, 42, 50, 51, 52, 53, 54, 46, 38, 30, 22, 21, 20, 28, 36]]
Explanation:
Traverse Layer by Layer - O(n^2) Time and O(n^2) Space
To form the two coils, we traverse the 4n * 4n matrix layer by layer without explicitly creating it.
We use four boundaries to control the spiral traversal and directly calculate each cell's value from its row and column.
The first coil moves inward in one direction, while the second follows the opposite direction.
How Do We Calculate the Cell Value?
- Each row contains size elements. So, row * size gives the number of elements before the current row, and col + 1 gives the position within that row.
Working of the Approach:
- Set size = 4 * n and initialize the boundaries as top = 0, bottom = size - 1, left = 0, and right = size - 1.
- For the first coil, traverse down -> right -> up -> left along the current boundaries.
- For the second coil, traverse up -> left -> down -> right along the same boundaries.
- After completing each side, move the corresponding boundary inward.
- For every visited cell (row, col), calculate its value using row * size + col + 1 and add it to the respective coil.
- Continue until all required layers are traversed and return the two coils.
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> formCoils(int n) {
int m = 8 * n * n;
vector<int> coil1(m), coil2(m);
coil1[0] = 8 * n * n + 2 * n;
int curr = coil1[0];
int flag = 1, step = 2;
int index = 1;
// Generate the standard first coil.
while (index < m) {
for (int i = 0; i < step && index < m; i++)
curr = coil1[index++] = curr - 4 * n * flag;
for (int i = 0; i < step && index < m; i++)
curr = coil1[index++] = curr + flag;
flag *= -1;
step += 2;
}
// Generate the second coil using complementary values.
for (int i = 0; i < m; i++)
coil2[i] = 16 * n * n + 1 - coil1[i];
// Reverse the standard order to match the required output.
reverse(coil1.begin(), coil1.end());
reverse(coil2.begin(), coil2.end());
return {coil2, coil1};
}
int main() {
int n = 1;
vector<vector<int>> ans = formCoils(n);
cout << "[";
for (int i = 0; i < 2; i++) {
cout << "[";
for (int j = 0; j < ans[i].size(); j++) {
cout << ans[i][j];
if (j + 1 < ans[i].size())
cout << ", ";
}
cout << "]";
if (i == 0)
cout << ", ";
}
cout << "]";
return 0;
}
import java.util.ArrayList;
import java.util.Collections;
class GFG {
static ArrayList<ArrayList<Integer>> formCoils(int n) {
int m = 8 * n * n;
ArrayList<Integer> coil1 = new ArrayList<>();
ArrayList<Integer> coil2 = new ArrayList<>();
int first = 8 * n * n + 2 * n;
coil1.add(first);
int curr = first;
int flag = 1, step = 2;
// Generate the standard first coil.
while (coil1.size() < m) {
for (int i = 0; i < step && coil1.size() < m; i++) {
curr -= 4 * n * flag;
coil1.add(curr);
}
for (int i = 0; i < step && coil1.size() < m; i++) {
curr += flag;
coil1.add(curr);
}
flag *= -1;
step += 2;
}
// Generate the second coil using complementary values.
for (int i = 0; i < m; i++)
coil2.add(16 * n * n + 1 - coil1.get(i));
Collections.reverse(coil1);
Collections.reverse(coil2);
ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
ans.add(coil2);
ans.add(coil1);
return ans;
}
public static void main(String[] args) {
int n = 1;
ArrayList<ArrayList<Integer>> ans = formCoils(n);
System.out.println(ans);
}
}
def formCoils(n):
m = 8 * n * n
coil1 = [0] * m
coil1[0] = 8 * n * n + 2 * n
curr = coil1[0]
flag = 1
step = 2
index = 1
# Generate the standard first coil.
while index < m:
for _ in range(step):
if index >= m:
break
curr -= 4 * n * flag
coil1[index] = curr
index += 1
for _ in range(step):
if index >= m:
break
curr += flag
coil1[index] = curr
index += 1
flag *= -1
step += 2
# Generate the second coil using complementary values.
coil2 = [16 * n * n + 1 - value for value in coil1]
coil1.reverse()
coil2.reverse()
return [coil2, coil1]
if __name__ == "__main__":
n = 1
print(formCoils(n))
using System;
using System.Collections.Generic;
class GFG
{
static List<List<int>> formCoils(int n)
{
int m = 8 * n * n;
List<int> coil1 = new List<int>();
List<int> coil2 = new List<int>();
int curr = 8 * n * n + 2 * n;
coil1.Add(curr);
int flag = 1;
int step = 2;
// Generate the standard first coil.
while (coil1.Count < m)
{
for (int i = 0; i < step && coil1.Count < m; i++)
{
curr -= 4 * n * flag;
coil1.Add(curr);
}
for (int i = 0; i < step && coil1.Count < m; i++)
{
curr += flag;
coil1.Add(curr);
}
flag *= -1;
step += 2;
}
// Generate the second coil using complementary values.
for (int i = 0; i < m; i++)
coil2.Add(16 * n * n + 1 - coil1[i]);
coil1.Reverse();
coil2.Reverse();
return new List<List<int>> { coil2, coil1 };
}
static void Main()
{
int n = 1;
List<List<int>> ans = formCoils(n);
Console.WriteLine(
"[" +
"[" + string.Join(", ", ans[0]) + "], " +
"[" + string.Join(", ", ans[1]) + "]" +
"]"
);
}
}
function formCoils(n) {
const m = 8 * n * n;
let coil1 = new Array(m);
coil1[0] = 8 * n * n + 2 * n;
let curr = coil1[0];
let flag = 1;
let step = 2;
let index = 1;
// Generate the standard first coil.
while (index < m) {
for (let i = 0; i < step && index < m; i++) {
curr -= 4 * n * flag;
coil1[index++] = curr;
}
for (let i = 0; i < step && index < m; i++) {
curr += flag;
coil1[index++] = curr;
}
flag *= -1;
step += 2;
}
// Generate the second coil using complementary values.
let coil2 = coil1.map(value => 16 * n * n + 1 - value);
coil1.reverse();
coil2.reverse();
return [coil2, coil1];
}
// Driver Code
let n = 1;
console.log(JSON.stringify(formCoils(n)));
Output
[[1, 5, 9, 13, 14, 15, 11, 7], [16, 12, 8, 4, 3, 2, 6, 10]]

