Given two integers i and j, the task is to print the i * j thmatrix elements that can be obtained by filling the matrix in the following spiral manner:
Spiral Grid Representation fig 1
Examples:
Input: i = 3, j = 4
Output: 12
Explanation:
i = 3, j = 4 and grid[3][4] = 12Input: i = 5, j = 5
Output: 21
Explanation:
i = 5, j = 5 grid[5][5] = 21
Approach: In the problem, it can be observed that when i is even the first number in the grid is i2 (22 = 4 in 2nd row), and when i is the odd first number in the grid is (i-1)2 + 1 ((3-1)2 + 1 = 5 in 3rd row). Similarly, when j is odd the first number in the grid is j2 (32 = 9 in 3rd column) and when j is even first number in the grid is (j-1)2 + 1 ((4-1)2 + 1 = 10 in 4th column). So every row starts with either the i2 or (i-1)2 + 1 and every column starts with either j2  or (j-1)2 + 1.Â
The problem can be divided into the following cases :Â
- Case 1 : i = jÂ
Observe that diagonal elements of the grid can be represented by the formula i2 – (i-1) or j2 – (j – 1). - Case 2: i > j
- Case 1: i is even
In this case, the first number of row i will be i2. Now by subtracting (j – 1) from the first number of the row calculate the value present at the given index. So the formula will be i2 – (j-1). - Case 2: i is oddÂ
In this case, the first number of row i will be (i – 1)2 + 1. Now by adding (j – 1) to the first number of the row calculate the value present at the given index. So the formula will be (i – 1)2 + 1 + (j – 1).
- Case 1: i is even
- Case 3: i < j
- Case 1: j is even
In this case, the first number of column j will be (j – 1)2 + 1. Now by adding (i – 1) to the first number of the column calculate the value present at the given index. So the formula will be (j – 1)2 + 1 + (i – 1). - Case 2: j is odd
In this case, the first number of column j will be j2. Now by subtracting (i – 1) from the first number of the column calculate the value present at the given index. So the formula will be j2 + 1 – (i – 1).
- Case 1: j is even
Follow the steps below to solve the problem:
- Check if i is equal to j and print i * i – (i – 1).
- If i is greater than j:
- If i is even print i * i – (j – 1).
- Otherwise, print (i – 1)* (i – 1) + 1 + (j – 1).
- If j is greater than i:
- If j is even print (j – 1) * (j – 1) + 1 + (i – 1).
- Otherwise, print j * j – (i – 1).
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <iostream> using namespace std; Â
// Function to the find // element at (i, j) index int findInGrid( int i, int j) { Â Â Â Â if (i == j) Â Â Â Â Â Â Â Â return (i * i - (i - 1)); Â
    else if (i > j) {         if (i % 2 == 0)             return i * i - (j - 1);         else             return (i - 1) * (i - 1) + 1 + (j - 1);     } Â
    else {         if (j % 2 == 0)             return (j - 1) * (j - 1) + 1 + (i - 1);         else             return j * j - (i - 1);     } } Â
// Driver Code int main() { Â
    int i = 3, j = 4; Â
    // Function Call     cout << findInGrid(i, j); Â
    return 0; } |
Java
// Java program for the above approach import java.io.*; Â
class GFG{ Â
// Function to the find // element at (i, j) index static int findInGrid( int i, int j) { Â Â Â Â if (i == j) Â Â Â Â Â Â Â Â return (i * i - (i - 1 )); Â
    else if (i > j)     {         if (i % 2 == 0 )             return i * i - (j - 1 );         else             return (i - 1 ) * (i - 1 ) +                          1 + (j - 1 );     } Â
    else     {         if (j % 2 == 0 )             return (j - 1 ) * (j - 1 ) +                          1 + (i - 1 );         else             return j * j - (i - 1 );     } } Â
// Driver Code public static void main(String[] args) { Â Â Â Â int i = 3 , j = 4 ; Â
    // Function Call     System.out.println(findInGrid(i, j)); } } Â
// This code is contributed by Dharanendra L V |
Python3
# Python3 program for the above approach Â
# Function to the find # element at(i, j) index def findInGrid(i, j): Â Â Â Â Â Â Â Â Â if (i = = j): Â Â Â Â Â Â Â Â return (i * i - (i - 1 )) Â
    elif (i > j):         if (i % 2 = = 0 ):             return i * i - (j - 1 )         else :             return ((i - 1 ) * (i - 1 ) +                           1 + (j - 1 )) Â
    else :         if (j % 2 = = 0 ):             return ((j - 1 ) * (j - 1 ) +                           1 + (i - 1 ))         else :             return j * j - (i - 1 ) Â
# Driver Code i = 3 j = 4 Â
# Function Call print (findInGrid(i, j)) Â
# This code is contributed by Dharanendra L V |
C#
// C# program for the above approach using System; Â
class GFG{ Â
// Function to the find // element at (i, j) index static int findInGrid( int i, int j) { Â Â Â Â if (i == j) Â Â Â Â Â Â Â Â return (i * i - (i - 1)); Â
    else if (i > j)     {         if (i % 2 == 0)             return i * i - (j - 1);         else             return (i - 1) * (i - 1) +                          1 + (j - 1);     } Â
    else     {         if (j % 2 == 0)             return (j - 1) * (j - 1) +                          1 + (i - 1);         else             return j * j - (i - 1);     } } Â
// Driver Code static public void Main() {     int i = 3, j = 4;          // Function Call     Console.WriteLine(findInGrid(i, j)); } } Â
// This code is contributed by Dharanendra L V |
Javascript
<script> Â
// Javascript program for the above approach Â
// Function to the find // element at (i, j) index function findInGrid(i, j) { Â Â Â Â if (i == j) Â Â Â Â Â Â Â Â return (i * i - (i - 1)); Â
    else if (i > j) {         if (i % 2 == 0)             return i * i - (j - 1);         else             return (i - 1) * (i - 1) + 1 + (j - 1);     } Â
    else {         if (j % 2 == 0)             return (j - 1) * (j - 1) + 1 + (i - 1);         else             return j * j - (i - 1);     } } Â
// Driver Code Â
    let i = 3, j = 4; Â
    // Function Call     document.write(findInGrid(i, j));      </script> |
12
Time complexity: O(1)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!