Swiggy interview question
Anonymous User
3990

Given an infinite spiral matrix,
1.find the manhattan distance of a number from center of matrix.

  1. find coordinates of that number (consider 1 at {0,0}).
    Note that Manhattan distance between two points {x1,y1} and {x2,y2} is |x1 - x2| + |y1 - y2|
    Matrix looks like this

    17 16 15 14 13
    18 5 4 3 12
    19 6 1 2 11
    20 7 8 9 10
    21 22 23 24 25 ...

Example -1
Input - 25
Output - Distance -> 4
Coordinate -> {2,-2}

Example -2
Input -12
Output - Distance ->3
Coordinate -> {2,1}

Let me know your approaches.

Comments (8)