Similar Problems

Similar Problems not available

Prime Arrangements - Leetcode Solution

Companies:

LeetCode:  Prime Arrangements Leetcode Solution

Difficulty: Easy

Topics: math  

Problem Statement:

The task is to create all possible permutations of the prime numbers from 1 to n (inclusive). Count the number of permutations that meets the following requirements - each prime number appears exactly once and for each index i (1 <= i <= n), the absolute difference between the elements at position i and i+1 must be a prime number. Since the answer can be large, return it modulo 10^9 + 7.

Example:

Input: n = 5 Output: 12 Explanation: The possible permutations are: [1,2,3,4,5] -> True [1,2,5,4,3] -> False, abs(2-5) is not prime [1,3,2,5,4] -> True [1,3,4,2,5] -> False, abs(3-4) is not prime [1,4,2,5,3] -> True [1,4,3,5,2] -> False, abs(4-3) is not prime [1,5,2,4,3] -> False, abs(5-4) is not prime [1,5,3,2,4] -> True [2,1,3,5,4] -> False, abs(1-2) is not prime [2,3,1,5,4] -> True [2,3,4,1,5] -> False, abs(4-1) is not prime [2,4,1,5,3] -> True There are a total of 12 valid permutations [1,2,3,4,5], [1,3,2,5,4], [1,4,2,5,3], [1,5,3,2,4], [2,3,1,5,4], [2,4,1,5,3], [3,2,1,4,5], [3,4,5,2,1], [4,1,5,2,3], [4,5,3,2,1], [5,2,4,1,3], [5,3,1,4,2].

Solution:

The brute force solution for this problem will be to generate all possible permutations of primes in the range of 1 to n using permutations or itertools. Then for each permutation, just check if it satisfies the given conditions of prime difference or not. If it is True, increment the counter.

But we all know that we cannot use this approach, because finding all possible permutations of n numbers will take a lot of time O(n!) and will not work for large values of n.

So, a more optimized approach is required. We can make use of the concept of generating prime numbers up to n, followed by generating all prime number differences. Then we can use backtracking to explore all possible permutations that satisfy the given constraint.

Algorithm:

  1. Generate prime numbers up to n, by iterating from 2 to n and checking if each number is a prime number or not. To check if a number is prime or not, we can use the square root method of prime number generation, which has time complexity of O(n √n).

  2. Generate prime differences from the list of prime numbers generated in step 1. To generate prime differences, we can create a boolean list primes of size n+1, initially set all the values to True. Then iterate through the list of prime numbers and find the difference between all pairs of prime numbers. If the difference is prime, mark all elements in primes between the two indexes as False. This means the difference between the two numbers is not prime, so we do not want to select any permutation with these indexes. This step has time complexity of O(n^2).

  3. Use backtracking to explore all possible permutations that satisfy the given constraints. For each index i (1 <= i <= n), iterate through all prime numbers that have not been used yet (using a boolean list of used numbers), and check if the absolute difference between the current number and the number at position i-1 is a prime number. If it is, add the current number to the permutation and mark it as used, otherwise skip it. If we have reached the end of the list (i.e., i == n), we have found a valid permutation, so increment the counter. This step has time complexity of O(n!) since we are exploring all possible permutations.

  4. Return the answer modulo 10^9 + 7.

Time Complexity:

The time complexity of the above algorithm is O(n^2 + n √n + n!) since we are generating prime numbers, prime differences, and exploring all possible permutations. The algorithm can handle values of n less than 20, but for larger values of n, this algorithm will take a lot of time.

Code:

Here is the python code for the above algorithm:

Prime Arrangements Solution Code

1