Find the distance between two person after reconstruction of queue | Function to find the correct order and then return the distance between the two persons ; Make pair of both height & infront and insert to vector ; Sort the vector in ascending order ; Find the correct place for every person ; Insert into position vector according to infront value ; Driver code
def getDistance ( arr , n , a , b ) : NEW_LINE INDENT vp = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT vp . append ( [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ) NEW_LINE DEDENT vp = sorted ( vp ) NEW_LINE pos = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( len ( vp ) ) : NEW_LINE INDENT height = vp [ i ] [ 0 ] NEW_LINE k = vp [ i ] [ 1 ] NEW_LINE pos [ k ] = height NEW_LINE DEDENT first = - 1 NEW_LINE second = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( pos [ i ] == a ) : NEW_LINE INDENT first = i NEW_LINE DEDENT if ( pos [ i ] == b ) : NEW_LINE INDENT second = i NEW_LINE DEDENT DEDENT return abs ( first - second ) NEW_LINE DEDENT arr = [ [ 5 , 0 ] , [ 3 , 0 ] , [ 2 , 0 ] , [ 6 , 4 ] , [ 1 , 0 ] , [ 4 , 3 ] ] NEW_LINE n = len ( arr ) NEW_LINE a = 6 NEW_LINE b = 5 NEW_LINE print ( getDistance ( arr , n , a , b ) ) NEW_LINE