Questions tagged [maximum-subarray]

36 questions
10
votes
2 answers

What is a contiguous subarray?

What is a contiguous subarray? I have tried searching online and have found pages about solving the largest contiguous subarray problem but none with a definition or explanation of what contiguous in this actually is. Ex: Wikipedia: Maximm subarray…
ansonl
  • 201
  • 1
  • 2
  • 5
8
votes
1 answer

Maximum sub-matrix sum

Given a $n\times m$ matrix $A$ of integers, find a sub-matrix whose sum is maximal. If there is one row only or one column only, then this is equivalent to finding a maximum sub-array. The 1D version can be solved in linear time by dynamic…
mrk
  • 3,748
  • 23
  • 35
5
votes
0 answers

Subarray whose (sum × length) is maximum

Consider the following problem: Given an array of $n$ integer numbers (positive and negative), find a (contiguous) subarray for which the product $(\text{sum of the elements})\times(\text{length of the subarray})$ is maximum. Is there an…
obag
  • 51
  • 1
4
votes
1 answer

Find two contiguous subarrays with the greatest difference

Here's an interview question I've seen on a few sites. People claim that an O(n) solution is possible, but I've been racking my brain these last 2 days and I couldn't come up with a solution, nor find one anywhere on the web. Given an array of…
Ram Rachum
  • 143
  • 3
4
votes
1 answer

Maximum subarray of bounded length

I'm giving an array $A[0..n-1]$ and an integer $w$. The goal is to find indices $i,j$ that maximize $$\Phi(i,j) = A[i] + A[i+1] + \dots + A[j-1],$$ subject to the requirements that $0 \le i \le j \le n$ and $j \le i+w$. Or, in other words, I want…
D.W.
  • 167,959
  • 22
  • 232
  • 500
2
votes
0 answers

Maximal subarray problem with length constraint

I was recently faced with the following interview question: Given an array A, and an integer k, find a contiguous subarray with a maximal sum, with the added constraint this subarray has length at most k. So, if $A=[8, -1, -1, 4, -2, -3, 5, 6,…
Steve D
  • 166
  • 5
2
votes
0 answers

Hardness of a maximum contiguous subarray sum for a sparse multi-dimensional array

Suppose we have a d-dimensional array A (d > 1) where each dimension has length n. The array is given in sparse notation as input, and the number of given non-zero elements is N. We want to find a contiguous range of indices for each dimension such…
user2566092
  • 1,741
  • 10
  • 18
2
votes
2 answers

Find the length of the longest subarray having sum greater than k

I tried to solve this problem but could not do it better than $O(n^2)$. My Algorithm: 1. Calculate prefix sum 2. for i in 1..n: for j in 1..i: if presum[i] - presum[j-1] > k: ans = max(ans, i - j) Edit: I have found a…
Tijal
  • 21
  • 3
2
votes
2 answers

What is exactly an empty Sub-array

I read the question in Exercise 4.1-4 in Introduction To Algorithms: Suppose we change the definition of the maximum-subarray problem to allow the result to be an empty subarray, where the sum of the values of an empty subarray is 0. How would you…
Sachin Bahukhandi
  • 247
  • 1
  • 3
  • 13
2
votes
1 answer

Cannot understand the relevance of $\binom{n-1}{2}$ subarrays in The Maximum Sub-array Problem

I recently came across the sentence in the Book Introduction to Algorithms section 4.1 The maximum sub-array problem: We still need to check $\binom{n-1}{2} = \Theta(n^2)$ subarrays for a period of $n$ days. Here $n$ is the number of days taken as…
1
vote
2 answers

Understanding algorithm for maximum sum of non-consecutive elements

There is a well-known problem in CS of finding the maximum sum of non-consecutive integers in a list. There is even an SO post about how to solve it: https://stackoverflow.com/questions/4487438/maximum-sum-of-non-consecutive-elements That said, I…
resu
  • 111
  • 1
  • 3
1
vote
0 answers

Online algorithm to find shortest subarray containing all k-elements

We have given array of $n$ integers in the range $[1, k]$. We want to design algorithm that can modify the array and also find the length of the shortest subarray containing at least one of each integers in the range $[1,k]$. $k$ can go up to 50,…
someone12321
  • 1,428
  • 15
  • 27
1
vote
0 answers

Algorithm to split an array into minimal number of subarrays where sum of their elements is less than or equal a given one

Title of the question pretty much summarises the question. Here is a concrete example of what I'm trying to achieve with no success. Let's say we have a sorted list (but sorted is not a requirement) L = [1,3,20,100,150,200,260] and K = 260. I need a…
1
vote
1 answer

Find maximum non contiguous subarray that respect a specific rule

Given these two arrays: [5, 3, 4, 1, 2] [1, 3, 2, 4, 5] Find the maximum subsequence in both arrays that the index of the elements are in a crescent order: Example: [3, 4] it's an answer because the indexes are in a crescent way in both arrays.…
1
vote
0 answers

Hint(s) please: Maximum Subarray O(n) solution CLRS Exercise 4.1-5

I've been stuck on this problem for some time, and I don't want to just look at an answer. I'd like a hint(s) so that I can get myself thinking in the right direction so that I might gain more insight into solving similar types of algorithm…
user3773048
  • 151
  • 5
1
2 3