Skip to content
Advertisement

How do you group an array by n and the number of decreasing integers (n-1)? With the output to be the total number of arrays

For example, this is the input array: [2, 1, 4, 4, 3]

From this array, n-1 patterns will be established from left to right.

This output would be the number 7 because the following separate arrays exist after grouping:

[2] [1] [4] [4] [3] – 1 group (n)

[4, 3] – 1 group (n-1)

[2, 1] – 1 group (n-1)

Output: 7 (arrays)

This is what I started so far, but it looks like I just summed everything together.

JavaScript

It would be appreciated if the solution and an explanation is provided in JavaScript. Thank you!

Advertisement

Answer

Script:

JavaScript

Get the number of unique elements then add it to the length of the filtered unique array where it also contains n-1.

Output:

output

If you want to get the arrays:

JavaScript

output2

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement