11/*
2- Problem: Number Spiral (CSES Problem Set)
3- Link: https://cses.fi/problemset/task/1071/
4- Topic: Math / Pattern Recognition
5-
6- Description:
7- Given coordinates (y, x) in an infinite number spiral, find the value at that position.
8- The spiral grows outwards starting from 1 at (1,1).
9-
10- Example spiral pattern:
11- 1 2 9 10 25
12- 4 3 8 11 24
13- 5 6 7 12 23
14- 16 15 14 13 22
15- 17 18 19 20 21
16-
17- Approach:
18- - The maximum of (x, y) determines which square layer the cell belongs to.
19- - The last number of each layer (n) is always n^2.
20- - Depending on whether n is even or odd, the numbering direction differs:
21- - Even layers fill top-to-bottom.
22- - Odd layers fill left-to-right.
23- - Use this pattern to directly calculate the value at (x, y) in O(1) time.
2+ Number Spiral (CSES Problem 1071)
3+ Language: C++
4+ Problem Description:
5+ You are given coordinates (y, x) in an infinite grid that forms a "number spiral".
6+ The spiral starts at (1, 1) = 1 and moves right and upward in square layers.
7+
8+ Your task: For each pair (y, x), determine the number located at that cell.
9+
10+ Example (small spiral visualization):
11+
12+ 1 2 9 10 25
13+ 4 3 8 11 24
14+ 5 6 7 12 23
15+ 16 15 14 13 22
16+ 17 18 19 20 21
17+
18+ So, for coordinate (y=2, x=3), the answer is 8.
19+
20+ Algorithm:
21+ 1. Each layer corresponds to the maximum of x and y.
22+ 2. The square of (layer - 1) gives the value before the current layer starts.
23+ 3. Depending on whether the layer number is even or odd:
24+ - Even layer → spiral moves upward.
25+ - Odd layer → spiral moves rightward.
26+ 4. Using conditional formulas, compute the number directly in O(1).
2427
2528Time Complexity: O(1)
2629Space Complexity: O(1)
@@ -29,38 +32,57 @@ Space Complexity: O(1)
2932#include < bits/stdc++.h>
3033using namespace std ;
3134
32- long long numberSpiralValue (long long x, long long y) {
33- if (x == 1 && y == 1 ) return 1 ;
34-
35- long long layer = max (x, y);
36- long long base = (layer - 1 ) * (layer - 1 );
37-
38- if (layer % 2 == 0 ) { // even layer
39- if (y == layer)
40- return base + x;
41- else
42- return base + layer + (layer - y);
43- }
44- else { // odd layer
45- if (x == layer)
46- return base + y;
35+ /*
36+ solve(x, y)
37+ Computes the value at coordinates (y, x) in the number spiral.
38+
39+ Parameters:
40+ x -> Column index (1-based)
41+ y -> Row index (1-based)
42+
43+ Returns:
44+ The number located at position (y, x)
45+ */
46+ long long solve (long long x, long long y) {
47+ if (x == 1LL && y == 1LL ) return 1LL ;
48+
49+ // Layer = max(x, y)
50+ long long layer = max (x, y) - 1LL ;
51+ long long baseValue = layer * layer; // Value before this layer starts
52+
53+ // Even layer → starts bottom-right, moves upward
54+ // Odd layer → starts top-left, moves rightward
55+ if (layer % 2LL == 0LL ) {
56+ if (y == layer + 1LL )
57+ return baseValue + x; // On bottom row of even layer
4758 else
48- return base + layer + (layer - x);
59+ return baseValue + x + layer + 1LL - y; // Moving upward
60+ } else {
61+ if (x == layer + 1LL )
62+ return baseValue + y; // On right column of odd layer
63+ else
64+ return baseValue + y + layer + 1LL - x; // Moving leftward
4965 }
5066}
5167
5268int main () {
53- vector<pair<long long , long long >> tests = {
54- {2 , 3 }, // Expected output: 8
55- {1 , 1 }, // Expected output: 1
56- {4 , 2 }, // Expected output: 15
57- {3 , 5 } // Expected output: 27
58- };
59-
60- for (auto [y, x] : tests) {
61- cout << " Input (y=" << y << " , x=" << x << " ) -> Output: "
62- << numberSpiralValue (x, y) << endl;
63- }
69+
70+ // Example test cases for verification
71+ cout << " Example Test Cases:" << endl;
72+
73+ cout << " Test 1 -> y=2, x=3 | Expected: 8 | Got: " << solve (3 , 2 ) << endl;
74+ cout << " Test 2 -> y=1, x=1 | Expected: 1 | Got: " << solve (1 , 1 ) << endl;
75+ cout << " Test 3 -> y=4, x=2 | Expected: 15 | Got: " << solve (2 , 4 ) << endl;
76+ cout << " Test 4 -> y=5, x=5 | Expected: 21 | Got: " << solve (5 , 5 ) << endl;
6477
6578 return 0 ;
6679}
80+
81+ /*
82+ Expected Output:
83+ Example Test Cases:
84+ Test 1 -> y=2, x=3 | Expected: 8 | Got: 8
85+ Test 2 -> y=1, x=1 | Expected: 1 | Got: 1
86+ Test 3 -> y=4, x=2 | Expected: 15 | Got: 15
87+ Test 4 -> y=5, x=5 | Expected: 21 | Got: 21
88+ */
0 commit comments