BackmediumArraysUberTCS

Subsequence Sum Threshold Solution

Problem Statement

You are given an array of integers readings and two integers threshold and min_length. Find the maximum sum of a subsequence in readings that does not exceed max_length elements, where max_length is 50, and contains at least min_length elements greater than threshold, where threshold is 200 and min_length is 10.

Example 1
Input
[250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300]
Output
550

Explanation: Step-by-step: with input [250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300], we first sort the array in descending order to get [300, 300, 300, 250, 250, 250, 250, 250, 250, 200, 200]. Then, we initialize a variable max_sum to 0 and a variable count to 0. We iterate through the sorted array, and for each element, we check if adding it to the current subsequence would exceed the max_length of 50. If it does, we reset the subsequence to the current element. We also check if the current element is greater than the threshold of 200 and increment the count if it is. If the count is at least the min_length of 10, we update max_sum with the maximum of max_sum and the sum of the current subsequence. Finally, we return max_sum.

Example 2
Input
[250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300]
Output
700

Explanation: Step-by-step: with input [250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300], we first sort the array in descending order to get [300, 300, 300, 250, 250, 250, 250, 250, 250, 250, 250, 200, 200, 200, 200, 200, 200, 200, 200, 200]. Then, we initialize a variable max_sum to 0 and a variable count to 0. We iterate through the sorted array, and for each element, we check if adding it to the current subsequence would exceed the max_length of 50. If it does, we reset the subsequence to the current element. We also check if the current element is greater than the threshold of 200 and increment the count if it is. If the count is at least the min_length of 10, we update max_sum with the maximum of max_sum and the sum of the current subsequence. Finally, we return max_sum.

Constraints

  • The length of the input array will not exceed 1000.
  • The values in the input array will be between 0 and 1000.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Subsequence Sum Threshold — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(n)
|
SpaceO(n)

Problem Description

You are given an array of integers readings and two integers threshold and min_length. Find the maximum sum of a subsequence in readings that does not exceed max_length elements, where max_length is 50, and contains at least min_length elements greater than threshold, where threshold is 200 and min_length is 10.

Examples

Example 1

Input

[250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300]

Output

550

Explanation: Step-by-step: with input [250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300], we first sort the array in descending order to get [300, 300, 300, 250, 250, 250, 250, 250, 250, 200, 200]. Then, we initialize a variable max_sum to 0 and a variable count to 0. We iterate through the sorted array, and for each element, we check if adding it to the current subsequence would exceed the max_length of 50. If it does, we reset the subsequence to the current element. We also check if the current element is greater than the threshold of 200 and increment the count if it is. If the count is at least the min_length of 10, we update max_sum with the maximum of max_sum and the sum of the current subsequence. Finally, we return max_sum.

Example 2

Input

[250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300]

Output

700

Explanation: Step-by-step: with input [250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300, 200, 250, 300], we first sort the array in descending order to get [300, 300, 300, 250, 250, 250, 250, 250, 250, 250, 250, 200, 200, 200, 200, 200, 200, 200, 200, 200]. Then, we initialize a variable max_sum to 0 and a variable count to 0. We iterate through the sorted array, and for each element, we check if adding it to the current subsequence would exceed the max_length of 50. If it does, we reset the subsequence to the current element. We also check if the current element is greater than the threshold of 200 and increment the count if it is. If the count is at least the min_length of 10, we update max_sum with the maximum of max_sum and the sum of the current subsequence. Finally, we return max_sum.

Constraints

  • The length of the input array will not exceed 1000.
  • The values in the input array will be between 0 and 1000.

Optimal Approach & Strategy

The optimized approach utilizes a sliding window technique, tracking the sum of the current subsequence and adjusting the window boundaries as necessary. This approach maintains a running sum and resets it when the subsequence exceeds the given constraints, resulting in a more efficient solution.

Brute Force Approach

The brute-force approach involves checking every possible subsequence, which results in a time complexity of O(n²). This is done by iterating over the array and generating all possible subsequences, then checking each one against the given constraints. However, this approach is inefficient and should be avoided for large inputs.

Verified Code Solutions

No solution currently available for JavaScript. Select another language tab above.

Asked in Top Tech Interviews

UberTCS

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.