Maximum sum of lengths of non-overlapping subarrays with k as the max element.
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
question source : https://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 = 4ans = calculateMaxSumLength(arr, size, k) print "Max Length ::",ans # Contributed by Harshit Agrawal |
C#
// A C# program to calculate max // sum lengths of non overlapping // contiguous subarrays with k as // max element using 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
<?php // PHP program to calculate max sum lengths // of non overlapping contiguous subarrays // with k as max element // Returns max sum of lengths with maximum // element as k function calculateMaxSumLength(&$arr, $n, $k) { $ans = 0; // final sum of lengths // number of elements in current subarray $count = 0; // variable for checking if k // appeared in subarray $flag = 0; for ($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 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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Count of subarrays whose maximum element is greater than k
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Maximum element in an array such that its previous and next element product is maximum
- Sum of minimum element of all subarrays of a sorted array
- Maximum of all Subarrays of size k using set in C++ STL
- Partitioning into two contiguous element subarrays with equal sums
- Sum of maximum of all subarrays | Divide and Conquer
- Maximum sum two non-overlapping subarrays of given size
- Number of subarrays whose minimum and maximum are same
- Find an element which divides the array in two subarrays with equal product
- Sum of minimum and maximum elements of all subarrays of size k.
- Maximum sum of non-overlapping subarrays of length atmost K
- Number of subarrays with maximum values in given range



