📌  相关文章
📜  具有不同奇偶校验的相邻元素的所有子序列的计数

📅  最后修改于: 2021-09-04 11:43:07             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是从给定数组中找到非空子序列的数量,使得该子序列的两个相邻元素没有相同的奇偶校验

例子:

原始的方法:生成所有非空的子序列,并选择使用替代奇偶奇偶号码的人并计算所有这些子序列,以获得答案。
时间复杂度: O(2 N )
有效的方法:
上述方法可以使用动态规划进行优化。请按照以下步骤解决问题:

  • 考虑一个维度为(N+1)*(2)dp[]矩阵。
  • dp[i][0]存储子序列的计数,直到i索引偶数元素结尾。
  • dp[i][1]存储子序列的计数,直到i索引奇数元素结尾。
  • 因此,对于每个i元素,检查该元素是偶数还是奇数,然后继续包括和排除第i元素
  • 因此,如果第 i元素为奇数,则递推关系为:
  • 同样,如果第 i元素是偶数:
  • 最后,包含所有以偶数元素结尾的子序列的 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


Javascript


输出:
9

时间复杂度: O(N)
辅助空间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live