Determining the highest and lowest prices in a VARIABLE number of bars - thinkscript

I do not know how to use highestall() or lowestall() here. I do not know how to use getvalue here that only addresses a single value. And the highest() function will not work with a variable. How would I determine the highest value in a known variable range of bars?
All of the above I wrestled with expecting to find the highest value, lowest value, and bar numbers within a variable length series of bars.

Related

Which value is the current maximum at std::max_element with lambda?

If I'm using std::max_element with a lambda and I have a special value in the row which is always considered to be lower than anything.
So when the parameters are given to the comparison-lambda:
which of both parameters is the current maximum, i.e. the maximum of the values evaluated before?
This would help me in a way that I won't have to check both values for my magic value.
The Compare functor must implement strict weak ordering - acting as <(less than). It is not specified which parameter is the current maximum. It must be one of them because exactly N-1 comparisons are made. But it can be either of them depending on the if statement used in the for loop.
The logic then dictates that the larger of those parameters will be the new maximum.

number of subarrays whose product is even?

given an array of size n, n<=10^5 what is efficient approach to count number of sub arrays whose product is even ?
i am using naive approach with (On^3) time complexity ?
please suggest some efficient approach?
Be careful: from your explanation I have the impression that you are taking all sub-arrays, calculate the product and check if it is even.
However there's one very important mathematical rule: when you have a series of natural numbers, as soon as there's one even number, the product will be even.
So, I'd advise you to program following algorithm:
Search in your array for an even number.
Count the amount of sub-arrays, containing that even number.
Search in your array for the next even number.
Count the amount of sub-arrays, containing that next even number, but not containing the previous even number.
Continue until you've processed all even numbers in your array.

Given an array containing both positive and negative integers, how to calculate number of pairs whose product is equal to a given clvalue

I am able to do this in O(n^2) complexity. But I want to know a better solution. I read about assignment of 2 indices start and end and check their product with the given value, but that's not applicable to arrays containing negative integers.

How to delete specific elements from an array in c++

I dont know the numbers which are stored in the array[multidimensional].As I get these numbers from the sensor.I just know that If the same number is repeated more than 5 times, that number should be deleted.
please help.
How to delete specific elements from an array
Depends on what do you mean by "delete". An array of x numbers always has exactly x numbers. An integer can't have a state that represents a "deleted" number, unless you decide that a specific value signifies such state. A typical choice would be -1 if only positive values are used otherwise. A floating point number could be set to NaN, but considering the "repeated 5 times" requirement, remember that equality comparison of floating point numbers is not trivial.
Or, you could maintain a duplicate array of bools which signifies whether the number in corresponding index has been deleted.
Another approach would be to augment your array with a pointer to the last "used" number (or rather, point to the one after the last used number). This allows you to represent a smaller (dynamic)array than fits into the whole array. The size of such dynamic array would be the distance between address of the first number and the pointer and the size may change up to x. The numbers beyond the pointer would then be considered deleted. You must take care not to access the deleted numbers thinking they would contain valid data. If you want to delete a number in middle of the array, simply copy all numbers after it one index to the left and decrement the pointer. If you don't want to implement this yourself (and you shouldn't want to), you may want to use std::vector instead since this is pretty much what vector does under the hood.

How to sort even and odd numbers alternatively with the limit of time and space complexity?(C/C++)

Given a integer array like
int numbers[8]={1, 3, 5, 7, 8, 6, 4, 2};
The half side in the front array are odd numbers, and the rest (the equal amount number)
are even. The odd numbers are in an ascending order and even part are in a descending order. After the sorting, the order of the numbers can't be changed.
How can I sort them alternatively with time complexity less than O(n^2) and space complexity O(1)?
For this example, the result would be: {1,8,3,6,5,4,7,2};
I can't use external array storage but temporary variables are acceptable.
I have tried to use two pointers(oddPtr, evenPtr) to point odd and even numbers separately, and move evenPtrto insert the even values to the middles of odd numbers.(Like insertion sort)
But it takes O(n^2).
UPDATED
As per Dukeling's comment I realized that the solution I propose in fact is not linear,but linearithmic and even worse - you can't control if it takes extra memory or not. On my second thought I realized you know a lot about the array you to implement a more specific, but probably easier solution.
I will make an assumption that all values in the array are positive. I need this so that I can use negative values as kind of 'already processed' flag. My idea is the following - iterate over the array from left to right. For each element if it is already processed(i.e. its value is negative) simply continue with the next one. Otherwise you will have a constant formula where is the position where this element should be:
If the value is odd and its index is i it should move to i*2
If the value is even and its index is i it should move to (i - n/2)*2 + 1
Store this value into a temporary and make the value at the current index of the array 0. Now until the position where the value we 'have at hand' is not zero, swap it with the value staying at the position we should place it according to the formula above. Also when you place the value at hand negate it to 'mark it as processed'. Now we have a new value 'at hand' and again we calculate where it should go according to the formula above. We continue moving values until the value we 'have at hand' should go to the position with 0. With a little thought you can prove that you will never have a negative('processed') value at hand and that eventually you will end up at the empty spot of the array.
After you process all the values iterate once over the array to negate all values and you will have the array you need. The complexity of the algorithm I describe is linear- each value will be no more than once 'at hand' and you will iterate over it no more than once.