Count of permutation such that GCD of all elements multiplied with position is not 1
Last Updated :
16 May, 2022
Given an integer array having N elements ranging from 1 to N and each element appearing exactly once. The task is to find the number of possible permutations such that the GCD of all elements multiplied with their position is greater than 1.
Note: As the answer can be very large, return the answer modulo 109 + 7
Examples:
Input: N = 2, arr[] = {1, 2}
Output: 1
Explanation: The only valid permutation will be is [2, 1] because GCD(1*2, 2*1) = 2.
Input: N = 4, arr[] = {4, 1, 3, 2}
Output: 4
Explanation:
The valid permutations will be
[4, 3, 2, 1] with GCD(1*4, 2*3, 3*2, 4*1) = 2.
[2, 3, 4, 1] with GCD(1*2, 2*3, 3*4, 4*1) = 2.
[2, 1, 4, 3] with GCD(1*2, 2*1, 3*4, 4*3) = 2.
[4, 1, 2, 3] with GCD(1*4, 2*1, 3*2, 4*3) = 2.
Approach: The idea to solve the problem is as follows:
Try to make the product of position and the number even, then in that situation GCD will be at least 2.
So if N is odd then there will 1 more odd element than possible even positions. So no permutation is possible.
Otherwise
- the N/2 even elements can be arranged in (N/2)! ways.
- For each of this arrangement N/2 odd elements can be arranged in (N/2)! ways.
So total number of possible ways are ((N/2)!)2.
Follow the below steps to solve this problem:
- If N is odd then return 0.
- Initialize one variable to store the answer (say ans = 1).
- Traverse from i = 1 to N/2.
- Make ans equal to ans * i * i % MOD.
- Find the mod of ans.
- Return ans.
Below is the implementation of the above approach.
C++
// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
// Function to find
// the number of valid permutations
int ValidPerm(int n, int a[])
{
// If n is odd
if (n & 1) {
return 0;
}
long long ans = 1;
// Counting number of permutations
for (int i = 1; i <= n / 2; ++i) {
ans *= i * i % MOD;
ans %= MOD;
}
// Return the number of
// possible permutations
return ans;
}
// Driver code
int main()
{
int N = 4;
int arr[N] = { 1, 3, 2, 4 };
// Function call
cout << ValidPerm(N, arr);
return 0;
}
Java
// Java code to implement the above approach
import java.util.*;
public class GFG {
static int MOD = 1000000007;
// Function to find
// the number of valid permutations
static int ValidPerm(int n, int a[])
{
// If n is odd
if ((n & 1) == 1) {
return 0;
}
long ans = 1;
// Counting number of permutations
for (int i = 1; i <= n / 2; ++i) {
ans *= i * i % MOD;
ans %= MOD;
}
// Return the number of
// possible permutations
return (int)ans;
}
// Driver code
public static void main(String args[])
{
int N = 4;
int arr[] = { 1, 3, 2, 4 };
// Function call
System.out.println(ValidPerm(N, arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code to implement the above approach
MOD = 1000000007
# Function to find
# the number of valid permutations
def ValidPerm(n, a):
# If n is odd
if (n & 1):
return 0
ans = 1
# Counting number of permutations
for i in range(1,(n // 2)+1):
ans *= i * i % MOD
ans %= MOD
# Return the number of
# possible permutations
return ans
# Driver code
N = 4
arr = [1, 3, 2, 4]
# Function call
print(ValidPerm(N, arr));
# This code is contributed by shinjanpatra
C#
// C# code to implement the above approach
using System;
public class GFG {
static int MOD = 1000000007;
// Function to find
// the number of valid permutations
static int ValidPerm(int n, int []a)
{
// If n is odd
if ((n & 1) == 1) {
return 0;
}
long ans = 1;
// Counting number of permutations
for (int i = 1; i <= n / 2; ++i) {
ans *= i * i % MOD;
ans %= MOD;
}
// Return the number of
// possible permutations
return (int)ans;
}
// Driver code
public static void Main()
{
int N = 4;
int []arr = { 1, 3, 2, 4 };
// Function call
Console.WriteLine(ValidPerm(N, arr));
}
}
// This code is contributed by jana_sayantan.
JavaScript
<script>
// JavaScript code to implement the above approach
const MOD = 1000000007;
// Function to find
// the number of valid permutations
const ValidPerm = (n, a) => {
// If n is odd
if (n & 1) {
return 0;
}
let ans = 1;
// Counting number of permutations
for (let i = 1; i <= n / 2; ++i) {
ans *= i * i % MOD;
ans %= MOD;
}
// Return the number of
// possible permutations
return ans;
}
// Driver code
let N = 4;
let arr = [1, 3, 2, 4];
// Function call
document.write(ValidPerm(N, arr));
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Generate Permutation such that GCD of all elements multiplied with position is not 1 Given an integer N and the task is to generate a permutation of the numbers in range [1, N] such that: The GCD of all the elements multiplied with their position (not index) is greater than 1 And if it is not possible return -1.If there are multiple possible permutations, print any one of them. Exam
7 min read
Count permutations of 0 to N-1 with at least K elements same as positions Given two integers, N and K, the task is to find the number of permutations of numbers from 0 to N - 1, such that there are at least K positions in the array such that arr[i] = i ( 0 <= i < N ). As the answer can be very large, calculate the result modulo 10^9+7. Examples: Input: N = 4, K = 3
15+ 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
Count of permutations of an Array having each element as a multiple or a factor of its index Given an integer n, the task is to count the number of ways to generate an array, arr[] of consisting of n integers such that for every index i (1-based indexing), arr[i] is either a factor or a multiple of i, or both. Note: The arr[] must be the permutations of all the numbers from the range [1, n]
14 min read
Count of permutations of an Array having each element as a multiple or a factor of its index Given an integer n, the task is to count the number of ways to generate an array, arr[] of consisting of n integers such that for every index i (1-based indexing), arr[i] is either a factor or a multiple of i, or both. Note: The arr[] must be the permutations of all the numbers from the range [1, n]
14 min read
Count arrays of length K whose product of elements is same as that of given array Given an integer array arr[] of length N and an integer K, the task is to count the number of possible arrays of length K such that the product of all elements of that array is equal to the product of all elements of the given array arr[]. Since the answer can be very large, return the answer modulo
15+ min read