Maximize count of indices with same element by pairing rows from given Matrices | JavaScript Program to implement the above approach ; Function to find the maximum score ; Stores the maximum sum of scores ; For each permutation of pos vector calculate the score ; If values at current indexes are same then increment the current score ; Print the maximum score ; Driver Code
function next_permutation ( array ) { var i = array . length - 1 ; while ( i > 0 && array [ i - 1 ] >= array [ i ] ) { i -- ; } if ( i <= 0 ) { return false ; } var j = array . length - 1 ; while ( array [ j ] <= array [ i - 1 ] ) { j -- ; } var temp = array [ i - 1 ] ; array [ i - 1 ] = array [ j ] ; array [ j ] = temp ; j = array . length - 1 ; while ( i < j ) { temp = array [ i ] ; array [ i ] = array [ j ] ; array [ j ] = temp ; i ++ ; j -- ; } return array ; } function maxScoreSum ( a , b ) { let maxSum = 0 ; let pos = [ ] ; for ( let i = 0 ; i < a . length ; i ++ ) { pos . push ( i ) ; } do { let curSum = 0 ; for ( let i = 0 ; i < a . length ; i ++ ) { for ( let j = 0 ; j < a [ pos [ i ] ] . length ; j ++ ) { curSum += ( a [ pos [ i ] ] [ j ] == b [ i ] [ j ] ) ; maxSum = Math . max ( maxSum , curSum ) ; } } } while ( next_permutation ( pos ) ) ; document . write ( maxSum ) ; } let N = 3 , M = 3 ; let a = [ [ 1 , 1 , 0 ] , [ 1 , 0 , 1 ] , [ 0 , 0 , 1 ] ] ; let b = [ [ 1 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 1 , 1 , 0 ] ] ; maxScoreSum ( a , b ) ;