longest sorted sequence | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

24th Feb 2018, 7:53 PM
Zeyad Sharo
Zeyad Sharo - avatar
4 Answers
+ 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
24th Feb 2018, 8:22 PM
John Wells
John Wells - avatar
24th Feb 2018, 9:06 PM
John Wells
John Wells - avatar
+ 4
thanks you so much 🌷
24th Feb 2018, 9:08 PM
Zeyad Sharo
Zeyad Sharo - avatar
+ 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]; } }
24th Feb 2018, 8:38 PM
Zeyad Sharo
Zeyad Sharo - avatar