Longest Sub-array with maximum average value Last Updated : 22 Jun, 2022 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of n integers. The task is to find the maximum length of the sub-array which has the maximum average value (average of the elements of the sub-array). Examples: Input: arr[] = {2, 3, 4, 5, 6} Output: 1 {6} is the required sub-arrayInput: arr[] = {6, 1, 6, 6, 0} Output: 2 {6} and {6, 6} are the sub-arrays with maximum average value. Approach: Average of any sub-array cannot exceed the maximum value of the array.The possible maximum value of the average will be the maximum element from the array.So to find the maximum length sub-array with the maximum average value, we have to find the max length of the sub-array where every element of the sub-array is same and equal to the maximum element from the array. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the max length of the // sub-array that have the maximum average // (average value of the elements) int maxLenSubArr(int a[], int n) { int count, j; int cm = 1, max = 0; // Finding the maximum value for (int i = 0; i < n; i++) { if (a[i] > max) max = a[i]; } for (int i = 0; i < n - 1;) { count = 1; // If consecutive maximum found if (a[i] == a[i + 1] && a[i] == max) { // Find the max length of consecutive 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; } // Driver code int main() { int arr[] = { 6, 1, 6, 6, 0 }; int n = sizeof(arr) / sizeof(arr[0]); cout << maxLenSubArr(arr, n); return 0; } Java // Java implementation of the approach class GFG { // Function to return the max length of the // sub-array that have the maximum average // (average value of the elements) static int maxLenSubArr(int a[], int n) { int count, j; int cm = 1, max = 0; // Finding the maximum value for (int i = 0; i < n; i++) { if (a[i] > max) max = a[i]; } for (int i = 0; i < n - 1; ) { count = 1; // If consecutive maximum found if (a[i] == a[i + 1] && a[i] == max) { // Find the max length of consecutive 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; } // Driver code public static void main(String[] args) { int arr[] = { 6, 1, 6, 6, 0 }; int n = arr.length; System.out.println(maxLenSubArr(arr, n)); } } // This code is contributed by Code_Mech. Python3 # Python3 implementation of the approach # Function to return the max length of the # sub-array that have the maximum average # (average value of the elements) def maxLenSubArr(a, n): cm, Max = 1, 0 # Finding the maximum value for i in range(0, n): if a[i] > Max: Max = a[i] i = 0 while i < n - 1: count = 1 # If consecutive maximum found if a[i] == a[i + 1] and a[i] == Max: # Find the max length of # consecutive max for j in range(i + 1, n): if a[j] == Max: count += 1 i += 1 else: break if count > cm: cm = count else: i += 1 i += 1 return cm # Driver code if __name__ == "__main__": arr = [6, 1, 6, 6, 0] n = len(arr) print(maxLenSubArr(arr, n)) # This code is contributed by # Rituraj Jain C# // C# implementation of the approach using System; class GFG { // Function to return the max length of the // sub-array that have the maximum average // (average value of the elements) static int maxLenSubArr(int []a, int n) { int count, j; int cm = 1, max = 0; // Finding the maximum value for (int i = 0; i < n; i++) { if (a[i] > max) max = a[i]; } for (int i = 0; i < n - 1; ) { count = 1; // If consecutive maximum found if (a[i] == a[i + 1] && a[i] == max) { // Find the max length of consecutive 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; } // Driver code static public void Main () { int []arr = { 6, 1, 6, 6, 0 }; int n = arr.Length; Console.WriteLine(maxLenSubArr(arr, n)); } } // This code is contributed by ajit. PHP <?php // PHP implementation of the approach // Function to return the max length of the // sub-array that have the maximum average // (average value of the elements) function maxLenSubArr($a, $n) { $cm = 1 ; $max = 0; // Finding the maximum value for ($i = 0; $i < $n; $i++) { if ($a[$i] > $max) $max = $a[$i]; } for ($i = 0; $i < $n - 1;) { $count = 1; // If consecutive maximum found if ($a[$i] == $a[$i + 1] && $a[$i] == $max) { // Find the max length of // consecutive 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; } // Driver code $arr = array( 6, 1, 6, 6, 0 ); $n = sizeof($arr); echo maxLenSubArr($arr, $n); // This code is contributed by Ryuga ?> JavaScript <script> // Javascript implementation of the approach // Function to return the max length of the // sub-array that have the maximum average // (average value of the elements) function maxLenSubArr(a, n) { let count, j; let cm = 1, max = 0; // Finding the maximum value for (let i = 0; i < n; i++) { if (a[i] > max) max = a[i]; } for (let i = 0; i < n - 1; ) { count = 1; // If consecutive maximum found if (a[i] == a[i + 1] && a[i] == max) { // Find the max length of consecutive 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; } let arr = [ 6, 1, 6, 6, 0 ]; let n = arr.length; document.write(maxLenSubArr(arr, n)); </script> Output: 2 Time Complexity: O(n2) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Longest Sub-array with maximum average value S SURENDRA_GANGWAR Follow Improve Article Tags : Mathematical DSA Arrays array-traversal-question Practice Tags : ArraysMathematical Similar Reads Longest Subarrays having each Array element as the maximum Given an array arr[] of length N, the task is to find the longest subarray for each array element arr[i], which contains arr[i] as the maximum. Examples: Input: arr[] = {1, 2, 3, 0, 1} Output: 1 2 5 1 2 Explanation: The longest subarray having arr[0] as the largest is {1} The longest subarray having 7 min read Longest alternating subsequence with maximum sum | Set 2 Given an array arr[] of size N, consisting of positive and negative integers, the task is to find the longest alternating subsequence(i.e. the sign of every element is opposite to that of its previous element) from the given array which has the maximum sum.Examples: Input: arr[] = {-2, 10, 3, -8, -4 7 min read Length of longest sub-array with maximum arithmetic mean. Given an array of n-elements find the longest sub-array with the greatest arithmetic mean. The length of the sub-array must be greater than 1 and the mean should be calculated as an integer only.Examples: Input : arr[] = {3, 2, 1, 2} Output : 2 sub-array 3, 2 has greatest arithmetic mean Input :arr[ 5 min read Longest subarray having maximum sum Given an array arr[] containing n integers. The problem is to find the length of the subarray having maximum sum. If there exists two or more subarrays with maximum sum then print the length of the longest subarray.Examples: Input : arr[] = {5, -2, -1, 3, -4}Output : 4There are two subarrays with ma 12 min read Longest subsequence having maximum sum Given an array arr[] of size N, the task is to find the longest non-empty subsequence from the given array whose sum is maximum. Examples: Input: arr[] = { 1, 2, -4, -2, 3, 0 } Output: 1 2 3 0 Explanation: Sum of elements of the subsequence {1, 2, 3, 0} is 6 which is the maximum possible sum. Theref 8 min read Like