Longest sub | Function to return the length of the largest subarray with maximum possible GCD ; To store the maximum number present in the array ; Finding the maximum element ; To store the final answer ; Two pointer ; Running a loop from j = i ; Condition for incrementing ' j ' ; Updating the answer ; Driver code
function findLength ( arr , n ) { var x = 0 ; for ( var i = 0 ; i < n ; i ++ ) x = Math . max ( x , arr [ i ] ) ; var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != x ) continue ; var j = i ; while ( arr [ j ] == x ) j ++ ; ans = Math . max ( ans , j - i ) ; } return ans ; } var arr = [ 1 , 2 , 2 ] ; var n = arr . length ; document . write ( findLength ( arr , n ) ) ;