给定一个包含N 个元素的数组arr[] 。任务是找到至少有两个连续元素的子序列的数量,使得它们之间的绝对差≤ 1 。
例子:
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
朴素的方法:这个想法是找到所有可能的子序列,并检查是否存在具有差值≤1的任何连续对的子序列并增加计数。
有效的方法:这个想法是迭代给定的数组,对于每个第 i 个元素,尝试找到所需的子序列,以第 i 个元素作为其最后一个元素。
对于每个 i,我们想使用 arr[i], arr[i] -1, arr[i] + 1,因此我们将定义二维数组dp[][] ,其中 dp[i][0] 将包含不存在差值小于1的连续对的子序列的个数,dp[i][1]包含具有差值≤1的连续对的子序列的个数。
此外,我们将维护两个变量required_subsequence和not_required_subsdequence来维护具有至少一个差值≤1的连续元素的子序列的计数和不包含任何差值≤1的连续元素对的子序列的计数。
现在,考虑子数组 arr[1] …. arr[i],我们将执行以下步骤:
- 通过在子序列中添加第 i 个元素,计算没有任何连续对且差值小于 1 的子序列的数量。这些基本上是 dp[arr[i] + 1][0]、dp[arr[i] – 1][0] 和 dp[arr[i]][0] 的总和。
- 子序列的总数至少有一个连续对,差异至少为 1 并且在 i 处结束等于找到直到 i 的子序列总数(只在最后附加 arr[i])+ 变成子序列的子序列有在添加 arr[i] 时,至少连续对的差异小于 1。
- 没有任何差值小于 1 的连续对且以 i 结束的总子序列 = 在 i + 1 之前没有任何差值小于 1 的连续对的总子序列(仅将当前元素作为子序列) .
- 更新 required_sub-sequence、not_required_subsequence 和 dp[arr[i][0]],最终答案将是 required_subsequence。
下面是上述方法的实现:
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][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 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;
}
return total_required_subsequence;
}
// 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
12
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。