+ 1
longest sorted sequence
how to find the longest sorted sequence of items in array for example array = 40,24,5,8,19,5,10,8,86,13 the solution is : 5,8,19
4 Respuestas
+ 4
zero longest count
set longest start to array index 0
set current count to one
set current start to array index 0
for second item index of 1 to last item index loop
if current item is greater than previous item
increment current count
else
if current count is greater than longest count
set longest count to current count
set longest start to current start
set current count to one
set current start to current item's array index
for 0 to longest count minus 1
display the array with index of longest start plus loop index
+ 4
+ 4
thanks you so much 🌷
+ 1
like this #Johnwells
static void longest_sorted_sequence()
{
int[] arr = new int[] {78,5, 8, 19, 20, 10, 8, 86, 13,78,90 };
int longest = arr[0];
int currentcount = 1;
int currentstart = arr[0];
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] > arr[i + 1])
{
currentcount++;
}
else
{
if (currentcount > longest)
currentcount = longest;
currentstart = longest;
currentcount = 1;
currentstart = arr[currentcount];
}
}