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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:06:54+00:00 2026-06-16T15:06:54+00:00

given a string consists only of 0s and 1s say 10101 how to find

  • 0

given a string consists only of 0s and 1s say 10101
how to find the length of the longest non decreasing sub-sequence??
for example,

for the string,
10101

the longest non decreasing sub sequences are

  • 111
  • 001
    so you should output 3

for the string
101001

the longest non decreasing sub sequence is

  • 0001

so you should output 4
how to find this??

how can this be done when we are provided with limits.sequence between the limit
for example
101001
limits [3,6]
the longest non decreasing sub sequence is

  • 001
    so you should output 3

can this be achieved in o(strlen)

  • 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-16T15:06:55+00:00Added an answer on June 16, 2026 at 3:06 pm

    Can this be achieved in O(strlen)?

    Yes. Observe that the non-decreasing subsequences would have one of these three forms:

    0........0 // Only zeros
    1........1 // Only ones
    0...01...1 // Some zeros followed by some ones
    

    The first two forms can be easily checked in O(1) by counting all zeros and by counting all ones.

    The last one is a bit harder: you need to go through the string keeping the counter of zeros that you’ve seen so far, along with the length of the longest string of 0...01...1 form that you have discovered so far. At each step where you see 1 in the string, the length of the longest subsequence of the third form is the larger of the number of zeros plus one or the longest 0...01...1 sequence that you’ve seen so far plus one.

    Here is the implementation of the above approach in C:

    char *str = "10101001";
    int longest0=0, longest1=0;
    for (char *p = str ; *p ; p++) {
        if (*p == '0') {
            longest0++;
        } else { // *p must be 1
            longest1 = max(longest0, longest1)+1;
        }
    }
    printf("%d\n", max(longest0, longest1));
    

    max is defined as follows:

    #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
    

    Here is a link to a demo on ideone.

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

Sidebar

Related Questions

I'm trying to check if a given UTF-8 string consists of letters only. I
I need to make a method that checks if a given String only consists
I'm in need to modify a given string to contain only alpha numerical characters,
How to find the below pattern is there or not in a given string
I am working on the problem http://www.spoj.pl/problems/DISUBSTR/ Given a string, we need to find
I was assigned a problem to find genes when given a string of the
The table consists of columns column_1, column_2, column_3. Given the set of string values
I'm trying to compress any given string to a shorter version, copy paste-able compressed
Possible Duplicate: how to rotate the given string to left or right in C?
I've been looking for a way to hash a given string in C# that

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.