Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given an integer array arr, consisting of N integers.
- Find the number of non-decreasing consecutive subsequences in arr.
- Every sequence starts after the previous one. For example:
- if the array arr consists of the numbers 1, 2, -3, 4, 4, 0, 1,
- the number of non-decreasing consecutive subsequences is 3 (the first is 1, 2,
- the second is -3, 4, 4 and the third is 0, 1).
- Each of the strings represents an integer. Element 0 of the array is the number N.
- Next N elements (from 1 to N) construct the array arr.
- Your method should return a single number - the number of non-decreasing
- consecutive subsequences. */
- "use strict";
- function solve(args) {
- var num = parseInt(args[0]);
- var numbers = new Array(num);
- var i;
- for (i = 1; i < args.length; i += 1) {
- numbers[i - 1] = parseInt(args[i]);
- }
- var subsequnces = 0;
- if (numbers.length > 1) {
- subsequnces += 1;
- for (i = 1; i < numbers.length; i += 1) {
- var previous = numbers[i - 1];
- var current = numbers[i];
- if (current < previous) {
- subsequnces += 1;
- }
- }
- }
- console.log(subsequnces);
- }
- solve(['7', '1', '2', '-3', '4', '4', '0', '1']);
- solve(['6', '1', '3', '-5', '8', '7', '-6']);
- solve(['9', '1', '8', '8', '7', '6', '5', '7', '7', '6']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement