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

  • SEARCH
  • Home
  • 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 4609882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:03:06+00:00 2026-05-22T01:03:06+00:00

I have an array like this: 0011011100011111001 I’d like to find the longest sequence

  • 0

I have an array like this:

0011011100011111001

I’d like to find the longest sequence of 1’s in this array, keeping note of the length and position of the starting point, here the length would be 5 and the starting point is 12.

Any ideas for how to go about this?

  • 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-05-22T01:03:07+00:00Added an answer on May 22, 2026 at 1:03 am

    You can set a starting position and length initially to zero, then go through the array elements one by one, keeping track of the transitions between 1 and 0 with a simple state machine.

    The state table looks like this:

                  +----------------------------------------+
                  |               lastNum                  |
                  +------------------+---------------------+
                  |         0        |          1          |
    +---------+---+------------------+---------------------+
    |         | 0 | Just get next    | Check/update runLen |
    |         |   |  character       |  against previous   |
    | thisNum +---+------------------+---------------------+
    |         | 1 | Store runPos  ,  | Increment runLen    |
    |         |   |  set runLen to 0 |                     |
    +---------+---+------------------+---------------------+
    

    Since you’re only interested in 1 sequences, the initial state has lastNum set to zero to ensure a 1 at the start correctly begins a run. We also set up the initial “largest run to date” to have a size and position of zero to ensure it gets overwritten by the first real run.

    There’s a special edge case to this method because we have to detect if the final number in the list is 1 – if so, it means there will have been no 1 -> 0 transition at the end of the list for checking the final run of 1 numbers.

    Since we will have exited the loop without checking that final run, we do a final check as if we were transitioning from 1 to 0.

    The pseudo-code goes something like this. First, the initialisation of all variables:

    # Store the current maximum run of '1' characters (none) and initial state.
    
    var maxLen = 0, maxPos = 0, lastNum = 0
    
    # runLen and runPos will be set in a 0->1 transition before we use them.
    
    var rnLen, runPos
    

    Then we can simply iterate over each number in the list and detect transitions as per the diagram above:

    # Iterate over entire list, one by one.
    
    for curPos = 0 to len(list) - 1:
        # 0 -> 0: do nothing.
    
        # 0 -> 1: store position, set run length to 1.
    
        if lastNum == 0 and list[curPos] == 1:
            runPos = curPos
            runLen = 1
        endif
    
        # 1 -> 1: increment the current run length.
    
        if lastNum == 1 and list[curPos] == 1:
            runLen = runLen + 1
        endif
    
        # 1 -> 0: check current run against greatest to date.
    
        if lastNum == 1 and list[curPos] == 0:
            if runLen > maxLen:
                maxPos = runPos
                maxLen = runLen
            endif
        endif
    
        # Save current number into lastNum for next iteration.
    
        lastNum = list[curPos]
    endfor
    

    Then, finally, handling of the afore-mentioned edge case:

    # If we finished with a `1`, need to check final run.
    
    if lastNum = 1:
        if runLen > maxLen:
            maxPos = runPos
            maxLen = runLen
        endif
    endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array like this. I need to add the total length of
I have an array like this: [school_name:My School Name, school_number:54546, note:, class:1] and HTML
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
Hello i have array like this [0] => Array ( [ExamMonth] => Array (
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have an array like this var array = [{ order: 3, sub -
I have an array like this one $state[1]='FG'; $state[2]='BU'; $state[3]='CA'; ... $state[150]='YT' What I
i have an array like this: $arr = array(array('1', '0', 'nokia'), array('2', '0', 'samsung'),
I have an array like this: Array ( [2] => Array ( [0] =>
I have an array like this: object[] args and need to insert those args

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.