Check if two arrays are permutations of each other using Mathematical Operation | Function to check if arrays are permutations of each other . ; Calculating sum and multiply of first array ; Calculating sum and multiply of second array ; If sum and mul of both arrays are equal , return true , else return false . ; Driver code
< ? php function arePermutations ( $ a , $ b , $ n , $ m ) { $ sum1 = 0 ; $ sum2 = 0 ; $ mul1 = 1 ; $ mul2 = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum1 += $ a [ $ i ] ; $ mul1 *= $ a [ $ i ] ; } for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ sum2 += $ b [ $ i ] ; $ mul2 *= $ b [ $ i ] ; } return ( ( $ sum1 == $ sum2 ) && ( $ mul1 == $ mul2 ) ) ; } $ a = array ( 1 , 3 , 2 ) ; $ b = array ( 3 , 1 , 2 ) ; $ n = sizeof ( $ a ) ; $ m = sizeof ( $ b ) ; if ( arePermutations ( $ a , $ b , $ n , $ m ) ) echo " Yes " . " STRNEWLINE " ; else echo " No " . " STRNEWLINE " ;