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 8399257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:15:11+00:00 2026-06-09T21:15:11+00:00

I have an unsorted array and need to extract the longest sequence of sorted

  • 0

I have an unsorted array and need to extract the longest sequence of sorted elements.
For instance

A = 2,4,1,7,4,5,0,8,65,4,2,34

here 0,8,65 is my target sequence

I need to keep track of the index where this sequence starts

  • 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-09T21:15:12+00:00Added an answer on June 9, 2026 at 9:15 pm

    You can do it in linear time O(N) with this algorithm: construct vector len of the same size N as the original vector, such that len[i] contains the length of the longest consecutive ascending run to which element seq[i] belongs.

    The value of len[i] can be calculated as follows:

    len[0] = 1;
    for (int i = 1 ; i != N ; i++) {
        len[i] = seq[i-1] >= seq[i] ? 1 : len[i-1]+1;
    }
    

    With len in hand, find the index of max(len) element. This is the last element of your run. Track back to len[j] == 1 to find the initial element of the run.

    seq    len
    ---    ---
      2      1
      4      2
      1      1
      7      2
      4      1
      5      2
      0      1
      8      2
     65      3 << MAX
      4      1
      2      1
     34      2
    

    Note that at each step of the algorithm you need only the element len[i-1] to calculate len, so you can optimize for constant space by dropping vector representation of len and keeping the prior one, the max_len, and max_len_index.

    Here is this algorithm optimized for constant space. Variable len represents len[i-1] from the linear-space algorithm.

    int len = 1, pos = 0, maxlen = 1, current_start = 0;
    for (int i = 1 ; i < seq.size() ; i++) {
        if (seq[i] > seq[i-1]) {
            len++;
            if (len > maxlen) {
                maxlen = len;
                pos = current_start;
            }
        } else {
            len = 1;
            current_start = i;
        }
    }
    

    Here is a link to this program on ideone.

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

Sidebar

Related Questions

I have an unsorted array of objects. I need to know how I can
For this problem I have a sorted Array of doubles. I need to be
I have a small unsorted array and I'd like to find the index of
Suppose you have an unsorted array, how will you find 2 equal elements such
I have an unsorted array, what is the best method to remove all the
I have a large unsorted list of items. Some items are important and need
I have an unsorted Array holding the following IDs: @un_array = ['bar', 'para-3', 'para-2',
We have two unsorted arrays and each array has a length of n. These
Let's say I have an unsorted array from 1 to 10, as shown below...
Possible Duplicate: PHP: sorting a multidimentional array ($arr[$i]['v']) I have an array of UNSORTED

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.