# Next highest and lowest element in double array ?

hello

Anyone have the code for this? i know it's simple but my mind is somehow blocked.

let's say i have an array contains few double elements,

I know one of these elements and need to get the closest hi and closest low to this element.

I don't want to use sorting and indexing,

thank you.

Mohamed Mostafa Mohamed Sonbol:

hello

Anyone have the code for this? i know it's simple but my mind is somehow blocked.

let's say i have an array contains few double elements,

I know one of these elements and need to get the closest hi and closest low to this element.

I don't want to use sorting and indexing,

thank you.

Interesting!

This is a possible solution for your problem:

```//+------------------------------------------------------------------+
//| Return the index of the closest lower element < target in the    |
//| unsorted list.                                                   |
//+------------------------------------------------------------------+
template<typename T>
int FindClosestLow(const T &array[], const T target)
{
T min = NULL;
T highest_min = NULL;
int minIndex = -1;

for(int i = 0; i < ArraySize(array); i++)
{
if(array[i] < target)
{
min = array[i];

if(minIndex == -1 || min > highest_min)
{
highest_min = min;
minIndex = i;
}
}
}

return minIndex;
}

//+------------------------------------------------------------------+
//| Return the index of the closest higher element > target in the   |
//| unsorted list.                                                   |
//+------------------------------------------------------------------+
template<typename T>
int FindClosestHigh(const T &array[], const T target)
{
T max = NULL;
T lowest_max = NULL;
int maxIndex = -1;

for(int i = 0; i < ArraySize(array); i++)
{
if(array[i] > target)
{
max = array[i];

if(maxIndex == -1 || max < lowest_max)
{
lowest_max = max;
maxIndex = i;
}
}
}

return maxIndex;
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
{
int array[] = { 4, 9, 2, 4, 1, 6, 4 };
Print("array:");
ArrayPrint(array);

Print("--------Find closest element higher > target---------");
for(int target = -1; target <= 10; target++)
{
int index = FindClosestHigh(array, target);

int element = (index > -1) ? array[index] : INT_MIN;

PrintFormat("FindClosestHigh(%d) element = %d", target, element);
}
}
//+------------------------------------------------------------------+

// sample output:

/*
--------Find closest element higher > target---------
array:
4 9 2 4 1 6 4
FindClosestHigh(-1) element = 1
FindClosestHigh(0) element = 1
FindClosestHigh(1) element = 2
FindClosestHigh(2) element = 4
FindClosestHigh(3) element = 4
FindClosestHigh(4) element = 6
FindClosestHigh(5) element = 6
FindClosestHigh(6) element = 9
FindClosestHigh(7) element = 9
FindClosestHigh(8) element = 9
FindClosestHigh(9) element = -2147483648
FindClosestHigh(10) element = -2147483648
*/
```
Files:

amrali #:

Interesting!

This is a possible solution for your problem:

thanks a lot Amr

it came quite longer than I expected :)