Close Subsequences

3.9

9 votes
Algorithms, Easy-Medium, dp, Dynamic Programming, Dynamic programming
Problem

Given an array , a subsequence of A is said to be good subsequence, if it has at least one pair of consecutive elements such that their absolute difference is less than equal to 1.

Formally, if the subsequence has elements, then it is a good subsequence if there exists at least one index () such that .

You have to find the total number of good subsequences of array A modulo .

Obviously, a good subsequence has to have at least 2 elements in it.

For example if sequence has 3 elements [3, 4, 5], subsequence [3, 4], [4, 5] and [3, 4, 5] are good subsequnces. So our answer will be 3 in this case.

Input Format

The first line contains a single integer (), denoting the number of elements in A

The second line contains integers ().

Output Format

Output the total number of good subsequences of the array modulo +7

Sample Input
5
1 6 2 1 9
Sample Output
12
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

[1, 2], [1, 2, 1], [1, 2, 1, 9], etc. are some of the good subsequences. Total 12 are there in the given sample.

Editor Image

?