Related Articles
Maximum sum of lengths of non-overlapping subarrays with k as the max element.
• Difficulty Level : Medium
• Last Updated : 08 Feb, 2021

Find the maximum sum of lengths of non-overlapping subarrays (contiguous elements) with k as the maximum element.
Examples:

Input : arr[] = {2, 1, 4, 9, 2, 3, 8, 3, 4}
k = 4
Output : 5
{2, 1, 4} => Length = 3
{3, 4} => Length = 2
So, 3 + 2 = 5 is the answer

Input : arr[] = {1, 2, 3, 2, 3, 4, 1}
k = 4
Output : 7
{1, 2, 3, 2, 3, 4, 1} => Length = 7

Input : arr = {4, 5, 7, 1, 2, 9, 8, 4, 3, 1}
k = 4
Ans = 4
{4} => Length = 1
{4, 3, 1} => Length = 3
So, 1 + 3 = 4 is the answer

Algorithm :

Traverse the array starting from first element
Take a loop and keep on incrementing count
If element is less than equal to k
if array element is equal to k, then mark
a flag

Take another loop and traverse the array
till element is greater than k
return ans

## C++

 // CPP program to calculate max sum lengths of// non overlapping contiguous subarrays with k as// max element#include using namespace std;  // Returns max sum of lengths with maximum element// as kint calculateMaxSumLength(int arr[], int n, int k){    int ans = 0; // final sum of lengths      // number of elements in current subarray    int count = 0;      // variable for checking if k appeared in subarray    int flag = 0;      for (int i = 0; i < n;) {        count = 0;        flag = 0;          // count the number of elements which are        // less than equal to k        while (arr[i] <= k && i < n) {            count++;            if (arr[i] == k)                flag = 1;            i++;        }          // if current element appeared in current        // subarray add count to sumLength        if (flag == 1)            ans += count;             // skip the array elements which are        // greater than k        while (arr[i] > k && i < n)            i++;        }    return ans;}  // driver programint main(){    int arr[] = { 4, 5, 7, 1, 2, 9, 8, 4, 3, 1 };    int size = sizeof(arr) / sizeof(arr[0]);    int k = 4;    int ans = calculateMaxSumLength(arr, size, k);    cout << "Max Length :: " << ans << endl;    return 0;}

## Java

 // A Java program to calculate max sum lengths of// non overlapping contiguous subarrays with k as// max elementpublic class GFG{    // Returns max sum of lengths with maximum element    // as k    static int calculateMaxSumLength(int arr[], int n, int k) {        int ans = 0; // final sum of lengths         // number of elements in current subarray        int count = 0;         // variable for checking if k appeared in subarray        int flag = 0;         for (int i = 0; i < n;) {            count = 0;            flag = 0;             // count the number of elements which are            // less than equal to k            while (i < n && arr[i] <= k) {                count++;                if (arr[i] == k)                    flag = 1;                i++;            }             // if current element appeared in current            // subarray add count to sumLength            if (flag == 1)                ans += count;             // skip the array elements which are            // greater than k            while (i < n && arr[i] > k)                i++;        }        return ans;    }     // driver program to test above method    public static void main(String[] args) {         int arr[] = { 4, 5, 7, 1, 2, 9, 8, 4, 3, 1 };        int size = arr.length;        int k = 4;        int ans = calculateMaxSumLength(arr, size, k);        System.out.println("Max Length :: " + ans);    }}// This code is contributed by Sumit Ghosh

## Python

 # Python program to calculate max sum lengths of non# overlapping contiguous subarrays with k as max element # Returns max sum of lengths with max elements as kdef calculateMaxSumLength(arr, n, k):    ans = 0 # final sum of lengths    i=0    while i < n :                 # number of elements in current sub array        count = 0                 # Variable for checking if k appeared in the sub array        flag = 0                 # Count the number of elements which are        # less than or equal to k        while i < n and arr[i] <= k :            count = count + 1            if arr[i] == k:                flag = 1            i = i + 1                     # if current element appeared in current        # subarray and count to sumLength        if flag == 1:            ans = ans + count                     # skip the array elements which are greater than k        while i < n and arr[i] > k :            i = i + 1                  return ans     # Driver Programarr = [4, 5, 7, 1, 2, 9, 8, 4, 3, 1]size = len(arr)k = 4ans = calculateMaxSumLength(arr, size, k)print "Max Length ::",ans # Contributed by Rohit

## C#

 // A C# program to calculate max// sum lengths of non overlapping// contiguous subarrays with k as// max elementusing System;class GFG {         // Returns max sum of lengths    // with maximum element as k    static int calculateMaxSumLength(int []arr,                                     int n,                                     int k)    {                 // final sum of lengths        int ans = 0;         // number of elements in        // current subarray        int count = 0;         // variable for checking if        // k appeared in subarray        int flag = 0;         for(int i = 0; i < n;)        {            count = 0;            flag = 0;             // count the number of            // elements which are            // less than equal to k            while (i < n && arr[i] <= k)            {                count++;                if (arr[i] == k)                    flag = 1;                i++;            }             // if current element            // appeared in current            // subarray add count            // to sumLength            if (flag == 1)                ans += count;             // skip the array            // elements which are            // greater than k            while (i < n && arr[i] > k)                i++;        }        return ans;    }     // Driver Code    public static void Main()    {        int []arr = {4, 5, 7, 1, 2, 9, 8, 4, 3, 1};        int size = arr.Length;        int k = 4;        int ans = calculateMaxSumLength(arr, size, k);        Console.WriteLine("Max Length :: " + ans);    }} // This code is contributed by anuj_67.

## PHP

 \$k && \$i < \$n)            \$i++;        }    return \$ans;} // Driver Code\$arr = array( 4, 5, 7, 1, 2,                 9, 8, 4, 3, 1 );\$size = sizeof(\$arr);\$k = 4;\$ans = calculateMaxSumLength(\$arr, \$size, \$k);echo "Max Length :: " . \$ans . "\n"; // This code is contributed by ita_c?>

Output:

Max Length :: 4

Time Complexity : O(n)
It may look like O(n2), but if you take a closer look, array is traversed only once
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up