Prime Arrangements - Problem

Imagine you have numbers from 1 to n and you want to arrange them in a line. But here's the twist: you want prime numbers to sit in prime positions (using 1-based indexing)!

Your task is to count how many different ways you can arrange the numbers 1, 2, 3, ..., n such that whenever a number is prime, it must be placed at a prime-numbered position.

Example: For n=5, the numbers are [1,2,3,4,5]. Prime numbers are {2,3,5} and prime positions are {2,3,5}. So primes must go in prime positions, and non-primes {1,4} must go in non-prime positions {1,4}.

Since the answer can be very large, return it modulo 109 + 7.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 5
โ€บ Output: 12
๐Ÿ’ก Note: Numbers [1,2,3,4,5]: Primes {2,3,5} must go to prime positions {2,3,5}, non-primes {1,4} to positions {1,4}. Arrangements: 3! ร— 2! = 6 ร— 2 = 12.
example_2.py โ€” Small Case
$ Input: n = 2
โ€บ Output: 1
๐Ÿ’ก Note: Numbers [1,2]: Prime {2} must go to prime position {2}, non-prime {1} to position {1}. Only one valid arrangement: [1,2].
example_3.py โ€” Edge Case
$ Input: n = 1
โ€บ Output: 1
๐Ÿ’ก Note: Only number [1]: Since 1 is not prime and position 1 is not prime, there's exactly one valid arrangement: [1].

Visualization

Tap to expand
๐ŸŽฏ VIP Seating Arrangement ProblemExample: n = 5 (5 guests, 5 seats)๐Ÿ‘‘ VIP Guests (Prime Numbers):235Count: 3 VIPs๐Ÿ‘ค Regular Guests (Non-Prime Numbers):14Count: 2 Regular๐Ÿ† VIP Seats (Prime Positions):235Count: 3 VIP Seats๐Ÿช‘ Regular Seats (Non-Prime Positions):14Count: 2 Regular Seats๐Ÿงฎ Calculation LogicStep 1: VIP arrangements3 VIPs can sit in 3 VIP seatsin 3! = 6 different waysStep 2: Regular arrangements2 Regular guests in 2 regular seatsin 2! = 2 different waysStep 3: Total combinationsTotal = 3! ร— 2! = 6 ร— 2 = 12โšก Time: O(n) | Space: O(1)
Understanding the Visualization
1
Identify VIPs
Count how many VIP guests (prime numbers) we have
2
Count VIP Seats
Count how many VIP seats (prime positions) are available
3
Arrange VIPs
VIP guests can be arranged among VIP seats in VIP_count! ways
4
Arrange Regular Guests
Regular guests can be arranged among regular seats in regular_count! ways
5
Multiply Results
Total arrangements = VIP_arrangements ร— regular_arrangements
Key Takeaway
๐ŸŽฏ Key Insight: This is a combinatorial problem where we separate the constraints - primes must go to prime positions, non-primes to non-prime positions. The total arrangements are simply the product of arrangements within each group!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass to count primes + O(n) to calculate factorials

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables for counting and calculation

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 100
  • Time limit: 1 second
  • Return answer modulo 109 + 7
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
28.4K Views
Medium Frequency
~15 min Avg. Time
832 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen