📜  偶数和奇数和的子序列数套装2

📅  最后修改于: 2021-06-25 17:06:59             🧑  作者: Mango

给定大小为N的数组arr [] 。任务是找到总和为偶数的子序列数和总和为奇数的子序列数。

例子:

方法:本文已经存在,时间复杂度为O(N)其中N是数组的大小。在继续之前,请先访问这里。

  • 如果我们可以找到奇数子序列的数量,那么我们可以轻松地找到偶数子序列的数量。
  • 奇数子序列可以通过两种方式形成:
    1. 通过奇数取奇数次。
    2. 取偶数和奇数为奇数时间。
  • 以下是一些变量及其定义:
    • N =数组中元素的总数。
    • 偶数=数组中的偶数总数。
    • 奇数=数组中奇数的总数。
    • Tseq =子序列总数。
    • Oseq =仅具有奇数的子序列总数。
    • Eseq =偶数个子序列的总数。
    • OddSumSeq =奇数和的子序列总数。
    • EvenSumSeq =偶数和的子序列总数。

下面是上述方法的实现:

C++
// CPP program to find number of
// Subsequences with Even and Odd Sum
#include 
using namespace std;
  
// Function to find number of
// Subsequences with Even and Odd Sum
pair countSum(int arr[], int n)
{
    int NumberOfOdds = 0, NumberOfEvens = 0;
  
    // Counting number of odds
    for (int i = 0; i < n; i++)
        if (arr[i] & 1)
            NumberOfOdds++;
  
    // Even count
    NumberOfEvens = n - NumberOfOdds;
  
    int NumberOfOddSubsequences = (1 << NumberOfEvens)
                                  * (1 << (NumberOfOdds - 1));
  
    // Total Subsequences is (2^n - 1)
    // For NumberOfEvenSubsequences subtract
    // NumberOfOddSubsequences from total
    int NumberOfEvenSubsequences = (1 << n) - 1
                                   - NumberOfOddSubsequences;
  
    return { NumberOfEvenSubsequences,
             NumberOfOddSubsequences };
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 3 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Calling the function
    pair ans = countSum(arr, n);
  
    cout << "EvenSum = " << ans.first;
    cout << " OddSum = " << ans.second;
  
    return 0;
}


Java
// Java program to find number of
// Subsequences with Even and Odd Sum
import java.util.*;
  
class GFG 
{
static class pair
{ 
    int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
}
  
// Function to find number of
// Subsequences with Even and Odd Sum
static pair countSum(int arr[], int n)
{
    int NumberOfOdds = 0, NumberOfEvens = 0;
  
    // Counting number of odds
    for (int i = 0; i < n; i++)
        if (arr[i] % 2 == 1)
            NumberOfOdds++;
  
    // Even count
    NumberOfEvens = n - NumberOfOdds;
  
    int NumberOfOddSubsequences = (1 << NumberOfEvens) * 
                                  (1 << (NumberOfOdds - 1));
  
    // Total Subsequences is (2^n - 1)
    // For NumberOfEvenSubsequences subtract
    // NumberOfOddSubsequences from total
    int NumberOfEvenSubsequences = (1 << n) - 1 - 
                                    NumberOfOddSubsequences;
  
    return new pair(NumberOfEvenSubsequences,
                    NumberOfOddSubsequences);
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 1, 2, 2, 3 };
  
    int n = arr.length;
  
    // Calling the function
    pair ans = countSum(arr, n);
  
    System.out.print("EvenSum = " + ans.first);
    System.out.print(" OddSum = " + ans.second);
}
} 
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to find number of 
# Subsequences with Even and Odd Sum 
  
# Function to find number of 
# Subsequences with Even and Odd Sum 
def countSum(arr, n) : 
  
    NumberOfOdds = 0; NumberOfEvens = 0; 
  
    # Counting number of odds 
    for i in range(n) : 
        if (arr[i] & 1) : 
            NumberOfOdds += 1; 
  
    # Even count 
    NumberOfEvens = n - NumberOfOdds; 
  
    NumberOfOddSubsequences = (1 << NumberOfEvens) * \
                              (1 << (NumberOfOdds - 1)); 
  
    # Total Subsequences is (2^n - 1) 
    # For NumberOfEvenSubsequences subtract 
    # NumberOfOddSubsequences from total 
    NumberOfEvenSubsequences = (1 << n) - 1 - \
                                NumberOfOddSubsequences; 
  
    return (NumberOfEvenSubsequences, 
            NumberOfOddSubsequences); 
  
# Driver code 
if __name__ == "__main__": 
  
    arr = [ 1, 2, 2, 3 ]; 
  
    n = len(arr); 
  
    # Calling the function 
    ans = countSum(arr, n); 
  
    print("EvenSum =", ans[0], end = " "); 
    print("OddSum =", ans[1]); 
  
# This code is contributed by AnkitRai01


C#
// C# program to find number of
// Subsequences with Even and Odd Sum
using System;
      
class GFG 
{
public class pair
{ 
    public int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
}
  
// Function to find number of
// Subsequences with Even and Odd Sum
static pair countSum(int []arr, int n)
{
    int NumberOfOdds = 0, NumberOfEvens = 0;
  
    // Counting number of odds
    for (int i = 0; i < n; i++)
        if (arr[i] % 2 == 1)
            NumberOfOdds++;
  
    // Even count
    NumberOfEvens = n - NumberOfOdds;
  
    int NumberOfOddSubsequences = (1 << NumberOfEvens) * 
                                  (1 << (NumberOfOdds - 1));
  
    // Total Subsequences is (2^n - 1)
    // For NumberOfEvenSubsequences subtract
    // NumberOfOddSubsequences from total
    int NumberOfEvenSubsequences = (1 << n) - 1 - 
                                    NumberOfOddSubsequences;
  
    return new pair(NumberOfEvenSubsequences,
                    NumberOfOddSubsequences);
}
  
// Driver code
public static void Main(String[] args) 
{
    int []arr = { 1, 2, 2, 3 };
  
    int n = arr.Length;
  
    // Calling the function
    pair ans = countSum(arr, n);
  
    Console.Write("EvenSum = " + ans.first);
    Console.Write(" OddSum = " + ans.second);
}
} 
  
// This code is contributed by 29AjayKumar


输出:
EvenSum = 7 OddSum = 8