Rearrange an array to make similar indexed elements different from that of another array | Function to find the arrangement of array B [ ] such that element at each index of A [ ] and B [ ] are not equal ; Length of array ; Print not possible , if arrays only have single equal element ; Reverse array B ; Traverse over arrays to check if there is any index where A [ i ] and B [ i ] are equal ; Swap B [ i ] with B [ i - 1 ] ; Break the loop ; Print required arrangement of array B ; Given arrays A [ ] and B [ ] ; Function call
def RearrangeB ( A , B ) : NEW_LINE INDENT n = len ( A ) NEW_LINE if ( n == 1 and A [ 0 ] == B [ 0 ] ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT for i in range ( n // 2 ) : NEW_LINE INDENT t = B [ i ] NEW_LINE B [ i ] = B [ n - i - 1 ] NEW_LINE B [ n - i - 1 ] = t NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( B [ i ] == A [ i ] ) : NEW_LINE INDENT B [ i ] , B [ i - 1 ] = B [ i - 1 ] , B [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT for k in B : NEW_LINE INDENT print ( k , end = " ▁ " ) NEW_LINE DEDENT DEDENT A = [ 2 , 4 , 5 , 8 ] NEW_LINE B = [ 2 , 4 , 5 , 8 ] NEW_LINE RearrangeB ( A , B ) NEW_LINE