Longest Sub | C ++ implementation of the approach ; Function to return the max length of the sub - array that have the maximum average ( average value of the elements ) ; Finding the maximum value ; If consecutive maximum found ; Find the max length of consecutive max ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLenSubArr ( int a [ ] , int n ) { int count , j ; int cm = 1 , max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; } for ( int i = 0 ; i < n - 1 ; ) { count = 1 ; if ( a [ i ] == a [ i + 1 ] && a [ i ] == max ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ j ] == max ) { count ++ ; i ++ ; } else break ; } if ( count > cm ) cm = count ; } else i ++ ; } return cm ; } int main ( ) { int arr [ ] = { 6 , 1 , 6 , 6 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxLenSubArr ( arr , n ) ; return 0 ; }