Largest subsequence having GCD greater than 1 | Returns length of the largest subsequence with GCD more than 1. ; Finding the Maximum value in arr ; Iterate from 2 to maximum possible divisor of all give values ; If we found divisor , increment count ; Driver program to test above
function largestGCDSubsequence ( arr , n ) { var ans = 0 ; var maxele = Math . max ( ... arr ) ; for ( var i = 2 ; i <= maxele ; ++ i ) { var count = 0 ; for ( j = 0 ; j < n ; ++ j ) { if ( arr [ j ] % i == 0 ) ++ count ; } ans = Math . max ( ans , count ) ; } return ans ; } var arr = [ 3 , 6 , 2 , 5 , 4 ] ; var size = arr . length ; document . write ( largestGCDSubsequence ( arr , size ) ) ;