BackmediumArraysPaytm

Circular Array Subset Sum Solution

Problem Statement

Given a circular array of integers weights and an integer capacity, find the maximum sum of a contiguous subarray within the circular array that does not exceed capacity.

Example 1
Input
[2, 3, 4, 1], 10
Output
10

Explanation: Step-by-step: Given the array [2, 3, 4, 1] and capacity 10, we consider all possible subarrays, including those that wrap around the end of the array. The maximum sum that does not exceed 10 is 10, which is the sum of the subarray [4, 1, 2, 3].

Example 2
Input
[3, 1, 2, 3], 6
Output
6

Explanation: Step-by-step: Given the array [3, 1, 2, 3] and capacity 6, we consider all possible subarrays, including those that wrap around the end of the array. The maximum sum that does not exceed 6 is 6, which is the sum of the subarray [1, 2, 3].

Constraints

  • 1 <= number of crates <= 10^5
  • 1 <= weight of each crate <= 10^6
  • 1 <= target storage capacity <= 10^7
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

Circular Array Subset Sum — Problem Statement & Solution Guide

ArraysMediumTwo Pointers
TimeO(n)
|
SpaceO(1)

Problem Description

Given a circular array of integers weights and an integer capacity, find the maximum sum of a contiguous subarray within the circular array that does not exceed capacity.

Examples

Example 1

Input

[2, 3, 4, 1], 10

Output

10

Explanation: Step-by-step: Given the array [2, 3, 4, 1] and capacity 10, we consider all possible subarrays, including those that wrap around the end of the array. The maximum sum that does not exceed 10 is 10, which is the sum of the subarray [4, 1, 2, 3].

Example 2

Input

[3, 1, 2, 3], 6

Output

6

Explanation: Step-by-step: Given the array [3, 1, 2, 3] and capacity 6, we consider all possible subarrays, including those that wrap around the end of the array. The maximum sum that does not exceed 6 is 6, which is the sum of the subarray [1, 2, 3].

Constraints

  • 1 <= number of crates <= 10^5
  • 1 <= weight of each crate <= 10^6
  • 1 <= target storage capacity <= 10^7

Optimal Approach & Strategy

The optimal approach involves using two pointers to track the current window of crates, allowing for a time complexity of O(n). By moving the pointers based on the current total weight, we can efficiently find the maximum total weight of crates that can be stored.

Brute Force Approach

A naive approach would involve checking all possible combinations of crates, resulting in a time complexity of O(n²). This can be improved by using dynamic programming to store intermediate results.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function circularArraySubsetSum(weights, capacity) {
  let maxSum = -Infinity;
  for (let i = 0; i < weights.length; i++) {
    let currentSum = 0;
    for (let j = i; j < weights.length + i; j++) {
      currentSum += weights[j % weights.length];
      if (currentSum > capacity) break;
      maxSum = Math.max(maxSum, currentSum);
    }
    currentSum = 0;
    for (let j = 0; j <= i; j++) {
      currentSum += weights[j % weights.length];
      if (currentSum > capacity) break;
      maxSum = Math.max(maxSum, currentSum);
    }
  }
  return maxSum;
}

Asked in Top Tech Interviews

Paytm

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.