Advertisement
svephoto

Non-Decreasing Subsequence [JavaScript]

Dec 11th, 2021
1,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function nonDecreasingSubsequence(inputArray) {
  2.     let result = [];
  3.  
  4.     let currentBiggestElement = 0;
  5.  
  6.     for (let i = 0; i < inputArray.length; i++) {
  7.         if (inputArray[i] >= currentBiggestElement) {
  8.             currentBiggestElement = inputArray[i];
  9.             result.push(inputArray[i]);
  10.         } else {
  11.             continue;
  12.         }
  13.     }
  14.  
  15.     console.log(result.join(' ').trim());
  16. }
  17.  
  18. // For testing:
  19. // nonDecreasingSubsequence([1, 3, 8, 4, 10, 12, 3, 2, 24]);
  20. // nonDecreasingSubsequence([1, 2, 3, 4]);
  21. // nonDecreasingSubsequence([20, 3, 2, 15, 6, 1]);
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement