array.binary_search_rightmost() - Pine Script Function
array.binary_search_rightmost()
Section titled “array.binary_search_rightmost()”Overview
Section titled “Overview”The function returns the index of the value if it is found. When the value is not found, the function returns the index of the element to the right of where the value would lie if it was in the array. The array must be sorted in ascending order.
Syntax
Section titled “Syntax”array.binary_search_rightmost(id, val) → series intParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
| id | array<int/float> | An array object. |
Remarks
Section titled “Remarks”- A binary search works on sorted arrays in ascending order. It begins by comparing an element in the middle of the array with the target value. If the element matches the target value, its position in the array is returned. If the element’s value is greater than the target value, the search continues in the lower half of the array. If the element’s value is less than the target value, the search continues in the upper half of the array. By doing this recursively, the algorithm progressively eliminates smaller and smaller portions of the array in which the target value cannot lie.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”//@version=6indicator("array.binary_search_rightmost, repetitive elements")a = array.from(4, 5, 5, 5)// Returns the index of the last instance.position = array.binary_search_rightmost(a, 5)plot(position) // Plots 3