Open In App

Find the position of given element N in infinite spiral matrix starting from top-left

Given a number N and an infinite 2D matrix, which is to be filled using the algorithm given below, the task is to find the coordinates of the given element present in the matrix. The algorithm is as follows:



Examples:

Input: N = 12
Output: 3 4
Explanation: 12 lies in the cell with row as 3 and column as 4



Input: N = 10549857
Output: 353 3249
Explanation: 10549857 lies in the cell with row as 353 and column as 3249

 

Approach: Following observations can be made:

d = l^2 – N 

r = l
c = d + l

r = 2l – d – 1
c = l 

Below is the implementation of the above approach:




// C++ Implementation for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the coordinates of n
void findCoordinates(int n)
{
    // Store the row and column of
    // n respectively
    int r, c;
 
    // Stores the inverted L
    // in which n lies
    int l = ceil(sqrt(n));
 
    // Stores the difference between
    // square of l and n
    int d = (l * l) - n;
 
    // If d is less than l
    if (d < l) {
        r = l;
        c = d + 1;
    }
    else {
        c = l;
        r = (2 * l) - d - 1;
    }
 
    cout << r << " " << c;
}
 
// Driver code
int main()
{
    // Given n
    int N = 10549857;
 
    // Function call
    findCoordinates(N);
 
    return 0;
}




// Java Implementation for the above approach
 
public class GFG {
     
// Function to find the coordinates of n
static void findCoordinates(int n)
{
   
    // Store the row and column of
    // n respectively
    int r, c;
 
    // Stores the inverted L
    // in which n lies
    int l = (int)Math.ceil(Math.sqrt(n));
 
    // Stores the difference between
    // square of l and n
    int d = (l * l) - n;
 
    // If d is less than l
    if (d < l) {
        r = l;
        c = d + 1;
    }
    else {
        c = l;
        r = (2 * l) - d - 1;
    }
 
    System.out.print(r + " " + c);
}
 
    // Driver code
    public static void main (String[] args) {
        // Given n
        int N = 10549857;
     
        // Function call
        findCoordinates(N);
 
    }
}
 
// This code is contributed by AnkThon




# Python Program to implement
# the above approach
import math
 
# Function to find the coordinates of n
def findCoordinates(n):
   
    # Store the row and column of
    # n respectively
    #let r, c
 
    # Stores the inverted L
    # in which n lies
    l = math.ceil(math.sqrt(n))
 
    # Stores the difference between
    # square of l and n
    d = (l * l) - n
 
    # If d is less than l
    if (d < l):
        r = l
        c = d + 1
    else:
        c = l
        r = (2 * l) - d - 1
 
    print(f"{r} {c}")
 
# Driver code
 
# Given n
N = 10549857
 
# Function call
findCoordinates(N)
 
# This code is contributed by Saurabh Jaiswal




// C# Implementation for the above approach
using System;
public class GFG {
     
// Function to find the coordinates of n
static void findCoordinates(int n)
{
   
    // Store the row and column of
    // n respectively
    int r, c;
 
    // Stores the inverted L
    // in which n lies
    int l = (int)Math.Ceiling(Math.Sqrt(n));
 
    // Stores the difference between
    // square of l and n
    int d = (l * l) - n;
 
    // If d is less than l
    if (d < l) {
        r = l;
        c = d + 1;
    }
    else {
        c = l;
        r = (2 * l) - d - 1;
    }
 
    Console.Write(r + " " + c);
}
 
    // Driver code
    public static void Main (string[] args) {
        // Given n
        int N = 10549857;
     
        // Function call
        findCoordinates(N);
 
    }
}
 
// This code is contributed by shivanisinghss2110




<script>
        // JavaScript Program to implement
        // the above approach
 
 
        // Function to find the coordinates of n
        function findCoordinates(n) {
            // Store the row and column of
            // n respectively
            let r, c;
 
            // Stores the inverted L
            // in which n lies
            let l = Math.ceil(Math.sqrt(n));
 
            // Stores the difference between
            // square of l and n
            let d = (l * l) - n;
 
            // If d is less than l
            if (d < l) {
                r = l;
                c = d + 1;
            }
            else {
                c = l;
                r = (2 * l) - d - 1;
            }
 
            document.write(r + " " + c);
        }
 
        // Driver code
 
        // Given n
        let N = 10549857;
 
        // Function call
        findCoordinates(N);
 
 
 
// This code is contributed by Potta Lokesh
    </script>

Output
353 3249

Time Complexity: O(log(n)) because inbuilt sqrt function has been used
Auxiliary Space: O(1)


Article Tags :