Advertisement
Nina-S

Untitled

Jan 6th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. Write a program that finds the length of the maximal increasing sequence in an array of N integers.
  2.  
  3. Input
  4. Read from the standard input
  5.  
  6. On the first line you will receive the number N
  7. On the next N lines the numbers of the array will be given
  8. Output
  9. Print to the standard output
  10.  
  11. Print the length of the maximal increasing sequence
  12. input 8 is the array lenght, with elements 7,3,2,3,5,2,2,4
  13. output is 3 - 2,3,5
  14.  
  15.  
  16. //let input = ["8", "7", "3", "2", "3", "5", "2", "2", "4"];
  17. //let input = ["1", "2"]; - output should be 1
  18. let input = ["2", "2", "1"]; - output should be 0
  19.  
  20. let print = this.print || console.log;
  21. let gets =
  22. this.gets ||
  23. (
  24. (arr, index) => () =>
  25. arr[index++]
  26. )(input, 0);
  27.  
  28. let arrLength = +gets();
  29.  
  30. let previous = 0; //7//3//2//3//5//2//2//4
  31. let counter = 1; //1//2
  32. let candidateCounter = 0;
  33.  
  34. for (let i = 0; i < arrLength; i++) {
  35. let element = +gets(); //3
  36. if (arrLength === 1) {
  37. candidateCounter++;
  38. }
  39. if (previous === 0) {
  40. previous = element;
  41. continue;
  42. }
  43. if (element > previous) {
  44. counter++;
  45. previous = element;
  46. } else {
  47. if (counter > 1) {
  48. candidateCounter = counter;
  49. counter = 0;
  50. }
  51. previous = element;
  52. }
  53. }
  54. print(candidateCounter);
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement