Advertisement
dimipan80

Exams - Sequences

Dec 29th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given an integer array arr, consisting of N integers.
  2.  Find the number of non-decreasing consecutive subsequences in arr.
  3.  Every sequence starts after the previous one. For example:
  4.  if the array arr consists of the numbers 1, 2, -3, 4, 4, 0, 1,
  5.  the number of non-decreasing consecutive subsequences is 3 (the first is 1, 2,
  6.  the second is -3, 4, 4 and the third is 0, 1).
  7.  Each of the strings represents an integer. Element 0 of the array is the number N.
  8.  Next N elements (from 1 to N) construct the array arr.
  9.  Your method should return a single number - the number of non-decreasing
  10.  consecutive subsequences. */
  11.  
  12. "use strict";
  13.  
  14. function solve(args) {
  15.     var num = parseInt(args[0]);
  16.     var numbers = new Array(num);
  17.     var i;
  18.     for (i = 1; i < args.length; i += 1) {
  19.         numbers[i - 1] = parseInt(args[i]);
  20.     }
  21.    
  22.     var subsequnces = 0;
  23.     if (numbers.length > 1) {
  24.         subsequnces += 1;
  25.         for (i = 1; i < numbers.length; i += 1) {
  26.             var previous = numbers[i - 1];
  27.             var current = numbers[i];
  28.             if (current < previous) {
  29.                 subsequnces += 1;
  30.             }
  31.         }
  32.     }
  33.    
  34.     console.log(subsequnces);
  35. }
  36.  
  37. solve(['7', '1', '2', '-3', '4', '4', '0', '1']);
  38. solve(['6', '1', '3', '-5', '8', '7', '-6']);
  39. solve(['9', '1', '8', '8', '7', '6', '5', '7', '7', '6']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement