Find an integer that is common in the maximum number of given arithmetic progressions | Javascript implementation of the approach ; Function to return element common in maximum number of APs ; Initialize the count variable ; Increment count for every element of an AP ; Find the index with maximum count ; Return the maximum common element ; Driver code
var MAXN = 1000000 ; function maxCommonElement ( A , D , N ) { var cnt = Array ( MAXN ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = A [ i ] ; j < MAXN ; j += D [ i ] ) cnt [ j ] ++ ; } var ans = 0 ; var com = 0 ; for ( var i = 0 ; i < MAXN ; i ++ ) { if ( cnt [ i ] > ans ) { ans = cnt [ i ] ; com = i ; } } return com ; } var A = [ 13 , 1 , 2 , 5 ] , D = [ 5 , 10 , 1 , 12 ] ; var N = A . length ; document . write ( maxCommonElement ( A , D , N ) ) ;