给定大小为N的数组arr [] ,任务是从给定数组中找到非空子序列的数量,以使子序列中没有两个相邻元素具有相同的奇偶性。
例子:
Input: arr[] = [5, 6, 9, 7]
Output: 9
Explanation:
All such subsequences of given array will be {5}, {6}, {9}, {7}, {5, 6}, {6, 7}, {6, 9}, {5, 6, 9}, {5, 6, 7}.
Input: arr[] = [2, 3, 4, 8]
Output: 9
幼稚的方法:生成所有非空子序列,并选择具有交替的奇偶或奇数的子序列,并对所有这些子序列进行计数以获得答案。
时间复杂度: O(2 N )
高效方法:
可以使用动态编程来优化上述方法。请按照以下步骤解决问题:
- 考虑一个尺寸为(N + 1)*(2)的dp []矩阵。
- dp [i] [0]存储子序列的计数,直到第i个索引以偶数元素结尾。
- dp [i] [1]存储子序列的计数,直到第i个索引以奇数元素结尾。
- 因此,对于每个第i个元素,请检查该元素是偶数还是奇数,然后通过包含和排除第i个元素来进行操作。
- 因此,如果第i个元素为奇数,则为递归关系:
dp[i][1] = dp[i – 1][0] (Including the ith element by considering all subsequences ending with even element till (i – 1)th index) + 1 + dp[i – 1][1] (Excluding the ith element)
- 同样,如果第i个元素是偶数:
dp[i][0] = dp[i – 1][1] (Including the ith element by considering all subsequences ending with odd element till (i – 1)th index) + 1 + dp[i – 1][0] (Excluding the ith element)
- 最后,要求答案是dp [n] [0]的总和,它包含以偶数元素结尾的所有此类子序列,而dp [n] [1]包含以奇数元素结尾的所有此类子序列。
下面是上述方法的实现:
C++
// C++ Program to implement the
// above approach
#include
using namespace std;
// Function to find required subsequences
int validsubsequences(int arr[], int n)
{
// dp[i][0]: Stores the number of
// subsequences till i-th index
// ending with even element
// dp[i][1]: Stores the number of
// subsequences till i-th index
// ending with odd element
long long int dp[n + 1][2];
// Initialise the dp[][] with 0.
for (int i = 0; i < n + 1; i++) {
dp[i][0] = 0;
dp[i][1] = 0;
}
for (int i = 1; i <= n; i++) {
// If odd element is
// encountered
if (arr[i - 1] % 2) {
// Considering i-th element
// will be present in
// the subsequence
dp[i][1] += 1;
// Appending i-th element to all
// non-empty subsequences
// ending with even element
// till (i-1)th indexes
dp[i][1] += dp[i - 1][0];
// Considering ith element will
// not be present in
// the subsequence
dp[i][1] += dp[i - 1][1];
dp[i][0] += dp[i - 1][0];
}
else {
// Considering i-th element
// will be present in
// the subsequence
dp[i][0] += 1;
// Appending i-th element to all
// non-empty subsequences
// ending with odd element
// till (i-1)th indexes
dp[i][0] += dp[i - 1][1];
// Considering ith element will
// not be present in
// the subsequence
dp[i][0] += dp[i - 1][0];
dp[i][1] += dp[i - 1][1];
}
}
// Count of all valid subsequences
return dp[n][0] + dp[n][1];
}
// Driver Code
int main()
{
int arr[] = { 5, 6, 9, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << validsubsequences(arr, n);
return 0;
}
Java
// Java Program implementation
// of the approach
import java.util.*;
import java.io.*;
class GFG{
// Function to find required subsequences
static int validsubsequences(int arr[], int n)
{
// dp[i][0]: Stores the number of
// subsequences till i-th index
// ending with even element
// dp[i][1]: Stores the number of
// subsequences till i-th index
// ending with odd element
long dp[][] = new long [n + 1][2];
// Initialise the dp[][] with 0.
for(int i = 0; i < n + 1; i++)
{
dp[i][0] = 0;
dp[i][1] = 0;
}
for(int i = 1; i <= n; i++)
{
// If odd element is
// encountered
if (arr[i - 1] % 2 != 0)
{
// Considering i-th element
// will be present in
// the subsequence
dp[i][1] += 1;
// Appending i-th element to all
// non-empty subsequences
// ending with even element
// till (i-1)th indexes
dp[i][1] += dp[i - 1][0];
// Considering ith element will
// not be present in
// the subsequence
dp[i][1] += dp[i - 1][1];
dp[i][0] += dp[i - 1][0];
}
else
{
// Considering i-th element
// will be present in
// the subsequence
dp[i][0] += 1;
// Appending i-th element to all
// non-empty subsequences
// ending with odd element
// till (i-1)th indexes
dp[i][0] += dp[i - 1][1];
// Considering ith element will
// not be present in
// the subsequence
dp[i][0] += dp[i - 1][0];
dp[i][1] += dp[i - 1][1];
}
}
// Count of all valid subsequences
return (int)(dp[n][0] + dp[n][1]);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 5, 6, 9, 7 };
int n = arr.length;
System.out.print(validsubsequences(arr, n));
}
}
// This code is contributed by code_hunt
Python3
# Python3 program to implement the
# above approach
# Function to find required subsequences
def validsubsequences(arr, n):
# dp[i][0]: Stores the number of
# subsequences till i-th index
# ending with even element
# dp[i][1]: Stores the number of
# subsequences till i-th index
# ending with odd element
# Initialise the dp[][] with 0.
dp = [[0 for i in range(2)]
for j in range(n + 1)]
for i in range(1, n + 1):
# If odd element is
# encountered
if(arr[i - 1] % 2):
# Considering i-th element
# will be present in
# the subsequence
dp[i][1] += 1
# Appending i-th element to all
# non-empty subsequences
# ending with even element
# till (i-1)th indexes
dp[i][1] += dp[i - 1][0]
# Considering ith element will
# not be present in
# the subsequence
dp[i][1] += dp[i - 1][1]
dp[i][0] += dp[i - 1][0]
else:
# Considering i-th element
# will be present in
# the subsequence
dp[i][0] += 1
# Appending i-th element to all
# non-empty subsequences
# ending with odd element
# till (i-1)th indexes
dp[i][0] += dp[i - 1][1]
# Considering ith element will
# not be present in
# the subsequence
dp[i][0] += dp[i - 1][0]
dp[i][1] += dp[i - 1][1]
# Count of all valid subsequences
return dp[n][0] + dp[n][1]
# Driver code
if __name__ == '__main__':
arr = [ 5, 6, 9, 7 ]
n = len(arr)
print(validsubsequences(arr, n))
# This code is contributed by Shivam Singh
C#
// C# program implementation
// of the approach
using System;
class GFG{
// Function to find required subsequences
static int validsubsequences(int[] arr, int n)
{
// dp[i][0]: Stores the number of
// subsequences till i-th index
// ending with even element
// dp[i][1]: Stores the number of
// subsequences till i-th index
// ending with odd element
long[,] dp = new long [n + 1, 2];
// Initialise the dp[][] with 0.
for(int i = 0; i < n + 1; i++)
{
dp[i, 0] = 0;
dp[i, 1] = 0;
}
for(int i = 1; i <= n; i++)
{
// If odd element is
// encountered
if (arr[i - 1] % 2 != 0)
{
// Considering i-th element
// will be present in
// the subsequence
dp[i, 1] += 1;
// Appending i-th element to all
// non-empty subsequences
// ending with even element
// till (i-1)th indexes
dp[i, 1] += dp[i - 1, 0];
// Considering ith element will
// not be present in
// the subsequence
dp[i, 1] += dp[i - 1, 1];
dp[i, 0] += dp[i - 1, 0];
}
else
{
// Considering i-th element
// will be present in
// the subsequence
dp[i, 0] += 1;
// Appending i-th element to all
// non-empty subsequences
// ending with odd element
// till (i-1)th indexes
dp[i, 0] += dp[i - 1, 1];
// Considering ith element will
// not be present in
// the subsequence
dp[i, 0] += dp[i - 1, 0];
dp[i, 1] += dp[i - 1, 1];
}
}
// Count of all valid subsequences
return (int)(dp[n, 0] + dp[n, 1]);
}
// Driver code
public static void Main()
{
int[] arr = { 5, 6, 9, 7 };
int n = arr.Length;
Console.Write(validsubsequences(arr, n));
}
}
// This code is contributed by chitranayal
9
时间复杂度: O(N)
辅助空间复杂度: O(N)