Maximum sum of lengths of non-overlapping subarrays with k as the max element.

2.2

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

 
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

Example 2:

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

Example 3:

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

question source : http://www.geeksforgeeks.org/amazon-interview-experience-set-376-campus-internship/

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
   
   If flag is marked, add this count to answer
   
   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 <bits/stdc++.h>
using namespace std;
 
// Returns max sum of lengths with maximum element
// as k
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 (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 program
int 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 element
public 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 k
def calculateMaxSumLength(arr, n, k):
    ans = 0 # final sum of lengths
    
    for i in range(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 Program
arr = [4, 5, 7, 1, 2, 9, 8, 4, 3, 1]
size = len(arr)
k = 4
ans = calculateMaxSumLength(arr, size, k)
print "Max Length ::",ans

# Contributed by Harshit Agrawal


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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.2 Average Difficulty : 2.2/5.0
Based on 8 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.