Count of subarrays with largest element at least twice the largest of remaining elements | Java program for the above approach ; Function to find count of subarrays which have max element greater than twice maximum of all other elements ; Stores the count of subarrays ; Generate all possible subarrays ; Stores the maximum element of the subarray ; Stores the maximum of all other elements ; Find the maximum element in the subarray [ i , j ] ; Find the maximum of all other elements ; If the maximum of subarray is greater than twice the maximum of other elements ; Print the maximum value obtained ; Driver code
import java . io . * ; class GFG { public static void countSubarray ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int mxSubarray = 0 ; int mxOther = 0 ; for ( int k = i ; k <= j ; k ++ ) { mxSubarray = Math . max ( mxSubarray , arr [ k ] ) ; } for ( int k = 0 ; k < i ; k ++ ) { mxOther = Math . max ( mxOther , arr [ k ] ) ; } for ( int k = j + 1 ; k < n ; k ++ ) { mxOther = Math . max ( mxOther , arr [ k ] ) ; } if ( mxSubarray > ( 2 * mxOther ) ) count ++ ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 10 , 9 , 7 , 3 } ; int N = arr . length ; countSubarray ( arr , N ) ; } }