Search element in a Spirally sorted Matrix | Function to return the ring , the number x belongs to . ; Returns - 1 if number x is smaller than least element of arr ; l and r represent the diagonal elements to search in ; Returns - 1 if number x is greater than the largest element of arr ; Function to perform binary search on an array sorted in increasing order l and r represent the left and right index of the row to be searched ; Function to perform binary search on a particular column of the 2D array t and b represent top and bottom rows ; Function to perform binary search on an array sorted in decreasing order ; Function to perform binary search on a particular column of the 2D array ; Function to find the position of the number x ; Finding the ring ; To store row and column ; Edge case if n is odd ; Check which of the 4 sides , the number x lies in ; Printing the position ; Driver code
def findRing ( arr , x ) : NEW_LINE INDENT if arr [ 0 ] [ 0 ] > x : NEW_LINE INDENT return - 1 NEW_LINE DEDENT l , r = 0 , ( n + 1 ) // 2 - 1 NEW_LINE if n % 2 == 1 and arr [ r ] [ r ] < x : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if n % 2 == 0 and arr [ r + 1 ] [ r ] < x : NEW_LINE INDENT return - 1 NEW_LINE DEDENT while l < r : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if arr [ mid ] [ mid ] <= x : NEW_LINE INDENT if ( mid == ( n + 1 ) // 2 - 1 or arr [ mid + 1 ] [ mid + 1 ] > x ) : NEW_LINE INDENT return mid NEW_LINE DEDENT else : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT DEDENT return r NEW_LINE DEDENT def binarySearchRowInc ( arr , row , l , r , x ) : NEW_LINE INDENT while l <= r : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if arr [ row ] [ mid ] == x : NEW_LINE INDENT return mid NEW_LINE DEDENT elif arr [ row ] [ mid ] < x : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def binarySearchColumnInc ( arr , col , t , b , x ) : NEW_LINE INDENT while t <= b : NEW_LINE INDENT mid = ( t + b ) // 2 NEW_LINE if arr [ mid ] [ col ] == x : NEW_LINE INDENT return mid NEW_LINE DEDENT elif arr [ mid ] [ col ] < x : NEW_LINE INDENT t = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT b = mid - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def binarySearchRowDec ( arr , row , l , r , x ) : NEW_LINE INDENT while l <= r : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if arr [ row ] [ mid ] == x : NEW_LINE INDENT return mid NEW_LINE DEDENT elif arr [ row ] [ mid ] < x : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def binarySearchColumnDec ( arr , col , t , b , x ) : NEW_LINE INDENT while t <= b : NEW_LINE INDENT mid = ( t + b ) // 2 NEW_LINE if arr [ mid ] [ col ] == x : NEW_LINE INDENT return mid NEW_LINE DEDENT elif arr [ mid ] [ col ] < x : NEW_LINE INDENT b = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT t = mid + 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def spiralBinary ( arr , x ) : NEW_LINE INDENT f1 = findRing ( arr , x ) NEW_LINE r , c = None , None NEW_LINE if f1 == - 1 : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT if n % 2 == 1 and f1 == ( n + 1 ) // 2 - 1 : NEW_LINE INDENT print ( f1 , f1 ) NEW_LINE return NEW_LINE DEDENT if x < arr [ f1 ] [ n - f1 - 1 ] : NEW_LINE INDENT c = binarySearchRowInc ( arr , f1 , f1 , n - f1 - 2 , x ) NEW_LINE r = f1 NEW_LINE DEDENT elif x < arr [ n - f1 - 1 ] [ n - f1 - 1 ] : NEW_LINE INDENT c = n - f1 - 1 NEW_LINE r = binarySearchColumnInc ( arr , n - f1 - 1 , f1 , n - f1 - 2 , x ) NEW_LINE DEDENT elif x < arr [ n - f1 - 1 ] [ f1 ] : NEW_LINE INDENT c = binarySearchRowDec ( arr , n - f1 - 1 , f1 + 1 , n - f1 - 1 , x ) NEW_LINE r = n - f1 - 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = binarySearchColumnDec ( arr , f1 , f1 + 1 , n - f1 - 1 , x ) NEW_LINE c = f1 NEW_LINE DEDENT if c == - 1 or r == - 1 : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " { 0 } ▁ { 1 } " . format ( r , c ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE arr = [ [ 1 , 2 , 3 , 4 ] , [ 12 , 13 , 14 , 5 ] , [ 11 , 16 , 15 , 6 ] , [ 10 , 9 , 8 , 7 ] ] NEW_LINE spiralBinary ( arr , 7 ) NEW_LINE DEDENT