Maximum sum of lengths of non-overlapping subarrays with k as the max element.
Last Updated : 12 Oct, 2022
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
Implementation:
C++
#include <bits/stdc++.h> using namespace std; int calculateMaxSumLength( int arr[], int n, int k) { int ans = 0; int count = 0; int flag = 0; for ( int i = 0; i < n;) { count = 0; flag = 0; while (arr[i] <= k && i < n) { count++; if (arr[i] == k) flag = 1; i++; } if (flag == 1) ans += count; while (arr[i] > k && i < n) i++; } return ans; } 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
public class GFG { static int calculateMaxSumLength( int arr[], int n, int k) { int ans = 0 ; int count = 0 ; int flag = 0 ; for ( int i = 0 ; i < n;) { count = 0 ; flag = 0 ; while (i < n && arr[i] <= k) { count++; if (arr[i] == k) flag = 1 ; i++; } if (flag == 1 ) ans += count; while (i < n && arr[i] > k) i++; } return ans; } 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); } } |
Python3
def calculateMaxSumLength(arr, n, k): ans = 0 i = 0 while i < n : count = 0 flag = 0 while i < n and arr[i] < = k : count = count + 1 if arr[i] = = k: flag = 1 i = i + 1 if flag = = 1 : ans = ans + count while i < n and arr[i] > k : i = i + 1 return ans 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) |
C#
using System; class GFG { static int calculateMaxSumLength( int []arr, int n, int k) { int ans = 0; int count = 0; int flag = 0; for ( int i = 0; i < n;) { count = 0; flag = 0; while (i < n && arr[i] <= k) { count++; if (arr[i] == k) flag = 1; i++; } if (flag == 1) ans += count; while (i < n && arr[i] > k) i++; } return ans; } 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); } } |
PHP
<?php function calculateMaxSumLength(& $arr , $n , $k ) { $ans = 0; $count = 0; $flag = 0; for ( $i = 0; $i < $n 😉 { $count = 0; $flag = 0; while ( $arr [ $i ] <= $k && $i < $n ) { $count ++; if ( $arr [ $i ] == $k ) $flag = 1; $i ++; } if ( $flag == 1) $ans += $count ; while ( $arr [ $i ] > $k && $i < $n ) $i ++; } return $ans ; } $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" ; ?> |
Javascript
<script> function calculateMaxSumLength(arr,n,k) { let ans = 0; let count = 0; let flag = 0; for (let i = 0; i < n;) { count = 0; flag = 0; while (i < n && arr[i] <= k) { count++; if (arr[i] == k) flag = 1; i++; } if (flag == 1) ans += count; while (i < n && arr[i] > k) i++; } return ans; } let arr=[4, 5, 7, 1, 2, 9, 8, 4, 3, 1]; let size = arr.length; let k = 4; let ans = calculateMaxSumLength(arr, size, k); document.write( "Max Length :: " + ans); </script> |
Time Complexity: O(n), It may look like O(n2), but if you take a closer look, array is traversed only once
Auxiliary Space: O(1)
Another approach:
Algorithm:
Traverse the array from first element to last element if the element is less than k increment the count if the element is equals to k if k is not found increment the count and mark flag as 1 if k is found add the value of count to ans and mark count as 1 if the element is greater than k if k is present in the subarray add the value of count to ans and assign value of count and flag variables as 0 finally check again if k value is found in subarray or not if k is found return sum of answer and count if not return ans
Implementation:
C++
#include <bits/stdc++.h> using namespace std; int calculateMaxSumLength( int arr[], int n, int k) { int ans = 0; int count = 0; int flag = 0; for ( int i = 0; i < n; i++) { if (arr[i] < k) { count++; } else if (arr[i] == k) { if (flag == 0) { count++; flag = 1; } else { ans += count; count = 1; } } else { if (flag == 1) { ans += count; } count = 0; flag = 0; } } if (flag == 1) { return ans + count; } return ans; } 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
class GFG { static int calculateMaxSumLength( int arr[], int n, int k) { int ans = 0 ; int count = 0 ; int flag = 0 ; for ( int i = 0 ; i < n; i++) { if (arr[i] < k) { count++; } else if (arr[i] == k) { if (flag == 0 ) { count++; flag = 1 ; } else { ans += count; count = 1 ; } } else { if (flag == 1 ) { ans += count; } count = 0 ; flag = 0 ; } } if (flag == 1 ) { return ans + count; } return ans; } 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); } } |
Python3
def calculateMaxSumLength(arr, n, k): ans = 0 count = 0 flag = 0 for i in range (n): if arr[i] < k: count = count + 1 elif arr[i] = = k: if flag = = 0 : count = count + 1 flag = 1 else : ans = ans + count count = 1 else : if flag = = 1 : ans = ans + count count = 0 flag = 0 if flag = = 1 : return ans + count return ans 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) |
C#
using System; class GFG { static int calculateMaxSumLength( int [] arr, int n, int k) { int ans = 0; int count = 0; int flag = 0; for ( int i = 0; i < n; i++) { if (arr[i] < k) { count++; } else if (arr[i] == k) { if (flag == 0) { count++; flag = 1; } else { ans += count; count = 1; } } else { if (flag == 1) { ans += count; } count = 0; flag = 0; } } if (flag == 1) { return ans + count; } return ans; } 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); } } |
Javascript
<script> function calculateMaxSumLength(arr, n, k) { let ans = 0; let count = 0; let flag = 0; for (let i = 0; i < n; i++) { if (arr[i] < k) { count++; } else if (arr[i] == k) { if (flag == 0) { count++; flag = 1; } else { ans += count; count = 1; } } else { if (flag == 1) { ans += count; } count = 0; flag = 0; } } if (flag == 1) { return ans + count; } return ans; } let arr = [ 4, 5, 7, 1, 2, 9, 8, 4, 3, 1 ]; let size = arr.length; let k = 4; let ans = calculateMaxSumLength(arr, size, k); document.write( "Max Length :: " ,ans, "</br>" ); </script> |
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Maximum Sum of two non-overlapping Subarrays of any length
Given an array A consisting of N integers, the task is to find the maximum sum of two non-overlapping subarrays of any length of the array. Note: You can select empty subarrays also. Examples: Input: N = 3, A[] = {-4, -5, -2}Output: 0Explanation: Two empty subarrays are optimal with maximum sum = 0.
6 min read
Maximum sum of non-overlapping subarrays of length atmost K
Given an integer array 'arr' of length N and an integer 'k', select some non-overlapping subarrays such that each sub-array if of length at most 'k', no two sub-arrays are adjacent and sum of all the elements of the selected sub-arrays are maximum.Examples: Input : arr[] = {-1, 2, -3, 4, 5}, k = 2 O
10 min read
Maximize the sum of maximum elements of at least K-sized subarrays
Given an integer array arr[] of length N and an integer K, partition the array in some non-overlapping subarrays such that each subarray has size at least K and each element of the array should be part of a subarray. The task is to maximize the sum of maximum elements across all the subarrays. Examp
7 min read
Maximize count of non-overlapping subarrays with sum K
Given an array arr[] and an integer K, the task is to print the maximum number of non-overlapping subarrays with a sum equal to K. Examples: Input: arr[] = {-2, 6, 6, 3, 5, 4, 1, 2, 8}, K = 10Output: 3Explanation: All possible non-overlapping subarrays with sum K(= 10) are {-2, 6, 6}, {5, 4, 1}, {2,
6 min read
Maximum sum two non-overlapping subarrays of given size
Given an array, we need to find two subarrays with a specific length K such that sum of these subarrays is maximum among all possible choices of subarrays. Examples: Input : arr[] = [2, 5, 1, 2, 7, 3, 0] K = 2 Output : 2 5 7 3 We can choose two arrays of maximum sum as [2, 5] and [7, 3], the sum of
12 min read
Maximum sum of at most K non-overlapping Subarray
Given an array, arr[] of size N, the task is to find the sum of the at most K non-overlapping contiguous subarray within an arr[] with the maximum sum. Examples: Input: arr[] = [4, 1, -3, 7, -5, 6, -2, 1], K = 3Output: 18Explanation: In the above input, the maximum k subarray sum is 18 and the subar
15 min read
Max sum of M non-overlapping subarrays of size K
Given an array and two numbers M and K. We need to find the max sum of sums of M subarrays of size K (non-overlapping) in the array. (Order of array remains unchanged). K is the size of subarrays and M is the count of subarray. It may be assumed that size of array is more than m*k. If total array si
15+ min read
Maximum non overlapping Subset with sum greater than K
Given an array, arr[] and integer K, the task is to find the maximum number of non-overlapping subsets such that the sum of the subsets is strictly greater than K when you may also change the value of each element to the maximum value of the particular subset. Examples: Input: arr[]= {90, 80, 70, 60
5 min read
Maximum sum of subarrays having distinct elements of length K
Given an array, arr[] and a value k, represent the length of the subarray to be considered. Find the maximum sum that can be obtained from the subarray of length k such that each element of the subarray is unique. If there is no subarray that meets the required condition then return 0. Examples: Inp
13 min read
Count non-overlapping Subarrays of size K with equal alternate elements
Given an array arr[] of length N, the task is to find the count of non-overlapping subarrays of size K such that the alternate elements are equal. Examples: Input: arr[] = {2, 4, 2, 7}, K = 3Output: 1Explanation: Given subarray {2, 4, 2} is a valid array because the elements in even position(index n
7 min read