Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
Last Updated :
25 May, 2022
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: 6
{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 exists a 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][0] will contain the number of sub sequence that do not have any consecutive pair with difference less than 1 and dp[i][1] 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 subsequences 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[1] .... arr[i], we will perform the following steps:
- 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][0], dp[arr[i] - 1][0] and dp[arr[i]][0].
- Total number of subsequences 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) + subsequences which turns into subsequence have at least consecutive pair with difference less than 1 on adding arr[i].
- Total subsequence 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 subsequence).
- Update required_sub-sequence, not_required_subsequence and dp[arr[i][0]] and the final answer will be required_subsequence.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
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][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
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 implementation 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#
using System;
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
static public void Main ()
{
int[] arr = { 1, 6, 2, 1, 9 };
int n = arr.Length;
Console.WriteLine(count_required_sequence(n, arr));
}
}
// This code is contributed by rag2127.
JavaScript
<script>
// Javascript implementation of the approach
var N = 10000;
// Function to return the number of subsequences
// which have at least one consecutive pair
// with difference less than or equal to 1
function count_required_sequence(n, arr)
{
var total_required_subsequence = 0;
var total_n_required_subsequence = 0;
var dp = Array.from(Array(N), ()=> Array(2).fill(0));
for (var i = 0; i < n; i++) {
// Not required sub-sequences which
// turn required on adding i
var turn_required = 0;
for (var 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
var required_end_i = (total_required_subsequence
+ turn_required);
// Similarly for not required
var 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
var arr = [ 1, 6, 2, 1, 9 ];
var n = arr.length;
document.write( count_required_sequence(n, arr) + "<br>");
</script>
Time Complexity: O(N), where N represents the size of the given array.
Auxiliary Space: O(N), where N represents the size of the given array.
Similar Reads
Maximum Subsequence sum with difference among consecutive numbers less than K
Given an array arr[], find the maximum sum that can be achieved by picking a subsequence such that the difference between consecutive elements in the subsequence is less than or equal to a given number 'K'. Examples: Input: arr[] = {1, 8, 9, 4, 6, 7}, K = 2Output: 24. Explanation: The maximum sum ca
7 min read
Count of elements such that difference between sum of left and right sub arrays is equal to a multiple of k
Given an array arr[] of length n and an integer k, the task is to find the number of indices from 2 to n-1 in an array having a difference of the sum of the left and right sub arrays equal to the multiple of the given number k. Examples: Input: arr[] = {1, 2, 3, 3, 1, 1}, k = 4 Output: 2 Explanation
7 min read
Print all sequences starting with n and consecutive difference limited to k
Given three positive integer n, s and k. The task is to print all possible sequence of length s, starting with n and the absolute difference between consecutive element is less than k.Examples : Input : n = 5, s = 3, k = 2 Output : 5 5 5 5 5 6 5 5 4 5 6 6 5 6 7 5 6 5 5 4 4 5 4 5 5 4 3 Input : n = 3,
9 min read
Minimum sum subsequence such that at least one of every four consecutive elements is picked
Given an array arr[] of positive integers. The task is to find minimum sum subsequence from the array such that at least one value among all groups of four consecutive elements is picked. Examples : Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}Output: 66 is sum of output subsequence {1, 5}Note that we hav
15+ min read
Sequence with sum K and minimum sum of absolute differences between consecutive elements
Given two integers N and K, the task is to find a sequence of integers of length N such that the sum of all the elements of the sequence is K and the sum of absolute differences between all consecutive elements is minimum. Print this minimized sum. Examples: Input: N = 3, K = 56 Output: 1 The sequen
4 min read
Maximum Sum Subsequence made up of consecutive elements of different parity
Given an array arr[] consisting of N integers, the task is to find the maximum sum of a non-empty subsequence such that each pair of consecutive terms is of different parity (even or odd). Examples: Input: arr[] = {1, 2, 6, 8, -5, 10}Output: 14Explanation: Considering the subsequence {1, 8, -5, 10}
9 min read
Count of subarrays whose product is equal to difference of two different numbers
Given a non-negative array a, the task is to find the count of subarrays whose product of elements can be represented as the difference of two different numbers. Examples: Input: arr = {2, 5, 6} Output: 2 Explanation: Product of elements of subarray {5} can be represented as 32 - 22 is equal to 5 Pr
13 min read
Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
Given two integers N and K where K < N, the task is to generate a permutation of integers from 1 to N such that the absolute difference of all the consecutive integers give exactly K distinct integers. Examples: Input: N = 3, K = 2 Output: 1 3 2 |1 - 3| = 2 and |3 - 2| = 1 which gives 2 distinct
8 min read
Subarray with difference between maximum and minimum element greater than or equal to its length
Given an array arr[], the task is to find a subarray with the difference between the maximum and the minimum element is greater than or equal to the length of subarray. If no such subarray exists then print -1.Examples: Input: arr[] = {3, 7, 5, 1} Output: 3 7 |3 - 7| > length({3, 7}) i.e. 4 ⥠2In
4 min read
Length of the longest subsequence such that XOR of adjacent elements is equal to K
Given an array arr[] of N non-negative integers and an integer K, the idea is to find the length of the longest subsequence having Xor of adjacent elements equal to K. Examples: Input: N = 5, arr[] = {3, 2, 4, 3, 5}, K = 1Output: 3Explanation:All the subsequences having Xor of adjacent element equal
13 min read