+ Reply to Thread
Results 1 to 3 of 3

Thread: Find largest subsequence sum

  1. #1
    TopGun Guest

    Find largest subsequence sum

    Give efficient algorithm to find largest subsequence sum in an array.

  2. #2
    Krazy Guest

    Re: Find largest subsequence sum

    I think that the following algorithm solves the problem in linear time. But it should be checked. "x" is the array of numbers and "n" is the number of elements in the array. Array is assumed to be 0-indexed. -9999999999 is used for -Inf and it should be changed with true -Inf constant. "maxv" is the maximum sum and "maxi1" and "maxi2" is the start and end indices of the sub-array.

    The idea behind the algorithm is as follows:
    It uses cumulative sum instead of the original array. Then at each index, it calculates the difference of the value at that index and the smallest value before that index. The maximum sub-array must have this value maximized. Two loops are merged into a single loop. After one pass over the numbers, we have the largest sum and boundary indices of the sub-array. So dynamic programming method is used.

    Code:
        mini = -1;    minv = 0;    maxi1 = 0;    maxi2 = 0;     maxv = -9999999999;   
        for (i=0;i<n;i++) {
            if (i>0)
                x[i] += x[i-1];
            if (x[i]-minv>=maxv) {
                maxv = x[i]-minv;
                maxi1 = mini+1;
                maxi2 = i;
            }
            if (x[i]<minv) {
                minv = x[i];
                mini = i;
            }
        }

  3. #3
    prodigy Guest
    Just Add all positive elements of the array

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find 6th largest element in an array
    By Surfer in forum Adobe
    Replies: 4
    Last Post: 5th December 2010, 01:08
  2. Given a node in a binary tree, find the next largest node.
    By yuzi in forum Algorithm/Data Structure Questions
    Replies: 1
    Last Post: 25th June 2010, 20:37
  3. Find 2nd largest
    By JavaGuy in forum Apple
    Replies: 0
    Last Post: 27th May 2010, 00:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts