
1. The permutation [1,4,3,2] is not a valid permutation because 2 is a prime number and thus it should be placed at a prime index but in this permutation, it is placed at index 4(1-indexed). Also, at index 2, a prime number should have been placed as 2 is a prime number but here, 4 is placed which is not a prime number. So, this is an invalid permutation.
2. The permutation [1,3,2,4] is a valid permutation because 2 and 3 are prime numbers, so index 2 and index 3 are prime indices. In this permutation, prime numbers are placed at prime indices only and composite numbers are placed at composite indices so this is a valid permutation.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains a single integer, ‘N’.
For each test case, return a single integer, denoting the total number of required permutations. Since, the answer may be very large, print the answer modulo 10^9 + 7.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N < 100
Time Limit: 1 sec
The approach is to find out the total number of prime numbers in the range, 1 to ‘N’. Let this number be ‘X’. So, the total number of prime indices will also be ‘X’ and the total number of different ways in which the prime numbers can be placed at prime indices will be ‘X!’. The total number of composite numbers will be ‘N-X’. So, the total number of different ways in which the composite numbers can be placed at prime indices will be ‘(N-X)!’.
The approach is to use the Sieve of Eratosthenes to find the count of prime numbers in the range, 1 to ‘N’. The Sieve of Eratosthenes is one of the most efficient ways to find all prime numbers smaller than ‘N’. We can learn more about Sieve of Eratosthenes here. The rest of the procedure to find the total number of different arrangements using the value of this count remains the same.
Pair Product Div by K
Pair Product Div by K
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
First Digit One
Special Digit Numbers