Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8549619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:45:28+00:00 2026-06-11T13:45:28+00:00

Given an array , we need to find the length of longest sub-sequence with

  • 0

Given an array , we need to find the length of longest sub-sequence with alternating increasing and decreasing values.

For example , if the array is ,
7 4 8 9 3 5 2 1 then the L = 6 for 7,4,8,3,5,2 or 7,4,9,3,5,1 , etc.

It could also be the case that first we have small then big element.

What could be the most efficient solution for this ? I had a DP solution in mind. And if we were to do it using brute force how would we do it (O(n^3) ?) ?

And it’s not a homework problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:45:29+00:00Added an answer on June 11, 2026 at 1:45 pm

    You indeed can use dynamic programming approach here. For sake of simplicity , assume we need to find only the maximal length of such sequence seq (it will be easy to tweak solution to find the sequence itself).

    For each index we will store 2 values:

    • maximal length of alternating sequence ending at that element where last step was increasing (say, incr[i])
    • maximal length of alternating sequence ending at that element where last step was decreasing (say, decr[i])

    also by definition we assume incr[0] = decr[0] = 1

    then each incr[i] can be found recursively:

    incr[i] = max(decr[j])+1, where j < i and seq[j] < seq[i]
    decr[i] = max(incr[j])+1, where j < i and seq[j] > seq[i]
    

    Required length of the sequence will be the maximum value in both arrays, complexity of this approach is O(N*N) and it requires 2N of extra memory (where N is the length of initial sequence)

    simple example in c:

    int seq[N]; // initial sequence
    int incr[N], decr[N];
    
    ... // Init sequences, fill incr and decr with 1's as initial values
    
    for (int i = 1; i < N; ++i){
        for (int j = 0; j < i; ++j){
             if (seq[j] < seq[i]) 
             {
                 // handle "increasing" step - need to check previous "decreasing" value
                 if (decr[j]+1 > incr[i])  incr[i] = decr[j] + 1;
             }
             if (seq[j] > seq[i]) 
             {
                 if (incr[j]+1 > decr[i])  decr[i] = incr[j] + 1;
             }
        }
    }
    
    ... // Now all arrays are filled, iterate over them and find maximum value
    

    How algorithm will work:

    step 0 (initial values):

    seq  = 7   4 8 9 3 5 2 1
    incr = 1   1 1 1 1 1 1 1
    decr = 1   1 1 1 1 1 1 1
    

    step 1 take value at index 1 (‘4’) and check previous values. 7 > 4 so we make “decreasing step from index 0 to index 1, new sequence values:

    incr = 1 1   1 1 1 1 1 1
    decr = 1 2   1 1 1 1 1 1
    

    step 2. take value 8 and iterate over previous value:

    7 < 8, make increasing step: incr[2] = MAX(incr[2], decr[0]+1):

    incr = 1 1 2   1 1 1 1 1
    decr = 1 2 1   1 1 1 1 1
    

    4 < 8, make increasing step: incr[2] = MAX(incr[2], decr[1]+1):

    incr = 1 1 3   1 1 1 1 1
    decr = 1 2 1   1 1 1 1 1
    

    etc…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the array, i need to find how many monotonically increasing Sub-arrays there are
Given array a= [1,4,5,9,2].I need to find/print combinations of two values where sum =
An unsorted array is given and we need to find top 5 elements in
I'm given an array (a2d) and I need to determine if every row and
I need to write effective and quick method to search byte array for given
I need to convert a navigable map to a 2D String array. Below given
Given an array of integers A[1...n-1] where N is the length of array A.
Given a set of sixteen letters and an English dictionary file, need to find
I have a pattern @@{} and given a string I need to find out
Given a list of assorted length words, what is the best way to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.