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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:44:09+00:00 2026-06-07T04:44:09+00:00

I was trying to solve this : Given a sorted array that contains continuous

  • 0

I was trying to solve this :
Given a sorted array that contains continuous integers starting from 0(one integer may be repeated many times) eg – 0,0,0,1,2,3,3,3,4,4(can be very long also – this is just an example) , efficiently find the starting and ending indices of a given integer.

I am thinking of using

1)traversal(complexity = O(n))

2) a modified binary search(complexity =O(log n)). [ n = length of total array]

Then was wondering if the continuous integers property could be utilized to solve it.
Any different ideas or suggestions ?

  • 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-07T04:44:11+00:00Added an answer on June 7, 2026 at 4:44 am

    To begin, let’s ignore the “continuity” property

    As long as the problem is about finding the most efficient way to handle a single individual request, the straightforward general solution would be to perform two consecutive binary searches: the first one finds the beginning of the sequence, the second one finds the end of the sequence. The second search is performed in the remainder of the array, i.e. to the right of the previously found beginning of the sequence.

    However, if you somehow know that the average length of the sequence is relatively small, then it begins to make sense to replace the second binary search with a linear search. (This is the same principle that works when merging two sorted sequences of similar length: linear search outperforms binary search, because the structure of the input guarantees that on average the target of the search is located close to the beginning of the sequence).

    More formally, if the length of the whole array is n and the number of different integer values in the array (variety metric) is k, then linear search begins to outperform binary search on average when n/k becomes smaller than log2(n) (some implementation-dependent constant factors might be needed to come up with a practical relationship).

    The extreme example that illustrates this effect is the situation when n=k, i.e. when all values in the array are different. Obviously, using the linear search to find the end of each sequence (once you know the beginning) will be vastly more efficient than using binary search.

    But that’s something that requires extra knowledge about the properties of the input array: we need to know k.

    And this is when your “continuity” property comes into play!

    Since the numbers are continuous, the last value in the array minus the first value in the array is equal to k-1, meaning that

    k = array[n-1] - array[0] + 1
    

    This rule can also be applied to any sub-array of your original array to calculate the variety metric for that sub-array.

    That already gives you a very viable and efficient algorithm for finding the sequence: first perform a binary search for the beginning of the sequence, and then perform either binary or linear search depending on the relationship between n and k (or, even better, between the length of the right sub-array and the variety metric of the right sub-array).

    P.S. The same technique can be applied to the first search as well. If you are looking for sequence of i, then you immediately know that it is the j-th sequence in the array, where j = i - array[0]. That means that the linear search for the beginning of that sequence will take j * n/k steps on average. If this value is smaller than log2(n), linear search might be a better idea than binary search.

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

Sidebar

Related Questions

I'm trying to solve this problem : http://uva.onlinejudge.org/external/7/732.html . For the given example, they
I'm trying to solve a problem which is something like this: I'm given n
I'm trying to solve this particular problem using c++. Problem: Given a set of
The problem I'm trying to solve is as follows: I'm given a sorted list
I was trying to solve random coding kata's and found this one, my question
Been trying to solve this without progress for a couple days, my question: Given
Trying to solve this problem . I would like to learn how the bootstrapper
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I am trying to solve this SPOJ problem . The question asks to find
I've been trying to solve this silly problem for a few days now and

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.