Tara's Beautiful Permutations


Problem Statement :


Tara has an array, A, consisting of n integers where each integer occurs at most 2 times in the array.

Let's define P to be a permutation of A where Pi is the ith element of permutation P. Tara thinks a permutation is beautiful if there is no index i such that Pi - P(i+1) = 0 where i ∈ [0, n-1).

You are given q queries where each query consists of some array A. For each A, help Tara count the number of possible beautiful permutations of the n integers in A and print the count, modulo 10^9 +7, on a new line.

Note: Two permutations, P and Q, are considered to be different if and only if there exists an index i such that Pi != Qi and [0, n-1).

Input Format

The first line contains a single integer, q, denoting the number of queries. The 2.q subsequent lines describe each query in the following form:

1.The first line contains an integer, n, denoting the number of elements in array A.
2.The second line contains n space-separated integers describing the respective values of a0, a1, ..., an-1 in array A.
Constraints
1 <= ai <= 10^9
Each integer in A can occur at most 2 times.
For 40% of the maximum score:
 1 <= q <= 100
 1 <= n <= 1000
The sum of n over all queries does not exceed 10^4.
For 100% of the maximum score:
  1 <= q <= 100
  1 <= n <= 2000
Output Format

For each query, print the the number of possible beautiful permutations, modulo 10^9 + 7, on a new line.



Solution :



title-img


                            Solution in C :

In C++ :





#include <bits/stdc++.h>

using namespace std;

const int MOD=1000000007;

int dp[2001][2001][2];

int rec(int one, int two, int ban)
{
    if(one==0 && two==0)
        return 1;
    int& ret=dp[one][two][ban];
    if(ret!=-1)
        return ret;
    ret=0;
    if(one>0)
        ret=(ret+1LL*rec(one-1, two, 0)*(one-ban))%MOD;
    if(two>0)
        ret=(ret+1LL*rec(one+1, two-1, 1)*two)%MOD;
    return ret;
}

int main()
{
    memset(dp, -1, sizeof dp);
    int Q;
    scanf("%d", &Q);
    while(Q--)
    {
        int N;
        scanf("%d", &N);
        map<int, int> m;
        for(int i=0; i<N; i++)
        {
            int a;
            scanf("%d", &a);
            m[a]++;
        }
        int one=0, two=0;
        for(auto& it: m)
        {
            if(it.second==1)
                one++;
            else
                two++;
        }
        printf("%d\n", rec(one, two, 0));
    }
    return 0;
}








In c :





#include<stdio.h>
#include<stdlib.h>
#define m 1000000007u
#define m2 500000004u
typedef long long unsigned llu;
typedef unsigned u;
int S(const void*x,const void*y){return*(int*)x-*(int*)y;}
u C[2222][2222],A[2222],F[2222];
u P(u b,u e)
{
	u r=1;
	for(;e;e>>=1)
	{
		if(e&1)r=r*(llu)b%m;
		b=b*(llu)b%m;
	}
	return r;
}
int main()
{
	u t,n,i,j,k,d,r;
	for(i=-1;++i<2222;)for(C[i][j=0]=1;j++<i;)
	if((C[i][j]=C[i-1][j-1]+C[i-1][j])>=m)C[i][j]-=m;
	for(F[i=0]=1;++i<2222;)F[i]=i*(llu)F[i-1]%m;
	for(scanf("%u",&t);t--;)
	{
		scanf("%u",&n);
		for(i=-1;++i<n;)scanf("%u",A+i);
		qsort(A,n,sizeof(u),S);
		for(i=d=0;++i<n;)d+=A[i]==A[i-1];
		k=P(m2,d);r=0;
		for(i=-1;++i<=d;)
		{
			j=C[d][i]*(llu)F[n-i]%m*(llu)k%m;
			if((k<<=1)>=m)k-=m;
			if(i&1)
			{
				if((r-=j)>m)r+=m;
			}
			else 
			{
				if((r+=j)>=m)r-=m;
			}
		}
		printf("%u\n",r);
	}
	return 0;
}








In Python3 :





factorial=[1,]
for i in range(1,2001):
    factorial.append(factorial[-1]*i)

pascal=[[0 for y in range(1001)] for x in range(1001)]

for i in range(1001):
    pascal[i][0]=1
    for j in range(1,i+1):
        pascal[i][j]=pascal[i-1][j-1]+pascal[i-1][j]
        
#print(factorial)
        
for _ in range(int(input())):
    m=int(input())
    n=len(set(input().split()))
    k=m-n
    
    ans=0
    f=1
    for j in range(0,k+1):

        kCj=pascal[k][j]
        ans+=f*kCj*factorial[m-j]//(2**(k-j))
        f=f*-1
    print(ans%1000000007)
                        








View More Similar Problems

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →