text: Count of subarrays with largest element at least twice the largest of remaining elements | Function to find count of subarrays which have max element greater than twice maximum of all other elements ; Stores the maximum element of the array ; Traverse the given array ; If the value of 2 * arr [ i ] is greater than mx ; Update the value of L and break out of loop ; If the value 2 * arr [ i ] is greater than mx ; Update the value of R and break out of loop ; Print the final answer ; Driver Code
code: function countSubarray ( arr , n ) { var count = 0 , L = 0 , R = 0 ; var mx = Math . max . apply ( null , arr ) ; var i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] * 2 > mx ) { L = i ; break ; } } for ( i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] * 2 > mx ) { R = i ; break ; } } document . write ( ( L + 1 ) * ( n - R ) ) ; } var arr = [ 1 , 6 , 10 , 9 , 7 , 3 ] var N = arr . length ; countSubarray ( arr , N ) ;