📌  相关文章
📜  计算用给定数组的偶积构造数组的方法,以使相同索引元素的绝对差最大为1

📅  最后修改于: 2021-05-17 22:37:46             🧑  作者: Mango

给定大小为N的数组A [] ,任务是计算构造大小为N的数组B []的方式数,以使相同索引元素的绝对差必须小于或等于1 ,即abs(A [i] – B [i]) ≤1 ,并且数组B []的元素乘积必须为偶数。

例子:

方法:想法是首先计算构造数组B []的方法数,以使abs(A [i] – B [i])<= 1 ,然后删除那些元素乘积不为偶数的数组数字。请按照以下步骤解决问题:

  • 使得abs(A [i] – B [i])<= 1B [i]可能值为{A [i],A [i] + 1,A [i] – 1} 。因此,构造abs(A [i] – B [i])小于或等于1的数组B []的总数为3 N。
  • 遍历数组并将偶数计数存储在数组A []中,例如X。
  • 如果A [i]是偶数,则(A [i] – 1)(A [i] +1)必须是奇数。因此,乘积不是偶数的构造数组B []的总数为2 X。
  • 最后,打印( 3 N – 2 X )的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find count the ways to construct
// an array, B[] such that abs(A[i] - B[i]) <=1
// and product of elements of B[] is even
void cntWaysConsArray(int A[], int N)
{
 
    // Stores count of arrays B[] such
    // that abs(A[i] - B[i]) <=1
    int total = 1;
 
    // Stores count of arrays B[] whose
    // product of elements is not even
    int oddArray = 1;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update total
        total = total * 3;
 
        // If A[i] is an even number
        if (A[i] % 2 == 0) {
 
            // Update oddArray
            oddArray *= 2;
        }
    }
 
    // Print 3^N - 2^X
    cout << total - oddArray << "\n";
}
 
// Driver Code
int main()
{
    int A[] = { 2, 4 };
    int N = sizeof(A) / sizeof(A[0]);
 
    cntWaysConsArray(A, N);
 
    return 0;
}


Java
// Java Program to implement the
// above approach
import java.util.*;
class GFG
{
  
// Function to find count the ways to construct
// an array, B[] such that abs(A[i] - B[i]) <=1
// and product of elements of B[] is even
static void cntWaysConsArray(int A[], int N)
{
 
    // Stores count of arrays B[] such
    // that abs(A[i] - B[i]) <=1
    int total = 1;
 
    // Stores count of arrays B[] whose
    // product of elements is not even
    int oddArray = 1;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // Update total
        total = total * 3;
 
        // If A[i] is an even number
        if (A[i] % 2 == 0)
        {
 
            // Update oddArray
            oddArray *= 2;
        }
    }
 
    // Print 3^N - 2^X
    System.out.println( total - oddArray);
}
  
// Driver Code
public static void main(String[] args)
{
    int A[] = { 2, 4 };
    int N = A.length;
    cntWaysConsArray(A, N);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program to implement
# the above approach
 
# Function to find count the ways to construct
# an array, B[] such that abs(A[i] - B[i]) <=1
# and product of elements of B[] is even
def cntWaysConsArray(A, N) :
 
    # Stores count of arrays B[] such
    # that abs(A[i] - B[i]) <=1
    total = 1;
 
    # Stores count of arrays B[] whose
    # product of elements is not even
    oddArray = 1;
 
    # Traverse the array
    for i in range(N) :
 
        # Update total
        total = total * 3;
 
        # If A[i] is an even number
        if (A[i] % 2 == 0) :
 
            # Update oddArray
            oddArray *= 2;
   
    # Print 3^N - 2^X
    print(total - oddArray);
 
# Driver Code
if __name__ == "__main__" :
    A = [ 2, 4 ];
    N = len(A);
    cntWaysConsArray(A, N);
     
    # This code is contributed by AnkThon


C#
// C# program to implement the
// above approach
using System;
 
class GFG{
  
// Function to find count the ways to construct
// an array, []B such that abs(A[i] - B[i]) <=1
// and product of elements of []B is even
static void cntWaysConsArray(int []A, int N)
{
     
    // Stores count of arrays []B such
    // that abs(A[i] - B[i]) <=1
    int total = 1;
 
    // Stores count of arrays []B whose
    // product of elements is not even
    int oddArray = 1;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Update total
        total = total * 3;
 
        // If A[i] is an even number
        if (A[i] % 2 == 0)
        {
             
            // Update oddArray
            oddArray *= 2;
        }
    }
 
    // Print 3^N - 2^X
    Console.WriteLine(total - oddArray);
}
  
// Driver Code
public static void Main(String[] args)
{
    int []A = { 2, 4 };
    int N = A.Length;
     
    cntWaysConsArray(A, N);
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
5

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