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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:04:24+00:00 2026-06-08T13:04:24+00:00

I have a sorted std::vector<int> and I would like to find the longest ‘streak

  • 0

I have a sorted std::vector<int> and I would like to find the longest ‘streak of consecutive numbers’ in this vector and then return both the length of it and the smallest number in the streak.

To visualize it for you :
suppose we have :
1 3 4 5 6 8 9

I would like it to return: maxStreakLength = 4 and streakBase = 3

There might be occasion where there will be 2 streaks and we have to choose which one is longer.

What is the best (fastest) way to do this ? I have tried to implement this but I have problems with coping with more than one streak in the vector. Should I use temporary vectors and then compare their lengths?

  • 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-08T13:04:25+00:00Added an answer on June 8, 2026 at 1:04 pm

    No you can do this in one pass through the vector and only storing the longest start point and length found so far. You also need much fewer than ‘N’ comparisons. *

    hint: If you already have say a 4 long match ending at the 5th position (=6) and which position do you have to check next?

    [*] left as exercise to the reader to work out what’s the likely O( ) complexity 😉

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

Sidebar

Related Questions

I have a std::vector<std::string> which would contain numbers and characters (single char). I would
I have a sorted std::vector<unsigned long long int> myvector (and all values are different).
I have some code that looks like this: std::set<int> s1, s2, out; // ...
How would I go about this, I have files which I have sorted the
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have some data that is stored in a sorted vector. This vector is
I have a problem sorting vectors in 2D vector? I would like to sort
I have a std::vector<std::pair<int,std::pair<Bone,std::string> > > I'm trying to sort it with std sort,
I have a collection of elements in a std::vector that are sorted in a
I have a sorted std::vector of relative small size ( from 5 to 20

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.