I am trying to study contiguous subarray but I am not getting any study material which explains this concept.
But I found one example with says that
Given the array [-2,1,-3,4,-1,2,1,-5,4] the contiguous subarray is [4,-1,2,1]
Can anyone explain on what bases they are saying contiguous subarray is [4,-1,2,1]
It isn't the contiguous subarray, there are many. It's just a subsequence without skipping any elements. E.g. [-2, 1], [-2, 1, -3], [2, 1, -5] are all contiguous subarrays of this array, but [2, 1, 4] isn't.
A contiguous subarray is simply a subarray of an array with a condition that the elements of the subarray should be in exact sequence as the sequence of the elements in the array.
for example if the array is [1,2,3,4,5]
then [1,3,5] is a subarray of the array, but not a contiguous subarray since the sequence does not match as the elements 2 and 4 are skipped.
[1,2,3] will be one of the contiguous subarrays.
Note: So I kept my original answer and extra thoughts here: https://cs.stackexchange.com/questions/43303/what-is-a-contiguous-subarray/149711?noredirect=1#comment313804_149711
The actual definition of contiguous subarray (as others have answered) is any sub series of elements in a given array that are contiguous ie their indices are continuous.
So given [1,2,3,4,5,6]:
[1,2,3], [3,4], [3,4,5,6] are all valid contiguous subarrays. Any algorithm can be used to generate the subarrays.
Personal note: What was confusing for me was most explanations either use or reference a specific problem or condition set to generate the subarrays. Most don't specifically state there's no direct relationship between the problem and the definition of contiguous subarrays.
For me it's the same as asking:
Q: "What does + do?"
A: "4+4=8"
Me: "OK, so I can only use + with 4, got it."
On my travels I'd see answers like this:
basically the sum of contiguous array must be greater, if you will try to add the all elements of array it will give you lowest sum, but if you will add this specific range in continuity you will get the greatest sum we can make out of this array.
Which is referring to some specific problem that uses contiguous subarrays, but no one was calling this out, so to a noob you could infer that contiguous subarrays has some further meaning.
This is how my brain worked to grok the definition, so hopefully it helps someone else out.
To get it fast:
Any subarray sliced out from a given array [0,...,n] keeping original continuous order of elements in it.
Which starts at element with chosen index between 0 and n-1
and ends at other chosen element of a higher index than the starting one.
Containing all elements of indexes in the chosen range.
[-2,1,-3,4,-1,2,1,-5,4] is indexed [0,1,2,3,4,5,6,7,8]
[4,-1,2,1] is a contiguous subarray being a range indexed: [3,...,6]
basically the sum of contiguous array must be greater, if you will try to add the all elements of array it will give you lowest sum, but if you will add this specific range in continuity you will get the greatest sum we can make out of this array.
Related
For example, given an array
a = [1, 2, 3, 7, 8, 9]
and an integer
i = 2. Find maximal subarrays where the distance between the largest and the smallest elements is at most i. The output for the example above would be:
[1,2,3] [7,8,9]
The subarrays are maximal in the sense given two subarrays A and B. There exists no element b in B such that A + b satisfies the condition given. Does there exist a non-polynomial time algorithm for said problem ?
This problem might be solved in linear time using method of two pointers and two deques storing indices, the first deque keeps minimum, another keeps maximum in sliding window.
Deque for minimum (similar for maximum):
current_minimum = a[minq.front]
Adding i-th element of array: //at the right index
while (!minq.empty and a[minq.back] > a[i]):
//last element has no chance to become a minimum because newer one is better
minq.pop_back
minq.push_back(i)
Extracting j-th element: //at the left index
if (!minq.empty and minq.front == j)
minq.pop_front
So min-deque always contains non-decreasing sequence.
Now set left and right indices in 0, insert index 0 into deques, and start to move right. At every step add index in order into deques, and check than left..right interval range is good. When range becomes too wide (min-max distance is exceeded), stop moving right index, check length of the last good interval, compare with the best length.
Now move left index, removing elements from deques. When max-min becomes good, stop left and start with right again. Repeat until array end.
We know about an algorithm that will find the Longest Increasing subsequence in O(nlogn). I was wondering whether we can find the Longest non-decreasing subsequence with similar time complexity?
For example, consider an array : (4,10,4,8,9).
The longest increasing subsequence is (4,8,9).
And a longest non-decreasing subsequence would be (4,4,8,9).
First, here’s a “black box” approach that will let you find the longest nondecreasing subsequence using an off-the-shelf solver for longest increasing subsequences. Let’s take your sample array:
4, 10, 4, 8, 9
Now, imagine we transformed this array as follows by adding a tiny fraction to each number:
4.0, 10.1, 4.2, 8.3, 9.4
Changing the numbers this way will not change the results of any comparisons between two different integers, since the integer components have a larger magnitude difference than the values after the decimal point. However, if you compare the two 4s now, the latter 4 compares bigger than the previous one. If you now find the longest nondecreasing subsequence, you get back [4.0, 4.2, 8.3, 9.4], which you can then map back to [4, 4, 8, 9].
More generally, if you’re working with an array of n integer values, you can add i / n to each of the numbers, where i is its index, and you’ll be left with a sequence of distinct numbers. From there running a regular LIS algorithm will do the trick.
If you can’t work with fractions this way, you could alternatively multiply each number by n and then add in i, which also works.
On the other hand, suppose you have the code for a solver for LIS and want to convert it to one that solves the longest nondecreasing subsequence problem. The reasoning above shows that if you treat later copies of numbers as being “larger” than earlier copies, then you can just use a regular LIS. Given that, just read over the code for LIS and find spots where comparisons are made. When a comparison is made between two equal values, break the tie by considering the later appearance to be bigger than the earlier one.
I think the following will work in O(nlogn):
Scan the array from right to left, and for each element solve a subproblem of finding a longest subsequence starting from the given element of the array. E.g. if your array has indices from 0 to 4, then you start with the subarray [4,4] and check what's the longest sequence starting from 4, then you check subarray [3,4] and what's the longest subsequence starting from 3, next [2,4], and so on, until [0,4]. Finally, you choose the longest subsequence established in either of the steps.
For the last element (so subarray [4,4]) the longest sequence is always of length 1.
When in the next iteration you consider another element to the left (e.g., in the second step you consider the subarray [3,4], so the new element is element with the index 3 in the original array) you check if that element is not greater than some of the elements to its right. If so, you can take the result for some element from the right and add one.
For instance:
[4,4] -> longest sequence of length 1 (9)
[3,4] -> longest sequence of length 2 (8,9) 1+1 (you take the longest sequence from above which starts with 9 and add one to its length)
[2,4] -> longest sequence of length 3 (4,8,9) 2+1 (you take the longest sequence from above, i.e. (8,9), and add one to its length)
[1,4] -> longest sequence of length 1 (10) nothing to add to (10 is greater than all the elements to its right)
[0,4] -> longest sequence of length 4 (4,4,8,9) 3+1 (you take the longest sequence above, i.e. (4,8,9), and add one to its length)
The main issue is how to browse all the candidates to the right in logarithmic time. For that you keep a sorted map (a balanced binary tree). The keys are the already visited elements of the array. The values are the longest sequence lengths obtainable from that element. No need to store duplicates - among duplicate keys store the entry with largest value.
I have a mathematical/algorithmic problem here.
Given an array of numbers, find a way to separate it to 5 subarrays, so that sum of each subarrays is less than or equal to a given number. All numbers from the initial array, must go to one of the subarrays, and be part of one sum.
So the input to the algorithm would be:
d - representing the number that each subarrays sum has to be less or equal
A - representing the array of numbers that will be separated to different subarrays, and will be part of one sum
Algorithm complexity must be polynomial.
Thank you.
If by "subarray" you mean "subset" as opposed to "contiguous slice", it is impossible to find a polynomial time algorithm for this problem (unless P = NP). The Partition Problem is to partition a list of numbers into to sets such that the sum of both sets are equal. It is known to be NP-complete. The partition problem can be reduced to your problem as follows:
Suppose that x1, ..., x_n are positive numbers that you want to partition into 2 sets such that their sums are equal. Let d be this common sum (which would be the sum of the xi divided by 2). extend x_i to an array, A, of size n+3 by adding three copies of d. Clearly the only way to partition A into 5 subarrays so that the sum of each is less than or equal to d is if the sum of each actually equals d. This would in turn require 3 of the subarrays to have length 1, each consisting of the number d. The remaining 2 subarrays would be exactly a partition of the original n numbers.
On the other hand, if there are additional constraints on what the numbers are and/or the subarrays need to be, there might be a polynomial solution. But, if so, you should clearly spell out what there constraints are.
Set up of the problem:
d : the upper bound for the subarray
A : the initial array
Assuming A is not sorted.
(Heuristic)
Algorithm:
1.Sort A in ascending order using standard sorting algorithm->O(nlogn)
2.Check if the largest element of A is greater than d ->(constant)
if yes, no solution
if no, continue
3.Sum up all the element in A, denote S. Check if S/5 > d ->O(n)
if yes, no solution
if no, continue
4.Using greedy approach, create a new subarray Asi, add next biggest element aj in the sorted A to Asi so that the sum of Asi does not exceed d. Remove aj from sorted A ->O(n)
repeat step4 until either of the condition satisfied:
I.At creating subarray Asi, there are only 5-i element left
In this case, split the remaining element to individual subarray, done
II. i = 5. There are 5 subarray created.
The algorithm described above is bounded by O(nlogn) therefore in polynomial time.
At the risk of this question being off topic, does anybody know if there's a technical term for the process of summing adjacent elements of an array (or matrix). Like this:
[1,2,3,4,5,6,7,8]
[3,7,11,15]
[10,26]
[36]
In other words, if was going to make a function that did this, what would it be called?
Here's an example with a matrix:
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]]
[[14, 22],
[46, 54]]
[[136]]
Is there a specific term for each iteration of this process?
I want to write an algorithm that traverses a 2dimensional matrix of size k by n+1 (say), and where each element in the array is a list of elements. These lists vary in size they may be length 1, 2, ... , k. I can even say, for sure, that in the first row, they will all be length 1, in the second row: length 2, ... , in the kth row: length k. I imagine that Haskell has some sort of mechanism for "k-tuples", I just don't know what it is. Even if the type were indexed by some fixed size, that would be okay -- it would mean a small performance hit, but not too bad.
Any suggestions?
If you have k rows of n columns each with k elements - you could perhaps use a list of rows of kxn matrices for the same thing?
In the repa head repository they have a slightly different design where you can have elements which are non-unboxed types - you could use lists (or vectors) there.
http://code.ouroborus.net/repa/