Close Subsequences
Practice
3.9 (9 votes)
Algorithms
Easy Medium
Dp
Dynamic programming
Dynamic programming
Problem
86% Success 1639 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Given an array $$A$$ , 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 $$B$$ has $$m$$ elements, then it is a good subsequence if there exists at least one index $$i$$ ($$2 \le i \le m$$) such that $$abs(B_i - B_{i-1}) \le 1$$.

You have to find the total number of good subsequences of array A modulo $$10^9+7$$.

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 $$N$$ ($$2 \le n \le 10^5$$), denoting the number of elements in A

The second line contains $$N$$ integers $$A_1, A_2, \ldots, A_n$$ ($$1 \le A_i \le 10^5$$).

Output Format

Output the total number of good subsequences of the array $$A$$ modulo $$10^9$$+7

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
18 votes
Tags:
Dynamic Programminggcddp on digitsdigit dpGreatest common divisormediumAlgorithmsEasy-MediumDigit DParithemtic operationsGreatest common divisorDynamic programming
Points:30
3 votes
Tags:
Dynamic ProgrammingAlgorithmsEasy-MediumDynamic programming
Points:30
7 votes
Tags:
AlgorithmsApprovedEasy-MediumDynamic programming