Efficient algo to find number of integers in a sorted array that are within a certain range in O(log(N)) time?

Bhaskar

I came across a interview question that has to be done in O(logn)

Given a sorted integer array and a number, find the start and end indexes of the number in the array.

Ex1: Array = {0,0,2,3,3,3,3,4,7,7,9} and Number = 3 --> Output = {3,6} 

Ex2: Array = {0,0,2,3,3,3,3,4,7,7,9} and Number = 5 --> Output = {-1,-1} 

I am trying to find an efficient algo for this but so fat have not been successful.

Ron Teller

You can use the concept of binary search to find the starting and ending index:

  • To find the starting index, you halve the array, if the value is equal to or greater than the input number, repeat with the lower half of the array, otherwise repeat with the higher half. stop when you reached an array of size 1.
  • To find the starting index, you halve the array, if the value is greater than the input number, repeat with the lower half of the array, otherwise repeat with the higher half. stop when you reached an array of size 1.

Note that when we reached an array of size 1, we may be one cell next to the input number, so we check if it equals the input number, if not, we fix the index by adding/decreasing 1 from the index we found.

findStartIndex(int[] A, int num)
{
    int start = 0; end = A.length-1;

    while (end != start) 
    {
        mid = (end - start)/2;

        if (A[mid] >= num)
            end = mid;
        else
            start = mid;
    }

    if(A[start] == num) 
        return start;
    else 
        return start+1;
}

findEndIndex(int[] A, int num)
{
    int start = 0; end = A.length-1;

    while (end != start) 
    {
        mid = (end - start)/2;

        if (A[mid] > num)
            end = mid;
        else
            start = mid;
    }

    if(A[start] == num) 
        return start;
    else 
        return start-1;
}

And the whole procedure:

int start = findStartIndex(A, num);

if (A[start]!=num) 
{ 
     print("-1,-1"); 
}
else
{
     int end = findEndIndex(A, num);
     print(start, end);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Detecting if a Number is within a Certain Range

From Dev

Excel Find Number Within a Range

From Dev

Efficient way of generating random integers within a range in Julia

From Dev

How to find if there's arithmatic mean of contiguous elements in a sorted array that equals to a given value in the most efficient running time?

From Dev

Find certain information within array

From Dev

Finding if a number falls within a certain range python

From Dev

Determine if the time an a certain timezone is within a range

From Dev

Determine if the time an a certain timezone is within a range

From Dev

Find grid cells within certain range

From Dev

python: if a number falls within a certain range, return the string corresponding to that range

From Dev

find number of days exists within a date range

From Dev

R: find if number is within range in a character string

From Dev

find number of days exists within a date range

From Dev

Excel find number from within date range

From Dev

Most efficient method to check for range of numbers within number without duplicates

From Dev

Find a number in sorted multidimentional array with binary search

From Dev

find a missing number in a sorted continuous array

From Dev

Most efficient way to insert an element into sorted array and find its index

From Dev

Find the largest number in an array of integers javascript

From Dev

Finding number of elements within a given range in an array

From Dev

Merge array elements within time range - how?

From Dev

Find the value associated with the next bigger number in a sorted range

From Dev

Count number of nodes within a certain range in AVL Tree

From Dev

How to determine if one number is within a certain range of another

From Dev

How do I assign a value in R if within a certain range of time?

From Dev

Given an array of integers, find the second largest and second smallest within the array

From Dev

How to find number of integers in a given range has even set bits

From Dev

Find value within a range of cells and return a value a certain distance away

From Dev

Find value within a range of cells and return a value a certain distance away

Related Related

  1. 1

    Detecting if a Number is within a Certain Range

  2. 2

    Excel Find Number Within a Range

  3. 3

    Efficient way of generating random integers within a range in Julia

  4. 4

    How to find if there's arithmatic mean of contiguous elements in a sorted array that equals to a given value in the most efficient running time?

  5. 5

    Find certain information within array

  6. 6

    Finding if a number falls within a certain range python

  7. 7

    Determine if the time an a certain timezone is within a range

  8. 8

    Determine if the time an a certain timezone is within a range

  9. 9

    Find grid cells within certain range

  10. 10

    python: if a number falls within a certain range, return the string corresponding to that range

  11. 11

    find number of days exists within a date range

  12. 12

    R: find if number is within range in a character string

  13. 13

    find number of days exists within a date range

  14. 14

    Excel find number from within date range

  15. 15

    Most efficient method to check for range of numbers within number without duplicates

  16. 16

    Find a number in sorted multidimentional array with binary search

  17. 17

    find a missing number in a sorted continuous array

  18. 18

    Most efficient way to insert an element into sorted array and find its index

  19. 19

    Find the largest number in an array of integers javascript

  20. 20

    Finding number of elements within a given range in an array

  21. 21

    Merge array elements within time range - how?

  22. 22

    Find the value associated with the next bigger number in a sorted range

  23. 23

    Count number of nodes within a certain range in AVL Tree

  24. 24

    How to determine if one number is within a certain range of another

  25. 25

    How do I assign a value in R if within a certain range of time?

  26. 26

    Given an array of integers, find the second largest and second smallest within the array

  27. 27

    How to find number of integers in a given range has even set bits

  28. 28

    Find value within a range of cells and return a value a certain distance away

  29. 29

    Find value within a range of cells and return a value a certain distance away

HotTag

Archive