Count Non | Function to find the gcd of the two numbers ; Function to find distinct elements in the array by repeatidely inserting the absolute difference of all possible pairs ; Stores largest element of the array ; Traverse the array , arr ; Update max_value ; Stores GCD of array ; Traverse the array , arr ; Update GCDArr ; Stores distinct elements in the array by repeatidely inserting absolute difference of all possible pairs ; Given array arr
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function DistinctValues ( arr , N ) { var max_value = Number . MIN_VALUE ; for ( i = 0 ; i < N ; ++ i ) { max_value = Math . max ( max_value , arr [ i ] ) ; } var GCDArr = arr [ 0 ] ; for ( i = 1 ; i < N ; ++ i ) { GCDArr = gcd ( GCDArr , arr [ i ] ) ; } var answer = ( max_value / GCDArr ) + 1 ; return answer ; } var arr = [ 4 , 12 , 16 , 24 ] ; var N = arr . length ; document . write ( DistinctValues ( arr , N ) ) ;