Related Articles

# Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1

• Difficulty Level : Hard
• Last Updated : 04 May, 2021

Given an array arr[] of N elements. The task is to find the number of sub-sequences which have at least two consecutive elements such that absolute difference between them is ≤ 1
Examples:

Input: arr[] = {1, 6, 2, 1}
Output:
{1, 2}, {1, 2, 1}, {2, 1}, {6, 2, 1}, {1, 1} and {1, 6, 2, 1}
are the sub-sequences that have at least one consecutive pair
with difference less than or equal to 1.
Input: arr[] = {1, 6, 2, 1, 9}
Output: 12

Naive approach: The idea is to find all the possible sub-sequences and check if there exist sub-sequence with any consecutive pair with difference ≤1 and increase the count.
Efficient approach: The idea is to iterate over the given array and for each ith-element, try to find the required sub-sequence ending with ith element as its last element.
For every i, we want to use arr[i], arr[i] -1, arr[i] + 1, so we will define 2D array, dp[][], where dp[i] will contain the number of sub sequence that do not have any consecutive pair with difference less than 1 and dp[i] contain the number of sub sequence having any consecutive pair with difference ≤1.
Also, we will maintain two variables required_subsequence and not_required_subsdequence to maintain the count of sub sequences which have at least one consecutive element with difference ≤1 and count of sub-sequences which do not contain any consecutive element pair with difference ≤1.
Now, considering the sub-array arr …. arr[i], we will perform the following steps:

1. Compute the number of sub sequences which do not have any consecutive pair with difference less than 1 but will have by adding the ith element in the sub sequence. These are basically sum of dp[arr[i] + 1], dp[arr[i] – 1] and dp[arr[i]].
2. Total number of sub sequences have at least one consecutive pair with difference at least 1 and ending at i is equal to total sub-sequences found till i (just append arr[i] at the last) + sub sequences which turns into sub sequence have at least consecutive pair with difference less than 1 on adding arr[i].
3. Total sub sequence which do not have any consecutive pair with difference less than 1 and ending at i = total sub-sequence which do not have any consecutive pair with difference less than 1 before i + 1 (just the current element as a sub sequence).
4. Update required_sub-sequence, not_required_subsequence and dp[arr[i]] and the final answer will be required_subsequence.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;``const` `int` `N = 10000;` `// Function to return the number of subsequences``// which have at least one consecutive pair``// with difference less than or equal to 1``int` `count_required_sequence(``int` `n, ``int` `arr[])``{``    ``int` `total_required_subsequence = 0;``    ``int` `total_n_required_subsequence = 0;``    ``int` `dp[N];``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Not required sub-sequences which``        ``// turn required on adding i``        ``int` `turn_required = 0;``        ``for` `(``int` `j = -1; j <= 1; j++)``            ``turn_required += dp[arr[i] + j];` `        ``// Required sub-sequence till now will be``        ``// required sequence plus sub-sequence``        ``// which turns required``        ``int` `required_end_i = (total_required_subsequence``                              ``+ turn_required);` `        ``// Similarly for not required``        ``int` `n_required_end_i = (1 + total_n_required_subsequence``                                ``- turn_required);` `        ``// Also updating total required and``        ``// not required sub-sequences``        ``total_required_subsequence += required_end_i;``        ``total_n_required_subsequence += n_required_end_i;` `        ``// Also, storing values in dp``        ``dp[arr[i]] += required_end_i;``        ``dp[arr[i]] += n_required_end_i;``    ``}` `    ``return` `total_required_subsequence;``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 6, 2, 1, 9 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``);` `    ``cout << count_required_sequence(n, arr) << ``"\n"``;` `    ``return` `0;``}`

