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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:14:23+00:00 2026-06-05T07:14:23+00:00

I have an array Values array: 12 20 32 40 52 ^ ^ ^

  • 0

I have an array

Values array: 12 20 32 40 52
              ^  ^  ^  ^  ^
              0  1  2  3  4

on which I have to perform binary search to find the index of the range in which the number lies. For example:

  1. Given the number -> 19 (It lies between index 0 and 1), return 0
  2. Given the number -> 22 (It lies between index 1 and 2), return 1
  3. Given the number -> 40 (It lies between index 3 and 4), return 3

I implemented the binary search in the following manner, and this comes to be correct for case 1, and 3 but incorrect if we search for case 2 or 52, 55 32, etc.

#include <iostream>
using namespace std;

int findIndex(int values[], int number, unsigned first, unsigned last)
{
    unsigned midPoint;
    while(first<last)
    {
        unsigned midPoint = (first+last)/2;
        if (number <= values[midPoint])
            last = midPoint -1;
        else if (number > values[midPoint])
            first = midPoint + 1;
    }
    return midPoint;
}


int main()
{
    int a[] = {12, 20, 32, 40, 52};
    unsigned i = findIndex(a, 55, 0, 4);
    cout << i;
}

Use of additional variables such as bool found is not allowed.

  • 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-05T07:14:24+00:00Added an answer on June 5, 2026 at 7:14 am

    A range in C or C++ is normally given as the pointing directly to the lower bound, but one past the upper bound. Unless you’re feeling extremely masochistic, you probably want to stick to that convention in your search as well.

    Assuming you’re going to follow that, your last = midpoint-1; is incorrect. Rather, you want to set last to one past the end of the range you’re going to actually use, so it should be last = midpoint;

    You also only really need one comparison, not two. In a binary search as long as the two bounds aren’t equal, you’re going to set either the lower or the upper bound to the center point, so you only need to do one comparison to decide which.

    At least by convention, in C++, you do all your comparisons using < instead of <=, >, etc. Any of the above can work, but following the convention of using only < keeps from imposing extra (unnecessary) requirements on contained types.

    Though most interviewers probably don’t care, there’s also a potential overflow when you do midpoint = (left + right)/2;. I’d generally prefer midpoint = left + (right - left)/2;

    Taking those into account, code might look something like this:

    template <class T>
    T *lower_bound(T *left, T *right, T val) {
        while (left < right) {
            T *middle = left + (right - left) / 2;
            if (*middle < val)
                left = middle + 1;
            else
                right = middle;
        }
        return left;
    }
    
    template <class T>
    T *upper_bound(T *left, T *right, T val) {
        while (left < right) {
            T *middle = left + (right - left) / 2;
            if (val < *middle)
                right = middle;
            else
                left = middle + 1;
        }
        return left;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of values which is almost, but not quite sorted, with
Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns
I have this 4 Dimensional array to store String values which are used to
I have an array, which holds values like this: $items_pool = Array ( [0]
i have an array which contains different values, i want split them off and
I have a multidimensional array in which some values are present i want to
I have an array, which I'd like to search for a value in and
I have a seat function which takes a number of values, 3 of which
I have two arrays with time values in them in this format. 00:00:00 which
i have an array which could look like this: $config[$name][$array]['key'] = 'value'; // here

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.