// C# program for the
// above approach
using System;
class GFG{
// Function to find total number of
// possible arrangements of array
static int waysForPairwiseSumToBeK(int i, int rem,
int previous,
int N, int [,,]dp)
{
// Base Case
if (i == N)
{
if (rem == 0)
return 1;
else
return 0;
}
// If rem exceeds
// 'k' return 0
if (rem < 0)
return 0;
// Return the already
// calculated states
if (dp[i, rem, previous] != -1)
return dp[i, rem, previous];
int ways = 0;
// Place a '0' at current position
ways += waysForPairwiseSumToBeK(i + 1, rem,
0, N, dp);
// Place a '1' at current position
// Add it to previous value
ways += waysForPairwiseSumToBeK(i + 1, rem -
(previous),
1, N, dp);
// Place a '2' at current position.
// Add it to previous value.
ways += waysForPairwiseSumToBeK(i + 1, rem -
(2 * previous),
2, N, dp);
// Store the current state result
// return the same result
dp[i, rem, previous] = ways;
return ways;
}
// Function to find number of possible
// arrangements of array with 0, 1, and
// 2 having pairwise product sum K
static void countOfArrays(int i, int rem,
int previous, int N)
{
// Store the overlapping states
int [,,]dp = new int[ 15, 15, 3 ];
// Initialize dp table with -1
for(int p = 0; p < 15; p++)
{
for(int q = 0; q < 15; q++)
{
for(int r = 0; r < 3; r++)
dp[p, q, r] = -1;
}
}
// Stores total number of ways
int totWays = waysForPairwiseSumToBeK(i, rem,
previous,
N, dp);
// Print number of ways
Console.Write(totWays);
}
// Driver Code
public static void Main(String []args)
{
// Given N and K
int N = 4, K = 3;
// Function Call
countOfArrays(0, K, 0, N);
}
}
// This code is contributed by gauravrajput1