CSES Solutions - Number Spiral
Last Updated : 27 Mar, 2024
A number spiral is an infinite grid whose upper-left square has the number 1. The task is to find the number in row Y and column X. Here are the first five layers of the spiral:

Examples:
Input: Y = 2, X = 3
Output: 8
Explanation: The 2nd row, 3rd column contains 8.
Input: Y = 4, X = 2
Output: 15
Explanation: The 4th row, 2nd column contains 15.
Approach: To solve the problem, follow the below idea:
It can be observed that grid consists of many squares and the values at boundaries of the square is either in increasing or decreasing order. The answer lies at the boundary of the square whose side is the maximum of Y or X. So now to get the value at Yth row and Xth column we can compute the area of inner square whose side is just one less than the side of square whose boundary contains the answer. The remaining value can be added to the area of inner square by checking the parity of Minimum of Y or X. It can be observed that for even row in the grid, the numbers are in decreasing order, and for the odd row the numbers are in increasing order in the anticlockwise direction.

Step-by-step algorithm:
- Compare Y and X:
- If Y>X:
- Compute the area of inner square which (Y-1)*(Y-1). This represents the numbers which are not on the boundary of the square which contains the answer.
- Now to add the remaining numbers which are on the boundary, check the parity of Yth row, if the Yth row is odd then simply add X to the area of inner square or else add (2*Y - X) to the area.
- Else:
- Similarly, compute the area of inner square which (X-1)*(X-1). This represents the numbers which are not on the boundary of the square which contains the answer.
- Now to add the remaining numbers which are on the boundary, check the parity of Xth column, if the Xth column is even then simply add Y to the area of inner square or else add (2*X - Y) to the area. This will give the final answer.
Below is the implementation of above approach:
C++ #include <bits/stdc++.h> using namespace std; typedef long long ll; // Function to compute the value at position (Y, X) in the // number spiral void NumberSpiral(ll Y, ll X) { // If Y is greater than X, implying Yth row is the outer // boundary if (Y > X) { // Compute the area of the inner square ll ans = (Y - 1) * (Y - 1); ll add = 0; // Check parity of Y to determine if numbers are in // increasing or decreasing order if (Y % 2 != 0) { // Add X to the area if Yth row is odd add = X; } else { // Add 2*Y - X to the area if Yth row is even add = 2 * Y - X; } // Print the final result cout << ans + add << "\n"; } // If X is greater than or equal to Y, implying Xth // column is the outer boundary else { // Compute the area of the inner square ll ans = (X - 1) * (X - 1); ll add = 0; // Check parity of X to determine if numbers are in // increasing or decreasing order if (X % 2 == 0) { // Add Y to the area if Xth column is even add = Y; } else { // Add 2*X - Y to the area if Xth column is odd add = 2 * X - Y; } // Print the final result cout << ans + add << "\n"; } } // Driver Code int main() { ll Y = 2, X = 3; NumberSpiral(Y, X); }
Java import java.util.*; public class NumberSpiral { // Function to compute the value at position (Y, X) in the number spiral static void numberSpiral(long Y, long X) { // If Y is greater than X, implying Yth row is the outer boundary if (Y > X) { // Compute the area of the inner square long ans = (Y - 1) * (Y - 1); long add; // Check parity of Y to determine if numbers are in increasing or decreasing order if (Y % 2 != 0) { // Add X to the area if Yth row is odd add = X; } else { // Add 2*Y - X to the area if Yth row is even add = 2 * Y - X; } // Print the final result System.out.println(ans + add); } // If X is greater than or equal to Y, implying Xth column is the outer boundary else { // Compute the area of the inner square long ans = (X - 1) * (X - 1); long add; // Check parity of X to determine if numbers are in increasing or decreasing order if (X % 2 == 0) { // Add Y to the area if Xth column is even add = Y; } else { // Add 2*X - Y to the area if Xth column is odd add = 2 * X - Y; } // Print the final result System.out.println(ans + add); } } // Driver Code public static void main(String[] args) { long Y = 2, X = 3; numberSpiral(Y, X); } } // This code is contributed by akshitaguprzj3
Python def number_spiral(Y, X): # If Y is greater than X, implying Yth row is the outer boundary if Y > X: # Compute the area of the inner square ans = (Y - 1) * (Y - 1) # Check parity of Y to determine if numbers are in increasing or decreasing order if Y % 2 != 0: # Add X to the area if Yth row is odd add = X else: # Add 2*Y - X to the area if Yth row is even add = 2 * Y - X # Print the final result print(ans + add) # If X is greater than or equal to Y, implying Xth column is the outer boundary else: # Compute the area of the inner square ans = (X - 1) * (X - 1) # Check parity of X to determine if numbers are in increasing or decreasing order if X % 2 == 0: # Add Y to the area if Xth column is even add = Y else: # Add 2*X - Y to the area if Xth column is odd add = 2 * X - Y # Print the final result print(ans + add) # Driver Code Y = 2 X = 3 number_spiral(Y, X)
C# using System; class GFG { // Function to compute the value at position (Y, X) in the // number spiral static void NumberSpiral(long Y, long X) { // If Y is greater than X, implying Yth row is the outer // boundary if (Y > X) { // Compute the area of the inner square long ans = (Y - 1) * (Y - 1); long add = 0; // Check parity of Y to determine if numbers are in // increasing or decreasing order if (Y % 2 != 0) { // Add X to the area if Yth row is odd add = X; } else { // Add 2*Y - X to the area if Yth row is even add = 2 * Y - X; } // Print the final result Console.WriteLine(ans + add); } // If X is greater than or equal to Y, implying Xth // column is the outer boundary else { // Compute the area of the inner square long ans = (X - 1) * (X - 1); long add = 0; // Check parity of X to determine if numbers are in // increasing or decreasing order if (X % 2 == 0) { // Add Y to the area if Xth column is even add = Y; } else { // Add 2*X - Y to the area if Xth column is odd add = 2 * X - Y; } // Print the final result Console.WriteLine(ans + add); } } // Driver Code static void Main() { long Y = 2, X = 3; NumberSpiral(Y, X); } }
JavaScript // Function to compute the value at position (Y, X) in the number spiral function numberSpiral(Y, X) { // If Y is greater than X, implying Yth row is the outer boundary if (Y > X) { // Compute the area of the inner square let ans = (Y - 1) * (Y - 1); let add = 0; // Check parity of Y to determine if numbers are in increasing or decreasing order if (Y % 2 !== 0) { // Add X to the area if Yth row is odd add = X; } else { // Add 2*Y - X to the area if Yth row is even add = 2 * Y - X; } // Print the final result console.log(ans + add); } // If X is greater than or equal to Y, implying Xth column is the outer boundary else { // Compute the area of the inner square let ans = (X - 1) * (X - 1); let add = 0; // Check parity of X to determine if numbers are in increasing or decreasing order if (X % 2 === 0) { // Add Y to the area if Xth column is even add = Y; } else { // Add 2*X - Y to the area if Xth column is odd add = 2 * X - Y; } // Print the final result console.log(ans + add); } } // Driver Code let Y = 2, X = 3; numberSpiral(Y, X);
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
CSES Solutions - Collecting Numbers You are given an array arr[] that contains each number between 1 ... N exactly once. Your task is to collect the numbers from 1 to N in increasing order. On each round, you go through the array from left to right and collect as many numbers as possible. What will be the total number of rounds? Examp
6 min read
Class 9 NCERT Solutions- Chapter 1 Number System - Exercise 1.3 Chapter 1 of the Class 9 NCERT Mathematics textbook, titled "Number System," introduces students to various types of numbers, including real numbers, irrational numbers, and the representation of numbers on the number line. Exercise 1.3 focuses on the representation of irrational numbers on the numb
7 min read
Number of integral solutions for equation x = b*(sumofdigits(x)^a)+c Given a, b and c which are part of the equation x = b * ( sumdigits(x) ^ a ) + c.Where sumdigits(x) determines the sum of all digits of the number x. The task is to find out all integer solutions for x that satisfy the equation and print them in increasing order. Given that, 1<=x<=109Examples:
9 min read
Number Patterns in Whole Numbers Number patterns in whole numbers are sequences that follow a specific rule or relationship, revealing fascinating structures within the set of whole numbers. These patterns often involve basic arithmetic operations such as addition, subtraction, multiplication, or division, and can manifest in vario
5 min read
Check if given point lies on the Square Spiral Path Given a 2D Graph, present initially at (0, 0). Print "YES" if the given point (X, Y) lies on the spiral path, otherwise "NO". The spiral path is to be followed ln a sequential manner as given below: Move one unit right side from the current position.Move two units upwards from the current position.M
6 min read
Find a Number X whose sum with its digits is equal to N Given a positive number N. We need to find number(s) such that sum of digits of those numbers to themselves is equal to N. If no such number is possible print -1. Here N \in [1, 1000000000] Examples: Input : N = 21Output : X = 15Explanation : X + its digit sum = 15 + 1 + 5 = 21 Input : N = 5Output :
15+ min read