Finding range index of a sorted array on a specific element

Piqka

I am working with array where the set is sorted. The task is to find the range of a value.

Lets assume we have this sorted array:

int[] array = {1,1,1,3,3,9,10}

Example We have to find the range- min and max for the element 1. We quickly spot that the first index of the element 1 is 0, and the range max is 2.

We now know that the range between 0 and 2 all have value 1. If the searched element is 3 we see that range is 3-4, and for the element 9 the range is 6-6.

I have used linear search to do this, but would hear, if there is a more faster way to do this?

Arturo Menchaca

You can use two versions of binary search to find the leftmost and rightmost positions:

int[] array = { 1, 1, 1, 3, 3, 9, 10 }
int value = ...

int leftmost(int min, int max)
{
    if (min == max) return min
    int mid = (min + max) / 2

    if (array[mid] < value) return leftmost(mid + 1, max)
    else return leftmost(min, mid)
}

int rightmost(int min, int max)
{
    if (min == max) return min
    int mid = (min + max + 1) / 2

    if (array[mid] > value) return rightmost(min, mid - 1)
    else return rightmost(mid, max)
}

Just make the calls with min=0 and max=array.length-1. Time complexity is O(logn).

You will get outputs like this (0-based indexes):

value=1  -> left=0, right=2
value=3  -> left=3, right=4
value=9  -> left=5, right=5
value=10 -> left=6, right=6

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding an index in a sorted array

From Dev

Finding the Index of sorted elements in Python Array

From Java

Index for finding an element in a JSON array

From Dev

Finding an index of an element in a string array

From Dev

Finding the index of an array element in an array of objects in JavaScript?

From Dev

finding a specific array element and remove the container array

From Dev

finding a specific index on a two dimensional array

From Dev

Numpy finding element index in another array

From Dev

Finding the max element in an array, sorted ascending first and then descending

From Dev

Finding the max element in an array, sorted ascending first and then descending

From Dev

Finding mode of a sorted array

From Dev

Find index range of specific values in sorted vector of vectors

From Dev

Replace element in sorted range

From Dev

Replace element in sorted range

From Dev

Index of element in sorted()

From Dev

Using bsearch to find index for inserting new element into sorted array

From Dev

Java: How to find index of last element of partially initialized sorted array

From Dev

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

From Dev

On finding the index of the first >= element

From Dev

Finding index of element that was selected

From Dev

Finding index of element that was selected

From Dev

Fastest way of finding the index of the closest element in a non-sorted Python list of floats

From Dev

C++ finding the (largest) index of the largest element in an array

From Dev

finding the index of last occurrence of an element in an array using binary search PHP

From Dev

finding the index of last occurrence of an element in an array using binary search PHP

From Dev

Finding index of largest negative and smallest positive element in array

From Dev

What methods are there for finding the element of an array containing a specific real value?

From Dev

What methods are there for finding the element of an array containing a specific real value?

From Java

Finding specific range of number in java

Related Related

  1. 1

    Finding an index in a sorted array

  2. 2

    Finding the Index of sorted elements in Python Array

  3. 3

    Index for finding an element in a JSON array

  4. 4

    Finding an index of an element in a string array

  5. 5

    Finding the index of an array element in an array of objects in JavaScript?

  6. 6

    finding a specific array element and remove the container array

  7. 7

    finding a specific index on a two dimensional array

  8. 8

    Numpy finding element index in another array

  9. 9

    Finding the max element in an array, sorted ascending first and then descending

  10. 10

    Finding the max element in an array, sorted ascending first and then descending

  11. 11

    Finding mode of a sorted array

  12. 12

    Find index range of specific values in sorted vector of vectors

  13. 13

    Replace element in sorted range

  14. 14

    Replace element in sorted range

  15. 15

    Index of element in sorted()

  16. 16

    Using bsearch to find index for inserting new element into sorted array

  17. 17

    Java: How to find index of last element of partially initialized sorted array

  18. 18

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

  19. 19

    On finding the index of the first >= element

  20. 20

    Finding index of element that was selected

  21. 21

    Finding index of element that was selected

  22. 22

    Fastest way of finding the index of the closest element in a non-sorted Python list of floats

  23. 23

    C++ finding the (largest) index of the largest element in an array

  24. 24

    finding the index of last occurrence of an element in an array using binary search PHP

  25. 25

    finding the index of last occurrence of an element in an array using binary search PHP

  26. 26

    Finding index of largest negative and smallest positive element in array

  27. 27

    What methods are there for finding the element of an array containing a specific real value?

  28. 28

    What methods are there for finding the element of an array containing a specific real value?

  29. 29

    Finding specific range of number in java

HotTag

Archive