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 ; Prlet 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 ; Prlet required arrangement of array B ; Given arrays A [ ] and B [ ] ; Function Call
function RearrangeB ( A , B ) { let n = A . length ; if ( n == 1 && A [ 0 ] == B [ 0 ] ) { document . write ( " " ) ; return ; } for ( let i = 0 ; i < n / 2 ; i ++ ) { let t = B [ i ] ; B [ i ] = B [ n - i - 1 ] ; B [ n - i - 1 ] = t ; } for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] == A [ i ] ) { let t = B [ i + 1 ] ; B [ i + 1 ] = B [ i ] ; B [ i ] = t ; break ; } } for ( let k in B ) document . write ( B [ k ] + " " ) ; } let A = [ 2 , 4 , 5 , 8 ] ; let B = [ 2 , 4 , 5 , 8 ] ; RearrangeB ( A , B ) ;