1

Can anyone help me out with this problem that I have been trying to solve for a while.

Lets say there is an array A[1,2...n] of numbers and we wish to find the longest increasing consecutive sub sequence with Divide an Conquer method. Specifically we wish to find indices i,j such that i<=j and A[i]<=A[i+1]<=.....A[j]. For Example if the array has 4,1,3,5,6,7,5,8,2 then it has to return [1,3,5,6,7].

I have searched a lot about this problem but all I can find is dynamic approach and Longest Increasing Subsequences without consecutive elements.

ArCh3r
  • 89
  • 1
  • 8

2 Answers2

1

How about this:

  1. Divide array A to A1 and A2.

  2. Find the longest consecutive sub sequence of sub array A1 and A2, named as s1, s2 respectively.

  3. If the longest consecutive sub sequence cross A1 and A2, the consecutive sub sequence must use the last of A1 and first of A2, named as s3.

  4. Compare s1, s2, s3, find the longest one.

  5. In step 2, it needs divide sub arrays constantly. And step 3 and 4 are conquer process.

Yanhui Zhou
  • 872
  • 6
  • 20
1

A divide-and-conquer solution can be done by dividing the array into 2, say A1 and A2. Then, once having recursively solved the problem for the two sub-arrays, you should consider the scenarios in which the optimal solution to the original array may lie.

Option 1: The longest contiguous increasing subsequence is completely in A1, in which case you already found the maximum length, or the relevant answer, or whatever it is you are planning to return.

Option 2: Similarly, the longest contiguous increasing subsequence is entirely in A2.

Option 3: The longest contiguous increasing subsequence is partially in A1 and partially in Array2. In this case, considering A1 is the left portion of the array and A2 is the right portion, you basically have to go left from the intersection until it is not decreasing or you reach the left end of A1. And then you go to right on A2 until it is not increasing or you reach the right end of it.

Among these options you take the one with the greatest length, and you're done.

However, I should note that divide-and-conquer is not the optimal solution to this problem, as it has O(nlogn) time complexity. As mentioned in Jon Bentley's notable book, Programming Pearls, a solution what he calls as the maximum sum contiguous subsequence problem is known to have linear time complexity. That solution may easily be adapted to handle increasing subsequences, instead of the maximum sum.

The algorithm is based on an approach Bentley calls scanning, and it is based on the idea that any subsequence has to end at some point.

The approach is painfully simple, and a Python implementation can be found below.

def maxIncreasing(arr):
    maxLength = 1
    maxStart = 0
    curStart = 0
    curLength = 1
    for i in range(1, len(arr)):
        if arr[i] <= arr[i-1]:
            if curLength > maxLength:
                maxLength = curLength
                maxStart = curStart
            curStart = i
            curLength = 1
        else:
            curLength += 1
    if curLength > maxLength:
        maxLength = curLength
        maxStart = curStart
    return (maxLength, maxStart)
ilim
  • 4,477
  • 7
  • 27
  • 46
  • please help me in understanding. how this solution is nlong? – shivankgtm Apr 02 '20 at 09:03
  • @shivank98 Consider dividing the array into 2 equal parts. You start with length `N`, and after you recursively divide it into 2 for `logN` times, you end up with `N` arrays of length 1 and you can't divide any further. So, after dividing a subsequence `S` into 2 and solving the problem for them recursively, if you can form the solution for sequence `S` from the solutions for its halves in `O(K)` time, where `K` is the length of subsequence `S`, then you end up with `O(NlogN)` complexity. Because there are `logN` levels of recursion and sum of lengths of subsequences at every level is `N`. – ilim Apr 02 '20 at 09:24