## Java

 `// Java implemenation of above approach``public` `class` `GFG``{``    ` `static` `int` `N = ``10000``;` `// Function to return the number of subsequences``// which have at least one consecutive pair``// with difference less than or equal to 1``static` `int` `count_required_sequence(``int` `n, ``int` `arr[])``{``    ``int` `total_required_subsequence = ``0``;``    ``int` `total_n_required_subsequence = ``0``;``    ``int` `[][]dp = ``new` `int``[N][``2``];``    ``for` `(``int` `i = ``0``; i < n; i++)``    ``{` `        ``// Not required sub-sequences which``        ``// turn required on adding i``        ``int` `turn_required = ``0``;``        ``for` `(``int` `j = -``1``; j <= ``1``; j++)``            ``turn_required += dp[arr[i] + j][``0``];` `        ``// Required sub-sequence till now will be``        ``// required sequence plus sub-sequence``        ``// which turns required``        ``int` `required_end_i = (total_required_subsequence``                            ``+ turn_required);` `        ``// Similarly for not required``        ``int` `n_required_end_i = (``1` `+ total_n_required_subsequence``                                ``- turn_required);` `        ``// Also updating total required and``        ``// not required sub-sequences``        ``total_required_subsequence += required_end_i;``        ``total_n_required_subsequence += n_required_end_i;` `        ``// Also, storing values in dp``        ``dp[arr[i]][``1``] += required_end_i;``        ``dp[arr[i]][``0``] += n_required_end_i;``    ``}` `    ``return` `total_required_subsequence;``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `arr[] = { ``1``, ``6``, ``2``, ``1``, ``9` `};``    ``int` `n = arr.length;` `    ``System.out.println(count_required_sequence(n, arr));``}``}` `// This code has been contributed by 29AjayKumar`

## Python3

 `# Python3 implementation of the approach``import` `numpy as np;` `N ``=` `10000``;` `# Function to return the number of subsequences``# which have at least one consecutive pair``# with difference less than or equal to 1``def` `count_required_sequence(n, arr) :``    ` `    ``total_required_subsequence ``=` `0``;``    ``total_n_required_subsequence ``=` `0``;``    ``dp ``=` `np.zeros((N,``2``));``    ` `    ``for` `i ``in` `range``(n) :` `        ``# Not required sub-sequences which``        ``# turn required on adding i``        ``turn_required ``=` `0``;``        ``for` `j ``in` `range``(``-``1``, ``2``,``1``) :``            ``turn_required ``+``=` `dp[arr[i] ``+` `j][``0``];` `        ``# Required sub-sequence till now will be``        ``# required sequence plus sub-sequence``        ``# which turns required``        ``required_end_i ``=` `(total_required_subsequence``                            ``+` `turn_required);` `        ``# Similarly for not required``        ``n_required_end_i ``=` `(``1` `+` `total_n_required_subsequence``                                ``-` `turn_required);` `        ``# Also updating total required and``        ``# not required sub-sequences``        ``total_required_subsequence ``+``=` `required_end_i;``        ``total_n_required_subsequence ``+``=` `n_required_end_i;` `        ``# Also, storing values in dp``        ``dp[arr[i]][``1``] ``+``=` `required_end_i;``        ``dp[arr[i]][``0``] ``+``=` `n_required_end_i;``        ` `    ``return` `total_required_subsequence;`  `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``1``, ``6``, ``2``, ``1``, ``9` `];``    ``n ``=` `len``(arr);` `    ``print``(count_required_sequence(n, arr)) ;` `# This code is contributed by AnkitRai01`

C#

``````
// C# implementation of the above approach
using System;

class GFG
{

static int N = 10000;

// Function to return the number of subsequences
// which have at least one consecutive pair
// with difference less than or equal to 1
static int count_required_sequence(int n, int []arr)
{
int total_required_subsequence = 0;
int total_n_required_subsequence = 0;
int [, ]dp = new int[N, 2];
for (int i = 0; i < n; i++)
{

// Not required sub-sequences which
// turn required on adding i
int turn_required = 0;
for (int j = -1; j <= 1; j++)
turn_required += dp[arr[i] + j, 0];

// Required sub-sequence till now will be
// required sequence plus sub-sequence
// which turns required
int required_end_i = (total_required_subsequence
+ turn_required);

// Similarly for not required
int n_required_end_i = (1 + total_n_required_subsequence
- turn_required);

// Also updating total required and
// not required sub-sequences
total_required_subsequence += required_end_i;
total_n_required_subsequence += n_required_end_i;

// Also, storing values in dp
dp[arr[i], 1] += required_end_i;
dp[arr[i], 0] += n_required_end_i;
}

}

// Driver code
public static void Main()
{
int [] arr = new int [] { 1, 6, 2, 1, 9 };
int n = arr.Length;

Console.WriteLine(count_required_sequence(n, arr));
}
}

// This code has been contributed by ihritik
``````

## Javascript

 ``
Output:
`12`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up