See also on Ideone.
The example below shows the start and end indices of each interval and the sign of the first derivative (+1 if positive, 0 if constant, and -1 if negative).
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
int[] arr = { 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2 };
Console.WriteLine("arr");
foreach (var intervalo in arr.IntervalosCrescimento())
{
Console.WriteLine($"Start {intervalo.Item1}, end {intervalo.Item2}, sinal {intervalo.Item3}");
}
int[] arr2 = { 0, 1, 2, 3, 3, 3, 4, 5, 6, -1, -8, 0, 8 };
Console.WriteLine();
Console.WriteLine("arr2");
foreach (var intervalo in arr2.IntervalosCrescimento())
{
Console.WriteLine($"Start {intervalo.Item1}, end {intervalo.Item2}, sinal {intervalo.Item3}");
}
}
}
public static class ExtensionMethod
{
public static IEnumerable<Tuple<int, int, int>> IntervalosCrescimento<T>(this IEnumerable<T> en) where T : IComparable
{
bool first = true;
Tuple<int, int, int> result;
int start = 0, end = 0;
T previous = default(T);
int? previousSign = null;
int? sign = null;
int index = 0;
foreach (T current in en)
{
if (first)
{
first = false;
start = index;
previous = current;
}
else
{
sign = current.CompareTo(previous);
if (previousSign == null)
{
previousSign = sign;
}
else if (previousSign != sign)
{
end = index;
result = Tuple.Create(start, end, previousSign.Value);
start = index;
previousSign = sign;
yield return result;
}
previous = current;
++index;
}
}
end = index;
if (start < end)
yield return Tuple.Create(start, end, sign.Value);
}
}
I couldn’t understand your question. Could you please explain to me? :/
– Pedro Paulo
I’ll edit here friend
– Felipe Mateus
You want to generate random numbers to insert into the array, but when it is index 0 to 4 or 8 to 10 it has to be in ascending order, but when it is index 4 to 8 you have to enter in descending order?
– Pedro Paulo
no, I already have the vector with the filled positions, I want to find the intervals where the values are going up or down.
– Felipe Mateus
I set up a code like CONSOLE Application fast, I will provide to have an idea of what you want... let’s see if the administrators here do not get angry with me... I want to earn points here, but the JAVA people do not let... KKK!
– FabioIn
And what is the criterion?
– Maniero
From position 5 to position 8 is decreasing...
– Pedro Paulo