Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6424 |

@blackshadows | 6380 |

@mb1973 | 5704 |

@Quandray | 5245 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5046 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@mantu_singh | 3532 |

@marius_valentin_dragoi | 3523 |

@sushant_a | 3459 |

Complete Leaderboard | |

Handle | Score |

@cfwong8 | 1160 |

@gurshehzadsingh | 1052 |

@nithinreddy3210 | 801 |

@mohanreddy8847 | 702 |

@balwanyadav34 | 622 |

@satyamchhatrala | 616 |

@pritsahkar2000 | 614 |

@AkashLahoty | 606 |

@amangiri168 | 596 |

@iamchaitanyahegde | 589 |

@iib2019024 | 587 |

Complete Leaderboard |

Given an array **arr[]** of **N** elements, the task is to find the maximum sum of lengths of all non-overlapping subarrays with **K** as the maximum element in the subarray.

**Example 1:**

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

**Example 2:**

**Input: **N = 7, K = 4
arr[] = {1, 2, 3, 2, 3, 4, 1}
**Output:** 7
**Explanation:** {1, 2, 3, 2, 3, 4, 1}
=> Length = 7.

**Your Task:**

This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function **calculateMaxSumLength**() that takes array **arr,** integer** N**, and integer** K **as parameters and returns the sum of the length of all such subarrays.

**Expected Time Complexity:** O(N).

**Expected Auxiliary Space:** O(1).

**Constraints:**

1 ≤ N ≤ 10^{6}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Sum of Lengths of Non-Overlapping SubArrays

...