Number of permutations such that pair of indices having odd sum have parity = K
Last Updated :
28 Feb, 2024
Given an array arr[] of N distinct elements along with an integer K (0 or 1). The task is to find the number of permutations of arr[] such that all possible pairs of indices (i, j) having odd sum ((i + j) must be odd) must follow ((arr[i] + arr[j]) % 2) = K.
Note: The number of permutations can be very large. Therefore, calculate it with mod of (109 + 7).
Examples:
Input: N = 3, K = 1, arr[] = {1, 2, 3}
Output: 2
Explanation: The 2 permutations satisfying the given constraints will be {1, 2, 3} and {3, 2, 1} respectively.
- Permutation {1, 2, 3}: Pair of indices having odd sum are (0, 1) and (1, 2).
- (arr[0] + arr[1]) % 2 = (1 + 2) % 2 = K holds true.
- (arr[1] + arr[2]) % 2 = (2 + 3) % 2= K holds true.
- Permutation {3, 2, 1}: Pair of indices having odd sum are (0, 1) and (1, 2).
- (arr[0] + arr[1]) % 2 = (3 + 2) % 2 = K holds true.
- (arr[1] + arr[2]) % 2 = (2 + 1) % 2 = K holds true.
There are 2 valid permutations satisfying the given constraints. Therefore, output is 2.
Input: N = 5, K = 0, arr[] = {6, 10, 1, 4, 8}
Output: 0
Explanation: It can be verified that there will be no permutation satisfying the given constraints.
Approach: To solve the problem, follow the below idea:
The problem is observation based. There will some cases based on the value K and count of odd and even elements. Let us describe the count of odd and even elements by Odd and Even respectively and a precomputed factorials array as Factorial[]. Now see those cases one by one:
Case 1: (N == 1)
- At base case there can be possible only one permutation as arr[] = {arr[0]}. So, answer for this case is 1.
Case 2: (K == 0 && (Odd == 0 || Even == 0))
- For (K == 0), we can have pairs (arr[i] + arr[j]) with even sum only. As (Sum of two even elements) or (Sum of two odd elements) both gives sum as even. Then, the number of possible permutations for this case will be Factorial[N] as we can arrange elements in any order.
Case 3: (K == 0 || (K == 1 && (Odd == 0 || Even == 0)))
- If after checking case 2 (Just above case), we still have (K == 0), then there must be (Odd != 0) and (Even != 0), we can't construct valid permutation in such cases. So, the number of possible permutations for this case will be 0.
- Also, if (K == 1). Then we can only have odd sum pairs (i, j) at i = (1, 3, 5. .) and j = (2, 4, 6. .) indices. If we have (Odd == 0 || Even ==0), then it not possible to construct valid permutation of elements as sum will always be even. Therefore, the number of possible permutations for this case will be also 0.
Case 4: Now, we have (K == 1) && (Odd != 0 and Even != 0).
- If (Abs(Odd - Even) > 1), then there will always be at least one pair such that sum will be even so, the valid permutation for such cases will be 0.
- If (Odd - Even) == 1), then, possible permutations = (Factorial[Even]*Factorial[Odd]) (As we can arrange even and odd numbers in any order at i = (1, 3, 5, 7. . .) and j = (2, 4, 6 . .) respectively.
- If (Odd == Even), then arr[]'s length is even. So, we can put odds at (1, 3, 5. .) and evens at (2, 4, 6. .) and vice versa also. Therefore, for such cases the count of valid permutations are = 2*(Factorial[Even]*Factorial[Odd]).
Step-by-step algorithm:
- Declare an array let say Fact[] to hold factorial values and precompute them.
- Create two variables let say Odd and Even to store the count of odd and even elements in arr[].
- Iterate using loops on arr[] and count odd and even elements.
- If (N == 1), Return 1.
- If (K == 0 && (Odd == 0 || Even == 0)), Return Fact[N].
- if (K == 0 || (K == 1 && (Odd == 0 || Even == 0)), return 0.
- Else
- If (Abs(Odd - Even) > 1), return 0.
- Else
- Declare a variable let say Ans.
- If (Odd == Even), Initialize Ans with 2 else 1.
- Ans = ((Ans * Fact[Even]) * Fact[Odd])
- Output Ans.
Below is the implementation of the algorithm:
C++
#include <iostream>
using namespace std;
// Value of Mod
const long long MOD = 1000000007;
// Declare len and Fact[] for precomputing factorials
// till len
const int len = 100001;
long long fact[len];
// Function to precompute factorials
void PreCompute()
{
fact[0] = 1;
fact[1] = 1;
for (int i = 2; i < len; i++)
{
fact[i] = (i * fact[i - 1]) % MOD;
fact[i] %= MOD;
}
}
// Function to implement the discussed approach
void func(int n, int k, int arr[])
{
int odd = 0;
int eve = 0;
// Count the number of odd and even elements in the
// array
for (int i = 0; i < n; i++)
{
if (arr[i] % 2 == 1)
odd++;
else
eve++;
}
// Base case: If there is only one element in the
// array
if (n == 1)
{
cout << "1\n";
return;
}
// Check conditions and calculate result accordingly
if (k == 0 && (odd == 0 || eve == 0))
{
// If k==0 and either no odd or no even
// elements, output factorial of n
cout << fact[n] << "\n";
return;
}
if (k == 0 || (k == 1 && (odd == 0 || eve == 0)))
{
// If k==0 or (k==1 and either no odd or no even
// elements), output 0
cout << "0\n";
}
else
{
if (abs(odd - eve) > 1)
{
// If the absolute difference between odd
// and even counts > 1, output 0
cout << "0\n";
}
else
{
// Calculate the answer based on conditions
long long ans = (odd == eve) ? 2 : 1;
ans = (((ans * fact[eve]) % MOD) * fact[odd]) % MOD;
cout << ans << "\n";
}
}
}
// Driver Function
int main()
{
// Inputs
int N = 3;
int K = 1;
int arr[] = {1, 2, 3};
// Function for Precomputing factorials
PreCompute();
// Function call
func(N, K, arr);
return 0;
}
// This code is contributed by rambabuguphka
Java
// Java code to implement the approach
import java.util.Scanner;
// Driver Class
public class Main {
// Value of Mod
static final long MOD = 1000000007;
// Declare len and Fact[] for precomputing factorials
// till len
static int len = 100001;
static long[] fact = new long[len];
// Driver Function
public static void main(String[] args)
{
// Inputs
int N = 3;
int K = 1;
int[] arr = { 1, 2, 3 };
// Function for Precomputing factorials
PreCompute();
// Function call
func(N, K, arr);
}
// Function to precompute factorials
public static void PreCompute()
{
fact[0] = 1;
fact[1] = 1;
for (int i = 2; i < len; i++) {
fact[i] = (i * fact[i - 1]) % MOD;
fact[i] %= MOD;
}
}
// Function to implement the discussed approach
public static void func(int n, int k, int[] arr)
{
int odd = 0;
int eve = 0;
// Count the number of odd and even elements in the
// array
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 1)
odd++;
else
eve++;
}
// Base case: If there is only one element in the
// array
if (n == 1) {
System.out.println("1");
return;
}
// Check conditions and calculate result accordingly
if (k == 0 && (odd == 0 || eve == 0)) {
// If k==0 and either no odd or no even
// elements, output factorial of n
System.out.println(fact[n]);
return;
}
if (k == 0 || (k == 1 && (odd == 0 || eve == 0))) {
// If k==0 or (k==1 and either no odd or no even
// elements), output 0
System.out.println("0");
}
else {
if (Math.abs(odd - eve) > 1) {
// If the absolute difference between odd
// and even counts > 1, output 0
System.out.println("0");
}
else {
// Calculate the answer based on conditions
long ans = (odd == eve) ? 2 : 1;
ans = (((ans * fact[eve]) % MOD)
* fact[odd])
% MOD;
System.out.println(ans);
}
}
}
}
Python3
# Value of Mod
MOD = 1000000007
# Declare len and fact[] for precomputing factorials till len
len_val = 100001
fact = [0] * len_val
# Function to precompute factorials
def pre_compute():
fact[0] = 1
fact[1] = 1
for i in range(2, len_val):
fact[i] = (i * fact[i - 1]) % MOD
fact[i] %= MOD
# Function to implement the discussed approach
def func(n, k, arr):
odd = 0
eve = 0
# Count the number of odd and even elements in the array
for i in range(n):
if arr[i] % 2 == 1:
odd += 1
else:
eve += 1
# Base case: If there is only one element in the array
if n == 1:
print("1")
return
# Check conditions and calculate result accordingly
if k == 0 and (odd == 0 or eve == 0):
# If k==0 and either no odd or no even elements, output factorial of n
print(fact[n])
return
if k == 0 or (k == 1 and (odd == 0 or eve == 0)):
# If k==0 or (k==1 and either no odd or no even elements), output 0
print("0")
else:
if abs(odd - eve) > 1:
# If the absolute difference between odd and even counts > 1, output 0
print("0")
else:
# Calculate the answer based on conditions
ans = 2 if odd == eve else 1
ans = (((ans * fact[eve]) % MOD) * fact[odd]) % MOD
print(ans)
# Driver Function
if __name__ == "__main__":
# Inputs
N = 3
K = 1
arr = [1, 2, 3]
# Function for Precomputing factorials
pre_compute()
# Function call
func(N, K, arr)
C#
using System;
public class Program
{
// Value of Mod
const long MOD = 1000000007;
// Declare len and Fact[] for precomputing factorials
// till len
const int len = 100001;
static long[] fact = new long[len];
// Function to precompute factorials
static void PreCompute()
{
fact[0] = 1;
fact[1] = 1;
for (int i = 2; i < len; i++)
{
fact[i] = (i * fact[i - 1]) % MOD;
fact[i] %= MOD;
}
}
// Function to implement the discussed approach
static void Func(int n, int k, int[] arr)
{
int odd = 0;
int eve = 0;
// Count the number of odd and even elements in the
// array
for (int i = 0; i < n; i++)
{
if (arr[i] % 2 == 1)
odd++;
else
eve++;
}
// Base case: If there is only one element in the
// array
if (n == 1)
{
Console.WriteLine("1");
return;
}
// Check conditions and calculate result accordingly
if (k == 0 && (odd == 0 || eve == 0))
{
// If k==0 and either no odd or no even
// elements, output factorial of n
Console.WriteLine(fact[n]);
return;
}
if (k == 0 || (k == 1 && (odd == 0 || eve == 0)))
{
// If k==0 or (k==1 and either no odd or no even
// elements), output 0
Console.WriteLine("0");
}
else
{
if (Math.Abs(odd - eve) > 1)
{
// If the absolute difference between odd
// and even counts > 1, output 0
Console.WriteLine("0");
}
else
{
// Calculate the answer based on conditions
long ans = (odd == eve) ? 2 : 1;
ans = (((ans * fact[eve]) % MOD) * fact[odd]) % MOD;
Console.WriteLine(ans);
}
}
}
// Driver Function
static void Main(string[] args)
{
// Inputs
int N = 3;
int K = 1;
int[] arr = { 1, 2, 3 };
// Function for Precomputing factorials
PreCompute();
// Function call
Func(N, K, arr);
}
}
JavaScript
// Value of Mod
const MOD = 1000000007;
// Declare len and fact[] for precomputing factorials till len
const len = 100001;
let fact = new Array(len);
// Function to precompute factorials
function preCompute() {
fact[0] = 1;
fact[1] = 1;
for (let i = 2; i < len; i++) {
fact[i] = (i * fact[i - 1]) % MOD;
fact[i] %= MOD;
}
}
// Function to implement the discussed approach
function func(n, k, arr) {
let odd = 0;
let eve = 0;
// Count the number of odd and even elements in the array
for (let i = 0; i < n; i++) {
if (arr[i] % 2 === 1)
odd++;
else
eve++;
}
// Base case: If there is only one element in the array
if (n === 1) {
console.log("1");
return;
}
// Check conditions and calculate result accordingly
if (k === 0 && (odd === 0 || eve === 0)) {
// If k==0 and either no odd or no even elements, output factorial of n
console.log(fact[n]);
return;
}
if (k === 0 || (k === 1 && (odd === 0 || eve === 0))) {
// If k==0 or (k==1 and either no odd or no even elements), output 0
console.log("0");
} else {
if (Math.abs(odd - eve) > 1) {
// If the absolute difference between odd and even counts > 1, output 0
console.log("0");
} else {
// Calculate the answer based on conditions
let ans = (odd === eve) ? 2 : 1;
ans = (((ans * fact[eve]) % MOD) * fact[odd]) % MOD;
console.log(ans);
}
}
}
// Inputs
let N = 3;
let K = 1;
let arr = [1, 2, 3];
// Function for Precomputing factorials
preCompute();
// Function call
func(N, K, arr);
Time Complexity: O(N), As we are counting odd and even elements and precomputing factorials as well
Auxiliary space: O(100001), As we used array Fact[] of size 100001.
Similar Reads
Number of permutations such that sum of elements at odd index and even index are equal
Given N numbers, find the number of permutations in which the sum of elements at odd index and sum of elements at even index are equal. Examples: Input: 1 2 3 Output: 2 The permutations are: 1 3 2 sum at odd index = 1+2 = 3, sum at even index = 3 2 3 1 sum at odd index = 2+1 = 3, sum at even index =
8 min read
Count of permutations such that sum of K numbers from given range is even
Given a range [low, high], both inclusive, and an integer K, the task is to select K numbers from the range(a number can be chosen multiple times) such that the sum of those K numbers is even. Print the number of all such permutations. Examples: Input: low = 4, high = 5, k = 3 Output: 4 Explanation:
7 min read
Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
Given two integers N and K, the task is to find a permutation of integers from the range [1, N] such that the number of indices (1-based indexing) where gcd(p[i], i) > 1 is exactly K. Print -1 if such permutation is not possible. Examples: Input: N = 4, K = 3 Output: 1 2 3 4 gcd(1, 1) = 1 gcd(2,
7 min read
Sum of elements of all partitions of number such that no element is less than K
Given an integer N, the task is to find an aggregate sum of all integer partitions of this number such that each partition does not contain any integer less than K. Examples: Input: N = 6 and K = 2 Output: 24 In this case, there are 4 valid partitions. 1) {6} 2) {4, 2} 3) {3, 3} 4) {2, 2, 2} Therefo
9 min read
Count ordered pairs of positive numbers such that their sum is S and XOR is K
Given a sum S and a number K . The task is to count all possible ordered pairs (a, b) of positive numbers such that the two positive integers a and b have a sum of S and a bitwise-XOR of K. Examples: Input : S = 9, K = 5 Output : 4 The ordered pairs are (2, 7), (3, 6), (6, 3), (7, 2) Input : S = 2,
7 min read
Find the Array Permutation having sum of elements at odd indices greater than sum of elements at even indices
Given an array arr[] consisting of N integers, the task is to find the permutation of array elements such that the sum of odd indices elements is greater than or equal to the sum of even indices elements. Examples: Input: arr[] = {1, 2, 3, 4}Output: 1 4 2 3 Explanation:Consider the permutation of th
6 min read
Minimum subarray reversals required such that sum of all pairs of adjacent elements is odd
Given an array arr[] of size N, having an equal number of even and odd integers, the task is to find the minimum number of subarrays required to be reversed to make the sum of pairs of adjacent elements odd. Examples: Input: arr[] = {13, 2, 6, 8, 3, 5, 7, 10, 14, 15} Output: 3Explanation:Step 1: Rev
9 min read
Find a permutation of 2N numbers such that the result of given expression is exactly 2K
Given two integers N and K, the task is to find a permutation of first 2*N natural numbers such that the following equation is satisfied. \sum\limits_{i=1}^N |A_{2i-1}-A_{2i}| - |\sum\limits_{i=1}^N A_{2i-1}-A_{2i}|=2K Note: The value of K will always be less than or equal to N.Examples: Input : N =
5 min read
Represent N as sum of K odd numbers with repetitions allowed
Given two integers N and K, the task is to represent N as sum of K odd numbers. If it is not possible to create the sum then output -1.Note: The representation may contain duplicate odd numbers.Examples: Input: N = 5, K = 3 Output: 1, 1, 3 Explanation: The given number N can be represented as 1 + 1
4 min read
Range Queries for finding the Sum of all even parity numbers
Given Q queries where each query consists of two numbers L and R which denotes a range [L, R]. The task is to find the sum of all Even Parity Numbers lying in the given range [L, R]. Parity of a number refers to whether it contains an odd or even number of 1-bits. The number has Even Parity if it co
10 min read