question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
prime-arrangements
Python 3 + Mathematical Solution + Sieve Method
python-3-mathematical-solution-sieve-met-jl6d
class Solution:\n def numPrimeArrangements(self, n: int) -> int:\n\t\n if n < 0:\n return 0\n flag = [1]n\n for i in range(2,
zhexiongliu
NORMAL
2019-09-01T04:07:33.069255+00:00
2019-09-01T04:28:12.289698+00:00
518
false
class Solution:\n def numPrimeArrangements(self, n: int) -> int:\n\t\n if n < 0:\n return 0\n flag = [1]*n\n for i in range(2, n+1):\n for j in range(i*i, n+1, i):\n flag[j-1] = 0\n \n prime_num = sum(flag)-1\n return (self.factor(n-prime_num)*self.factor(prime_num))%(10**9 + 7)\n \n def factor(self, n):\n res = 1\n for i in range(n, 1, -1):\n res *= i\n return res\n
3
1
['Math', 'Python', 'Python3']
0
prime-arrangements
C# Readable
c-readable-by-daviti-saa0
\npublic static int NumPrimeArrangements(int n)\n {\n bool IsPrime(int num)\n {\n for (int i = 2; i <= num / 2; i++)
daviti
NORMAL
2019-09-01T04:06:19.537454+00:00
2019-09-01T04:06:19.537486+00:00
260
false
```\npublic static int NumPrimeArrangements(int n)\n {\n bool IsPrime(int num)\n {\n for (int i = 2; i <= num / 2; i++)\n if (num % i == 0)\n return false;\n\n return true;\n }\n\n int mod = (int)Math.Pow(10, 9) + 7;\n\n int count = 0;\n for (int i = 2; i <= n; i++)\n if (IsPrime(i))\n count++;\n\n long res = 1;\n for (int i = count; i > 0; i--)\n {\n res = (res * i) % mod;\n res %= mod;\n }\n\n for (int i = n - count; i > 0; i--)\n {\n\n res = (res * i) % mod;\n res %= mod;\n }\n\n return (int)res;\n }\n```
3
0
[]
0
prime-arrangements
Prime Permutation Power: Counting Prime Arrangements Using C++
prime-permutation-power-counting-prime-a-spkh
Intuition\nTo solve the problem of counting prime arrangements, we first need to identify how many prime numbers exist up to a given number n. The problem revol
Krishnaa2004
NORMAL
2024-09-06T12:55:00.471282+00:00
2024-09-06T12:55:00.471307+00:00
419
false
# Intuition\nTo solve the problem of counting prime arrangements, we first need to identify how many prime numbers exist up to a given number `n`. The problem revolves around arranging primes and non-primes in factorial ways, so understanding the distinction between primes and non-primes is crucial.\n\n# Approach\n1. **Prime Identification**: Create a helper function `prime(n)` that checks if a number `n` is prime. The function returns `1` if the number is prime and `0` otherwise.\n2. **Counting Primes**: Iterate through all numbers from `1` to `n` and count how many of them are prime.\n3. **Calculate Permutations**: \n - Calculate the factorial of the prime count modulo `1000000007`.\n - Similarly, calculate the factorial of the non-prime count modulo `1000000007`.\n - The result is the product of these two factorials.\n\n# Complexity\n- **Time complexity**: $$O(n \\sqrt{n})$$ \u2014 Checking for primality up to $$n$$ takes $$O(\\sqrt{n})$$ time for each number, and there are $$n$$ numbers to check.\n- **Space complexity**: $$O(1)$$ \u2014 Only a constant amount of extra space is used, excluding the input and output.\n\n# Code\n```cpp\nclass Solution {\npublic:\n int prime(int n) {\n if (n == 1) {\n return 0; \n }\n for (int i = 2; i * i <= n; ++i) {\n if (n % i == 0) {\n return 0; \n }\n }\n return 1; \n }\n\n int numPrimeArrangements(int n) {\n int count = 0; \n for (int i = 1; i <= n; ++i) {\n if (prime(i) == 1) {\n count++; \n }\n }\n long long int perm = 1; \n for (int i = 1; i <= count; ++i) {\n perm = (perm * i) % 1000000007; \n }\n for (int i = 1; i <= n - count; ++i) {\n perm = (perm * i) % 1000000007; \n }\n return perm; \n }\n};\n```\n
2
0
['C++']
0
prime-arrangements
Python Solution | EASY 100%
python-solution-easy-100-by-ogjdurhi-gmr6
\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n def factorial(n: int) -> int:\n if n == 0:\n return 1\n
adarshiremath
NORMAL
2024-07-11T16:41:43.974522+00:00
2024-07-11T16:41:43.974568+00:00
227
false
```\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n def factorial(n: int) -> int:\n if n == 0:\n return 1\n else:\n return n * factorial(n - 1)\n\n def is_prime(num):\n if num < 2:\n return False\n for i in range(2, int(num ** 0.5) + 1):\n if num % i == 0:\n return False\n return True\n\n pcount = 0\n for num in range(1, n + 1):\n if is_prime(num):\n pcount += 1\n\n return factorial(pcount) * factorial(n - pcount) % (10**9 + 7)\n```\n\n![image](https://assets.leetcode.com/users/images/56738745-a646-4e1a-bf1b-81d687205c30_1720657656.9161804.gif)\n
2
0
['Python', 'Python3']
0
prime-arrangements
Solution with Full Explanation! 🔢 🔀 🔥
solution-with-full-explanation-by-sirseb-1llu
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to count the number of ways to arrange numbers from 1 to n such that prime
sirsebastian5500
NORMAL
2024-06-25T03:49:09.250474+00:00
2024-06-25T03:49:09.250502+00:00
378
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to count the number of ways to arrange numbers from 1 to $$n$$ such that prime numbers occupy prime indices. This problem is interesting because it combines elements of number theory (identifying prime numbers) and combinatorics (counting permutations).\n\nThe key insight is to recognize that the placement of prime numbers is independent of the specific prime indices. Instead, we only need to count the number of prime and non-prime numbers up to $$n$$, and then determine how many ways we can arrange these counts. By calculating the factorial of the number of primes and the factorial of the number of non-primes, we account for all possible arrangements.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Prime Checking:** We implement an efficient prime-checking function. For each number up to $$n$$, we check if it is prime. This is done by first eliminating small prime factors (2 and 3) and then checking for larger prime factors up to the square root of the number.\n\n2. **Counting Primes:** We iterate through all numbers from 1 to $$n$$, using the prime-checking function to count how many are prime.\n\n3. **Factorial Calculation:** We implement a function to calculate the factorial of a number modulo $$10^9 + 7$$. This function will be used to compute the factorial of the number of primes and non-primes.\n\n4. **Calculate Result:** We multiply the factorial of the number of primes by the factorial of the number of non-primes and take the result modulo $$10^9 + 7$$. This gives us the total number of valid permutations.\n\n# Complexity\nTime complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n - Prime checking for each number from 1 to $$n$$ takes $$O(n \\sqrt{n})$$.\n - Calculating factorials takes $$O(n)$$.\n - Overall time complexity is $$O(n \\sqrt{n})$$.\n\nSpace complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n - The algorithm uses $$O(1)$$ extra space for variables and results.\n\n# Code\n```python\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n MOD = 10**9 + 7\n \n def is_prime(num):\n """\n # Alternative:\n for i in range(2, int(n**0.5) + 1):\n if n % i == 0: return False\n return True \n """\n if num <= 1:\n return False\n if num <= 3:\n return True\n if num % 2 == 0 or num % 3 == 0:\n return False\n i = 5\n while i * i <= num:\n if num % i == 0 or num % (i + 2) == 0:\n return False\n i += 6 # Skip even numbers and multiples of 3\n return True\n\n def factorial(num):\n f = 1\n for i in range(2, num + 1):\n f = f * i \n return f\n \n primes_count = sum(is_prime(x) for x in range(1, n + 1))\n non_primes_count = n - primes_count\n return factorial(primes_count) * factorial(non_primes_count) % MOD\n```\nUPVOTE IF HELPFUL \uD83E\uDD19\nTHANKS
2
0
['Python3']
0
prime-arrangements
[Python] factorial(primes count) * factorial(not primes count)
python-factorialprimes-count-factorialno-2r6e
python\n"""\nCalculate possible valid indexes of primes and not primes. \nThen multiply factorial of it.\n\nExample:\n Array = [1,2,3,4,5] (n = 5)\n\n Val
pbelskiy
NORMAL
2023-09-24T14:56:58.973055+00:00
2023-09-24T14:56:58.973079+00:00
194
false
```python\n"""\nCalculate possible valid indexes of primes and not primes. \nThen multiply factorial of it.\n\nExample:\n Array = [1,2,3,4,5] (n = 5)\n\n Valid indexes of primes:\n [X,2,3,X,5] => 3 positions\n Valid indexes of not primes:\n [1,X,X,4,X] => 2 positions\n\nTC: O(N)\nSC: O(1)\n"""\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n primes = set([\n 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, \n 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,\n ])\n \n a, b = 0, 0\n \n for i in range(1, n + 1):\n if i in primes:\n a += 1\n else:\n b += 1\n \n return (factorial(a) * factorial(b)) % (10**9 + 7)\n```
2
0
['Python']
1
prime-arrangements
C➕➕ Easy Solution || 100% Faster || Permutation || Mathematics
c-easy-solution-100-faster-permutation-m-7otf
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Find total prime numbers from 1 to n\n2. Answer = (permutation of prim
tripathiharish2001
NORMAL
2023-06-16T06:05:47.690685+00:00
2023-06-16T06:05:47.690714+00:00
700
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Find total prime numbers from 1 to n\n2. Answer = (permutation of prime)*(permutation of non-prime)\n\n# Code\n```\nclass Solution {\npublic:\n \n int numPrimeArrangements(int n) {\n vector<int> prime(n+1,1);\n if(n==1)return 1;\n prime[0]=0;\n prime[1]=0;\n\n int cnt = 0;\n for(int i = 0 ; i<=n ; i++){\n if(prime[i]==0)continue;\n cnt++;\n for(int j=i*2;j<=n;j+=i)prime[j]=0;\n }\n\n const int m = 1e9+7;\n\n int ans=1;\n int temp =n-cnt;\n while(cnt>0){\n ans = ( 1LL*(ans%m) * 1LL*(cnt%m))%m;\n cnt--;\n }\n\n int fact2 = 1;\n while(temp>0){\n fact2 = ( 1LL*(fact2%m) * 1LL*(temp%m))%m;\n temp--;\n }\n \n return (1LL*(ans%m) * 1LL*(fact2%m))%m;\n }\n};\n```
2
0
['C++']
0
prime-arrangements
simple cpp solution
simple-cpp-solution-by-prithviraj26-b4of
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
prithviraj26
NORMAL
2023-01-25T15:09:49.728402+00:00
2023-01-25T15:09:49.728437+00:00
1,326
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n long long fact(int n)\n {\n if(n<=1)return 1;\n return (n*fact(n-1)%1000000007)%1000000007;\n }\n int numPrimeArrangements(int n) {\n if(n==1)return 1;\n if(n<=3)return n-1;\n int t=0,flag;\n for(int i=2;i<=n;i++)\n {\n flag=0;\n for(int j=2;j<=sqrt(i);j++)\n {\n if(i%j==0)\n {\n flag=1;\n break;\n }\n }\n if(flag==0)\n {\n t++;\n }\n }\n return (fact(t)*fact(n-t))%1000000007;\n\n }\n};\n```
2
0
['C++']
0
prime-arrangements
✅ Sieve || 100% faster
sieve-100-faster-by-t86-fdny
c++\nclass Solution {\n int mod = 1e9 + 7;\npublic:\n int numPrimeArrangements(int n, int primeCount = 0, long long res = 1) {\n vector<int> prime(
t86
NORMAL
2023-01-23T15:35:33.387462+00:00
2023-03-24T20:08:00.432949+00:00
176
false
```c++\nclass Solution {\n int mod = 1e9 + 7;\npublic:\n int numPrimeArrangements(int n, int primeCount = 0, long long res = 1) {\n vector<int> prime(n + 1, true);\n prime[0] = false; prime[1] = false;\n for (auto i = 2; i * i <= n; i++)\n if (prime[i])\n for (auto factor = 2; i * factor <= n; factor++) \n prime[i * factor] = false;\n for (auto val: prime) if (val) primeCount++;\n for (auto i = 1; i <= primeCount; i++) res = (res * i) % mod;\n for (auto i = 1; i <= n - primeCount; i++) res = (res * i) % mod;\n return res;\n }\n};\n```\n\n**For more solutions, check out this \uD83C\uDFC6 [GITHUB REPOSITORY](https://github.com/MuhtasimTanmoy/playground) with over 1500+ solutions.**
2
0
[]
0
prime-arrangements
Python Simple Intuitive Solution
python-simple-intuitive-solution-by-h2k4-5suo
Intuition\n Describe your first thoughts on how to solve this problem. \nFactorial comes into mind and then concept of number of prime numbers from [2, n] inclu
h2k4082k
NORMAL
2023-01-05T07:38:26.209081+00:00
2023-01-05T07:38:26.209132+00:00
1,483
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFactorial comes into mind and then concept of number of prime numbers from [2, n] inclusive is to be dealt with. Remember 1 is not prime.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nDefine an isPrime() function that tells whether an input (y) is prime or not.\nRun a for loop from 1 to n+1 and count how many primes are there.\nDefine a Factorial function to calculate x!\nNow we know that prime indexes can be arranged in c! ways so we use fac(c) and also rest of the indices i.e. n-c can be arranged in (n-c)!\nways so we use fac() to calculate these factorials and our final answer will be given after modulo operation with (10**9+7)\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(n**2) due to isPrime() function\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->O(1) due to the variable c.\n\n# Code\n```\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n def isPrime(y):\n if y == 1:\n return False\n for i in range(2, y//2+1):\n if y%i==0:\n return False\n return True\n c = 0\n for i in range(1, n+1):\n if isPrime(i) == True:\n c+=1\n def fac(x):\n f = 1\n while x > 0:\n f = (f*x)%(10**9+7)\n x-=1\n return f\n return fac(n-c)*fac(c)%(10**9+7)\n```
2
0
['Python3']
0
prime-arrangements
Python solution using sieve of Erastosthenes
python-solution-using-sieve-of-erastosth-rtyz
The key is to compute the value (n - p)! p!, where p is the number of primes between 1 and n (inclusive). To see this, if the p primes are to be fixed among "pr
on_danse_encore_on_rit_encore
NORMAL
2022-09-06T00:15:44.712021+00:00
2022-09-06T02:38:40.647336+00:00
458
false
The key is to compute the value **(n - p)! p!**, where *p* is the number of primes between 1 and *n* (inclusive). To see this, if the *p* primes are to be fixed among "prime locations" on the number line, there are *(n - p)!* ways to freely arrange the remaining (non-prime) integers. Since there are also *p!* ways to arrange the *p* primes themselves, we get *(n - p)! p!*.\n\n```\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n # Compute (with reduction by modulo 10^9 + 7 along the way)\n # (n - noPrimes)! * noPrimes!\n \n MODULUS = 10**9 + 7\n noPrimes = len (list(filter(lambda m : m, self.sieve(n))))\n return self.pfact (n - noPrimes, MODULUS) * self.pfact(noPrimes, MODULUS) % MODULUS\n \n def sieve(self, n):\n primes = [True] * (n+1)\n primes[0] = primes[1] = False # 1 is not prime\n \n for i in range(2,n+1):\n if primes[i]:\n # "remove" multiples of the prime by marking them False\n for j in range(2*i, n+1, i):\n primes[j] = False\n \n return primes\n \n def pfact(self, n, p = 10^9 + 7):\n if n == 0 or n == 1: return 1\n \n fact = 1\n for i in range(2, n+1):\n fact = fact * i % p\n \n return fact\n```
2
0
[]
0
prime-arrangements
100% runtime, factorial, counting prime and non prime
100-runtime-factorial-counting-prime-and-a8px
\nPLEASE UPVOTE IF YOU LIKE.\n\n\tpublic int numPrimeArrangements(int n) {\n \n int mod = (int) (Math.pow(10,9) + 7);\n boolean[] arr=new b
Hurshidbek
NORMAL
2022-07-28T20:02:07.011691+00:00
2022-08-20T13:22:46.358644+00:00
592
false
```\nPLEASE UPVOTE IF YOU LIKE.\n```\n\tpublic int numPrimeArrangements(int n) {\n \n int mod = (int) (Math.pow(10,9) + 7);\n boolean[] arr=new boolean[n+1];\n int cntNonPrm = 1;\n \n for (int i = 2; i * i <= n; i++) {\n for (int j = i * 2; j <= n; j += i) {\n if(!arr[j]) {\n arr[j]=true;\n cntNonPrm++;\n }\n }\n }\n\n int cntPrm = n-cntNonPrm;\n long sum = 1;\n\n for (int i = 1; i <= Math.max(cntPrm, cntNonPrm) ; i++) {\n if (i <= Math.min(cntPrm, cntNonPrm)){\n sum *= ((long) i * i );\n sum %= mod;\n }\n else {\n sum *= i;\n sum %= mod;\n }\n }\n\n return (int) sum;\n }
2
0
['Java']
0
prime-arrangements
java easy solution || count primes and nonprimes|| runtime 0ms
java-easy-solution-count-primes-and-nonp-v09f
\nclass Solution {\n public int numPrimeArrangements(int n) {\n int nonPrime=1;\n boolean[] arr=new boolean[n+1];\n Arrays.fill(arr,true
Rakesh_Sharma_4
NORMAL
2022-06-22T19:26:20.207093+00:00
2022-06-22T19:26:20.207139+00:00
414
false
```\nclass Solution {\n public int numPrimeArrangements(int n) {\n int nonPrime=1;\n boolean[] arr=new boolean[n+1];\n Arrays.fill(arr,true);\n for(int i=2;i*i<=n;i++)\n {\n for(int j=i*2;j<=n;j+=i)\n {\n if(arr[j]==true)\n {\n arr[j]=false;\n nonPrime++;\n }\n }\n }\n long res=1;\n int prime=n-nonPrime;\n for(int i=prime;i>0;i--)\n {\n res*=i;\n res%=1000000007;\n }\n for(int i=nonPrime;i>0;i--)\n {\n res*=i;\n res%=1000000007;\n }\n return (int)res;\n }\n}\n```
2
0
['Java']
0
prime-arrangements
PYTHON DIRECT solution with explanation (24ms)
python-direct-solution-with-explanation-o08ah
\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n \n\t\t#First, understand that there is a one-to-one correspondence between\n\t\t#
greg_savage
NORMAL
2022-03-25T03:10:44.757083+00:00
2022-03-25T03:30:46.496166+00:00
297
false
```\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n \n\t\t#First, understand that there is a one-to-one correspondence between\n\t\t#Prime numbers and prime indices\n\t\t\n\t\t#Second, we must concern ourselves with order. The order of the indices matter\n\t\t# [ 1 , 2 , 3 ] is not the same as [ 2, 1, 3]\n\t\t\n\t\t#We divide the problem into two parts:\n\t\t#The ordering of the primes and the ordering of the non primes within their respective indices\n\t\t#That is, primes can only be shuffled between existing prime slots\n\t\t#And non primes can only be shuffled between existing non prime slots\n\t\t\n\t\t#Lets say there are 3 valid slots (don\'t worry about prime or not for the moment ) \n\t\t#with three distinct choices. That means there are 3 valid numbers\n\t\t\n\t\t#When zero slots are chosen, we have 3 choices { X, X, X } ; _ _ _\n\t\t#We choose one. One slot is chosen, we have 2 choices left {X, X} ; X _ _\n\t\t#We choose again. Two slots are chosen. We have one choice left { X } ; X X _\n\t\t\n\t\t#Say we started with {A, B ,C }. We could generate\n\t\t# { A ,B ,C } _ _ _\n\t\t\n\t\t# { B ,C } A _ _\n\t\t\n\t\t# { C } A B _\n\t\t# { B } A C _\n\t\t\n\t\t# { } A B C\n\t\t# { } A C B\t\n\t\t\n\t\t#So we started with 3 choices, Then could only choose 2, Then were forced to choose 1\n\t\t#Because each ordering is uniqie, we could have started with any A, B or C\n\t\t#So 3 ways to start * 2 ways after * 1 one left = 3 * 2 * 1\n\t\t\n\t\t#This can be extended for any amount of choices\n\t\t#If we have 10 things. With each chosen slotting, we have one less\n\t\t# 10, 9, 8, 7 , ... , 3 , 2, 1\n\t\t#In other words we have 10 * 9 * 8 * 7 * ... * 3 * 2 * 1 possibiliites of unique slotting\n\t\t#Notice this is 10!\n\t\t\n #The mod val to reduce the size of the number\n MOD_VAL = pow( 10, 9 ) + 7;\n \n #Find all the primes in n\n #I use the sieve of Eratosthenes\n primes = set( self.sieve( n ) );\n\n #I find the number of prime slots\n #And the number of non prime slots\n\t\t#Remember, there is a 1 to 1 correspondence between primes and their indices\n\t\t#Which also means there is a 1 to 1 correspondence between non primes and their indices\n all_primes = len ( primes );\n all_non = n - all_primes;\n \n \n #There are n! ways of selecting unique slots of primes\n placement_of_primes = factorial( all_primes );\n \n #There are m! ways of selecting unique slots of non primes\n placement_of_non = factorial( all_non );\n \n #There are a * b ways of choosing the possibilities of scenario a_i <-> b_j\n #For all k possibilities of i and q possiblities of j\n\t\t\n\t\t#Another way of saying we have all the unique ways of organizing the primes\n\t\t#Each unique way of organizing the primes can be ordered with each unique way or \n\t\t#Organizing the non-primes\n\t\t\n\t\t#So all possibilites would be \n\t\t\n\t\t# n! * m!\n\t\t#All the possiblities of uniquely re arranging prime slots times all the possiblities\n\t\t#Of uniquely re arraning the non prime slots\n all_possibilities = placement_of_primes * placement_of_non;\n \n return all_possibilities % MOD_VAL;\n \n \n def sieve( self, n ):\n \n isPrime = [ True for _ in range( n + 1 ) ];\n \n isPrime[ 0 ] = False;\n isPrime[ 1 ] = False;\n \n pointer = 0;\n \n length = len ( isPrime );\n \n while pointer < length:\n \n if isPrime[ pointer ] == False:\n pointer += 1;\n else:\n for i in range( pointer + pointer , n + 1, pointer):\n isPrime[ i ] = False;\n \n pointer += 1;\n \n primes = [];\n for i in range( length ):\n if isPrime[ i ]:\n primes.append( i );\n return primes;\n```
2
0
[]
1
prime-arrangements
C++ | 100% Fast 0ms Simple Solution
c-100-fast-0ms-simple-solution-by-__priy-rsp9
\nint M = pow(10, 9) + 7;\nclass Solution {\npublic:\n int numPrimeArrangements(int n) {\n \n vector<bool> seive(n + 1, true);\n seive[1
__priyanshu__
NORMAL
2022-02-09T13:51:44.389758+00:00
2022-02-09T13:51:57.262329+00:00
412
false
```\nint M = pow(10, 9) + 7;\nclass Solution {\npublic:\n int numPrimeArrangements(int n) {\n \n vector<bool> seive(n + 1, true);\n seive[1] = false;\n int no_of_primes = 0;\n for(int i = 2; i <= n; i++)\n if(seive[i])\n {\n no_of_primes++;\n int tmp = 2;\n while(tmp * i <= n)\n seive[tmp * i] = false, tmp++;\n }\n \n long long ans = 1;\n for(int i = 1; i <= no_of_primes; i++)\n ans = (ans * i) % M;\n \n for(int i = 1; i <= n - no_of_primes; i++)\n ans = (ans * i) % M;\n \n return ans;\n }\n};\n```
2
0
['C']
0
prime-arrangements
Rust solution
rust-solution-by-bigmih-zdi4
\nconst MOD: i64 = 1_000_000_007;\nconst PRIME: [i32; 25] = [\n 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 9
BigMih
NORMAL
2021-10-09T20:54:10.361560+00:00
2021-10-09T20:54:10.361588+00:00
170
false
```\nconst MOD: i64 = 1_000_000_007;\nconst PRIME: [i32; 25] = [\n 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,\n];\n\nimpl Solution {\n pub fn num_prime_arrangements(n: i32) -> i32 {\n let factorial_mod = |n: i32| -> i64 { (2..=n as i64).fold(1, |acc, x| (acc * x) % MOD) };\n let num_primes = PRIME.iter().take_while(|&&x| x <= n).count() as i32;\n ((factorial_mod(num_primes) * factorial_mod(n - num_primes)) % MOD) as i32\n }\n}\n```
2
0
['Rust']
0
prime-arrangements
Java - 1 ms
java-1-ms-by-cpp_to_java-erju
\nclass Solution {\n final int MOD = 1000000007;\n \n public int numPrimeArrangements(int n) {\n int p = countPrimes(n);\n \n int
cpp_to_java
NORMAL
2021-07-29T05:21:34.075200+00:00
2021-07-29T05:21:34.075249+00:00
261
false
```\nclass Solution {\n final int MOD = 1000000007;\n \n public int numPrimeArrangements(int n) {\n int p = countPrimes(n);\n \n int i;\n long prod = 1;\n \n for(i = 1; i <= p; i++){\n prod = (prod * i) % MOD;\n }\n \n for(i = 1; i <= (n - p); i++){\n prod = (prod * i) % MOD; // number of desired permutations = p! * (n - p)!\n }\n \n return (int)prod;\n }\n \n private int countPrimes(int n){\n if(n < 2)\n return 0;\n else{\n boolean[] prime = new boolean[n + 1];\n Arrays.fill(prime, true);\n prime[0] = false;\n prime[1] = false;\n \n int i, j, k, x, count = 0;\n \n for(i = 2; i <= n; i++){\n k = n / i;\n x = i;\n for(j = 2; j <= k; j++){\n x = x + i;\n prime[x] = false;\n }\n }\n \n for(i = 2; i <= n; i++){\n if(prime[i]){\n ++count;\n }\n }\n \n return count;\n }\n }\n}\n```
2
0
[]
0
prime-arrangements
Python - the sieve of Eratosthenes
python-the-sieve-of-eratosthenes-by-blue-rsdw
\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n def sieveOfEratosthenes(n):\n primes = [1] * (n + 1)\n for
blue_sky5
NORMAL
2020-11-16T02:29:09.705844+00:00
2020-11-16T02:29:09.705890+00:00
481
false
```\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n def sieveOfEratosthenes(n):\n primes = [1] * (n + 1)\n for i in range(2, int(math.sqrt(n))+1):\n if primes[i]:\n primes[i*i:n+1:i] = [0] * len(primes[i*i:n+1:i]) \n return sum(primes) - 2\n \n count = sieveOfEratosthenes(n) \n return math.factorial(n-count) * math.factorial(count) % (10**9 + 7)\n```
2
0
['Python', 'Python3']
0
prime-arrangements
Java Prime counts and Permutation formula
java-prime-counts-and-permutation-formul-k1k2
\n public int numPrimeArrangements(int n) {\n boolean[] p = new boolean[n + 1];\n long cp = 0;\n for (int i = 2; i <= n; i++) {\n
hobiter
NORMAL
2020-07-19T23:56:34.452614+00:00
2020-07-19T23:56:34.452663+00:00
98
false
```\n public int numPrimeArrangements(int n) {\n boolean[] p = new boolean[n + 1];\n long cp = 0;\n for (int i = 2; i <= n; i++) {\n if(p[i]) continue;\n cp++;\n for (int j = 2; j * i <= n; j++) {\n p[j * i] = true;\n }\n }\n long cnp = n - cp, res = 1, mod = 1_000_000_007;\n for (long cnt : new long[]{cp, cnp}){\n for (long i = cnt; i >= 1; i--) {\n res = res * i % mod;\n }\n }\n return (int) res; \n }\n```
2
1
[]
0
prime-arrangements
100 % fast 0 ms java easy Solution
100-fast-0-ms-java-easy-solution-by-sanj-z452
```\nclass Solution {\n \n boolean isPrime(int n){\n if(n <= 1)\n return false;\n if(n <= 3)\n return true;\n i
sanjeet_raj
NORMAL
2020-05-15T19:49:07.559085+00:00
2020-05-15T19:49:07.559134+00:00
110
false
```\nclass Solution {\n \n boolean isPrime(int n){\n if(n <= 1)\n return false;\n if(n <= 3)\n return true;\n if(n % 2 == 0 || n % 3 == 0)\n return false;\n for(int i = 5; i * i <= n; i+=6){\n if(n % i == 0 || n % (i+2) == 0)\n return false;\n }\n return true;\n }\n \n public int numPrimeArrangements(int n) {\n \n int primtCount = 0;\n int mod = (int)(1e9+7);\n \n for(int i = 1; i <= n; i++)\n if(isPrime(i))\n primtCount++;\n \n int nonPrime = n - primtCount;\n long fact[] = new long[n+1];\n fact[0] = 1;\n \n for(int i = 1; i <= n; i++){\n fact[i] = (fact[i-1] * i) % mod;\n }\n \n long ans = (fact[primtCount] * fact[nonPrime]) % mod;\n return (int)ans;\n \n }\n}
2
0
[]
0
prime-arrangements
Concise JS Solution
concise-js-solution-by-hbjorbj-1s86
\nvar numPrimeArrangements = function(n) {\n const mod = 10**9 + 7;\n let primes = 0, nonPrimes = 0;\n let res = 1;\n for (let i = 1; i <= n; i++) {
hbjorbj
NORMAL
2020-03-17T04:14:29.771379+00:00
2020-10-15T22:41:17.371420+00:00
413
false
```\nvar numPrimeArrangements = function(n) {\n const mod = 10**9 + 7;\n let primes = 0, nonPrimes = 0;\n let res = 1;\n for (let i = 1; i <= n; i++) {\n if (isPrime(i)) res *= ++primes;\n else res *= ++nonPrimes;\n res = res % mod;\n }\n return res;\n // Time Complexity: O(n^(3/2))\n // Space Complexity: O(1)\n};\n\nvar isPrime = function(n) {\n if (n <= 1) return false;\n if (n <= 3) return true;\n let i = 2;\n while (i <= Math.sqrt(n)) {\n if (n % i == 0) return false;\n i++;\n }\n return true;\n}\n```
2
0
['JavaScript']
1
prime-arrangements
JavaScript Solution with Sieve of Eratosthenes
javascript-solution-with-sieve-of-eratos-b14y
javascript\n/**\n * @param {number} n\n * @return {number}\n */\nvar numPrimeArrangements = function(n) {\n const primes = countPrimes(n);\n return (factorial
shengdade
NORMAL
2020-02-25T19:47:00.631903+00:00
2020-02-25T19:49:05.352681+00:00
382
false
```javascript\n/**\n * @param {number} n\n * @return {number}\n */\nvar numPrimeArrangements = function(n) {\n const primes = countPrimes(n);\n return (factorial(primes) * factorial(n - primes)) % 1000000007n;\n};\n\n// return factorial after modulo operation\nconst factorial = n =>\n n <= 1 ? 1n : (BigInt(n) * factorial(n - 1)) % 1000000007n;\n\n// return number of primes within [1,n]\nconst countPrimes = function(n) {\n const nums = [...Array(n + 1).keys()].slice(2);\n for (let i = 0; i <= Math.floor(Math.sqrt(n)); i++) {\n if (nums[i]) {\n for (let j = i + nums[i]; j <= n; j += nums[i]) {\n nums[j] = undefined; // Sieve of Eratosthenes\n }\n }\n }\n return nums.filter(n => n).length;\n};\n```\n\n* 100/100 cases passed (44 ms)\n* Your runtime beats 100 % of javascript submissions\n* Your memory usage beats 100 % of javascript submissions (35.3 MB)
2
1
['JavaScript']
0
prime-arrangements
Go 0ms solution
go-0ms-solution-by-tjucoder-fob2
go\nfunc numPrimeArrangements(n int) int {\n prime, noPrime := 0, 0\n for i := 1; i <= n; i++ {\n if isPrime(i) {\n prime++ \n }
tjucoder
NORMAL
2020-02-01T09:56:26.594063+00:00
2020-02-01T09:56:26.594108+00:00
165
false
```go\nfunc numPrimeArrangements(n int) int {\n prime, noPrime := 0, 0\n for i := 1; i <= n; i++ {\n if isPrime(i) {\n prime++ \n } else {\n noPrime++\n }\n }\n answer := 1\n for i := 2; i <= prime; i++ {\n answer *= i\n answer %= 1000000007\n }\n for i := 2; i <= noPrime; i++ {\n answer *= i\n answer %= 1000000007\n }\n return answer\n}\n\nfunc isPrime(n int) bool {\n if n == 1 {\n return false\n }\n for i := 2; i < n; i++ {\n if n % i == 0 {\n return false\n }\n }\n return true\n}\n```
2
0
['Go']
0
prime-arrangements
dp
dp-by-je390-popz
dp[n] = number of prime arrangements with [1.2,3,4,5,6,7,8,...,n]\n\nif n is prime\ndp[n] = (number of primes <= n) * dp[n-1]\n\nif n is not prime\ndp[n] = (num
je390
NORMAL
2019-09-01T20:30:31.304904+00:00
2019-09-01T20:31:14.681121+00:00
137
false
dp[n] = number of prime arrangements with [1.2,3,4,5,6,7,8,...,n]\n\nif n is prime\ndp[n] = (number of primes <= n) * dp[n-1]\n\nif n is not prime\ndp[n] = (number of non primes <= n) * dp[n-1]\n\navoid using memory O(n) and made it O(1) in memory\n\n\n\n```\nclass Solution(object):\n def numPrimeArrangements(self, n):\n p = 0 # number of primes\n np = 1 # number of non primes \n og = 1 # on going number of prime arrangements \n mod = 10 ** 9 + 7\n for i in range(2,n + 1):\n if self.isprime(i):\n p += 1\n og = (og * p) % mod\n else:\n np += 1\n og = (og * np) % mod\n return og\n \n def isprime(self,n):\n return n > 1 and all([n % ii != 0 for ii in range(2,min(n,int(n ** 0.5) + 1))])\n```
2
0
[]
0
prime-arrangements
Solution in Python 3 (beats 100.00%) (one line)
solution-in-python-3-beats-10000-one-lin-i4rz
```\nfrom math import factorial as f\nfrom bisect import bisect_right as br\n\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n \treturn
junaidmansuri
NORMAL
2019-09-01T04:17:17.478313+00:00
2019-09-01T04:20:33.814195+00:00
433
false
```\nfrom math import factorial as f\nfrom bisect import bisect_right as br\n\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n \treturn (lambda x:(f(x)*f(n-x))%(10**9+7))(br([2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97],n))\n\n\n- Junaid Mansuri\n(LeetCode ID)@hotmail.com
2
2
['Python', 'Python3']
1
prime-arrangements
Easy Java Solution || Beats 100% of Java Users .
easy-java-solution-beats-100-of-java-use-o7yd
IntuitionApproachComplexity Time complexity: Space complexity: Code
zzzz9
NORMAL
2025-02-24T00:10:43.235398+00:00
2025-02-24T00:10:43.235398+00:00
143
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public boolean isPrime( int n ){ if( n == 2 || n == 3 ) return true ; if( n % 2 == 0 || n % 3 == 0 ) return false ; int i = 5 ; while( i*i <= n ){ if( n % i == 0 || n % (i+2) == 0 ){ return false ; } i += 6 ; } return true ; } public int numPrimeArrangements(int n) { int mod = 1_000_000_007 ; int primes = 0 ; for( int i=2 ; i<=n ; ++i ){ if( isPrime(i) ) primes++ ; } int others = n - primes ; long rs = 1 ; while( primes > 0 ){ rs = ( rs * primes ) % mod ; primes-- ; } while( others > 0 ){ rs = ( rs * others ) % mod ; others-- ; } return (int) rs ; } } ```
1
0
['Math', 'Combinatorics', 'Counting', 'Java']
0
prime-arrangements
Python solution | Sieve of Eratosthenes and simple combinatorics
python-solution-sieve-of-eratosthenes-an-cie6
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
vncvtkv
NORMAL
2024-11-29T18:38:30.556256+00:00
2024-11-29T18:38:30.556297+00:00
212
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nfrom math import factorial\n\nclass Solution:\n def countPrimes(self, n: int) -> int:\n\n isPrime = [True] * (n + 1)\n d = 2\n while d * d <= n:\n if isPrime[d]:\n for i in range(d ** 2, n + 1, d):\n isPrime[i] = False\n d += 1\n\n x = len([x for x in isPrime[2:n] if x])\n\n return x\n\n def numPrimeArrangements(self, n: int) -> int:\n\n return factorial(self.countPrimes(n + 1)) * factorial(n - self.countPrimes(n + 1)) % (10**9 + 7)\n \n```
1
0
['Python3']
0
prime-arrangements
Easy || Beat 100%🔥🔥 || C++
easy-beat-100-c-by-vishalkumar00-jpcl
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
vishalkumar00
NORMAL
2024-04-15T14:42:21.672543+00:00
2024-04-15T14:42:21.672579+00:00
546
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int mod=1e9+7;\n long long factorial(int n) {\n return (n==1 || n==0) ? 1: ((n%mod)*factorial(n - 1))%mod; \n } \n bool isPrime(int n){\n if (n <= 1)\n return false;\n for (int i = 2; i <= n / 2; i++){\n if (n % i == 0)\n return false;\n }\n return true;\n }\n int numPrimeArrangements(int n) {\n int noOfPrime=0;\n for(int i=1;i<=n;i++){\n if(isPrime(i)){\n noOfPrime++;\n }\n }\n int nonPrime=n-noOfPrime;\n cout<<noOfPrime<<" "<<nonPrime<<endl;\n return (factorial(noOfPrime)*factorial(nonPrime))%mod;\n }\n};\n```
1
0
['C++']
0
prime-arrangements
Easy Understandable Solution | Beats 100% | Brute Force
easy-understandable-solution-beats-100-b-k7r5
\n\n# Code\n\nclass Solution {\n public int numPrimeArrangements(int n) {\n int numPrime=countPrime(n);\n int numComposite=n-numPrime;\n\n
sumo25
NORMAL
2024-04-15T11:53:58.698874+00:00
2024-04-15T11:53:58.698894+00:00
597
false
\n\n# Code\n```\nclass Solution {\n public int numPrimeArrangements(int n) {\n int numPrime=countPrime(n);\n int numComposite=n-numPrime;\n\n long primePermu=permutation(numPrime);\n long compositePermu=permutation(numComposite);\n\n long result= (primePermu*compositePermu)%(long)(Math.pow(10,9)+7);\n return (int)result;\n }\n public int countPrime(int n){\n int count=0;\n for(int i=2;i<=n;i++){\n int flag=1;\n for(int j=2;j<=Math.sqrt(i);j++){\n if(i%j==0){\n flag=0;\n break;\n }\n }\n if(flag==1){\n count++;\n }\n }\n return count;\n }\n public long permutation(int num){\n long pro=1;\n for(int i=num;i>=1;i--){\n pro=(pro*i)%(long)(Math.pow(10,9)+7);\n }\n return pro;\n }\n}\n```
1
0
['Java']
0
prime-arrangements
Simple java code 0 ms beats 100 %
simple-java-code-0-ms-beats-100-by-arobh-mwjp
\n# Complexity\n- \n\n# Code\n\nclass Solution {\n int mod=(int)1e9 +7;\n public boolean check(int n){\n if(n==2){\n return true;\n
Arobh
NORMAL
2024-01-09T02:59:48.126209+00:00
2024-01-09T02:59:48.126243+00:00
213
false
\n# Complexity\n- \n![image.png](https://assets.leetcode.com/users/images/439423c0-6e3e-48c2-9ff8-91661e1e4601_1704769182.8242888.png)\n# Code\n```\nclass Solution {\n int mod=(int)1e9 +7;\n public boolean check(int n){\n if(n==2){\n return true;\n }\n else if(n%2==0){\n return false;\n }\n int len=(int)Math.sqrt(n);\n for(int i=3;i<=len;i+=2){\n if(n%i==0){\n return false;\n }\n }\n return true;\n }\n public long fact(int n){\n long mul=1;\n for(int i=2;i<=n;i++){\n mul=(mul*i)%mod;\n }\n return mul;\n }\n public int numPrimeArrangements(int n) {\n int count=0;\n for(int i=2;i<=n;i++){\n if(check(i)){\n count++;\n }\n }\n int x=n-count;\n long f1=fact(count)%mod;\n long f2=fact(x)%mod;\n return (int)((f1*f2)%mod);\n }\n}\n```
1
0
['Java']
0
prime-arrangements
Prime Arrangements - JS - fastest solution
prime-arrangements-js-fastest-solution-b-4inh
\nvar numPrimeArrangements = function(n) {\n a = 0, b = 0, res = 1\n mod = 10 ** 9 + 7\n\n for (let i = 1; i <= n; i++) {\n res *= isPrime(i) ?
zemamba
NORMAL
2023-12-29T05:42:12.562714+00:00
2023-12-29T05:46:29.659723+00:00
145
false
```\nvar numPrimeArrangements = function(n) {\n a = 0, b = 0, res = 1\n mod = 10 ** 9 + 7\n\n for (let i = 1; i <= n; i++) {\n res *= isPrime(i) ? ++a : ++b\n res = res % mod\n }\n\n return res\n\n function isPrime (num) {\n s = Math.sqrt(num)\n\n for (let i = 2; i <= s; i++) {\n if (num % i == 0) return false\n }\n\n return num > 1\n }\n\n};\n```
1
0
['JavaScript']
0
prime-arrangements
using BigInt in dart and making the fastest solution in dart
using-bigint-in-dart-and-making-the-fast-j3e9
\n# Complexity\n- Time complexity:\n O(n * sqrt(n))\n- Space complexity:\n O(1)\n# Code\n\nclass Solution {\n static const int MOD = 1000000007;\n\n int numPr
ahmed___
NORMAL
2023-09-08T22:07:28.451859+00:00
2023-09-08T22:07:28.451878+00:00
4
false
\n# Complexity\n- Time complexity:\n O(n * sqrt(n))\n- Space complexity:\n O(1)\n# Code\n```\nclass Solution {\n static const int MOD = 1000000007;\n\n int numPrimeArrangements(int n) {\n int primeCount = countPrimes(n);\n int nonPrimeCount = n - primeCount;\n\n BigInt primePermutations = calculatePermutations(primeCount);\n BigInt nonPrimePermutations = calculatePermutations(nonPrimeCount);\n\n BigInt result = (primePermutations * nonPrimePermutations) % BigInt.from(MOD);\n \n return result.toInt();\n }\n\n int countPrimes(int n) {\n int count = 0;\n for (int i = 1; i <= n; i++) {\n if (isPrime(i)) {\n count++;\n }\n }\n return count;\n }\n\n bool isPrime(int num) {\n if (num <= 1) {\n return false;\n }\n if (num == 2) {\n return true;\n }\n if (num % 2 == 0) {\n return false;\n }\n for (int i = 3; i * i <= num; i += 2) {\n if (num % i == 0) {\n return false;\n }\n }\n return true;\n }\n\n BigInt calculatePermutations(int count) {\n BigInt result = BigInt.from(1);\n for (int i = 1; i <= count; i++) {\n result = (result * BigInt.from(i)) % BigInt.from(MOD);\n }\n return result;\n }\n}\n\n```
1
0
['Dart']
0
prime-arrangements
pre-computation (sieve + prefix) + math (factorial) || optimal
pre-computation-sieve-prefix-math-factor-bjti
\n# Approach\n Describe your approach to solving the problem. \nAs, precomputation\n1. Make sieve\n2. Apply prefix\n\nIn main function,\n1. Calculate factorial
ankit_m15
NORMAL
2023-07-26T18:10:58.985783+00:00
2023-07-26T18:10:58.985812+00:00
224
false
\n# Approach\n<!-- Describe your approach to solving the problem. -->\nAs, precomputation\n1. Make sieve\n2. Apply prefix\n\nIn main function,\n1. Calculate factorial (considering Mod=1e9+7)\nfact(prefix[n])\nfact(prefixn-prefix)\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n#include <iostream>\n#include <vector>\n\nconst long long MOD = 1e9 + 7;\n\nclass Solution {\nprivate:\n std::vector<int> prefix;\n\npublic:\n Solution() {\n prefix.assign(101, 1);\n prefix[0] = 0;\n prefix[1] = 0;\n for (int i = 2; i <= 100; i++) {\n if (prefix[i]) {\n for (int j = i * i; j <= 100; j += i) {\n prefix[j] = 0;\n }\n }\n }\n for (int i = 1; i <= 100; i++) {\n prefix[i] += prefix[i - 1];\n }\n }\n long long fact(int n){\n long long result=1;\n for(int i=1; i<=n; i++){\n result*=i;\n result%=MOD;\n }\n return result;\n }\n int numPrimeArrangements(int n) {\n return (fact(prefix[n])*fact(n-prefix[n]))%MOD;\n }\n};\n\n\n```
1
0
['Math', 'Prefix Sum', 'C++']
2
prime-arrangements
Beats 100% in time and space, C++ code
beats-100-in-time-and-space-c-code-by-va-xck1
\n# Code\n\nclass Solution {\npublic:\n int numPrimeArrangements(int n) {\n \n int arr[100];\n for (int i=0; i<100; i++) {\n
vanshdhawan60
NORMAL
2023-07-24T22:10:56.631358+00:00
2023-07-24T22:10:56.631381+00:00
445
false
\n# Code\n```\nclass Solution {\npublic:\n int numPrimeArrangements(int n) {\n \n int arr[100];\n for (int i=0; i<100; i++) {\n arr[i] = 1;\n }\n arr[0] = 0;\n for (int i=2; i*i<=100; i++) {\n for (int j=i; j*i<=100; j++) {\n arr[(i*j)-1] = 0;\n }\n }\n for (int i=1; i<100; i++) {\n arr[i] += arr[i-1];\n }\n long long int ans =1; int r = arr[n-1];\n long long int req = 1e9 + 7;\n for (int i=1; i<=r; i++) {\n ans = (ans%req * i%req)%req;\n }\n for (int i=1; i<=(n-r); i++) {\n ans = (ans%req * i%req)%req;\n }\n return ans;\n }\n};\n```
1
0
['C++']
0
prime-arrangements
🥇 Beats 100% | Java, C++, Python | Explanation
beats-100-java-c-python-explanation-by-w-c0d6
TL;DR\njava []\nclass Solution {\n private static boolean isPrime(int n) {\n for (int i = 2; i <= n / 2; i++) {\n if (n % i == 0) return fa
wallandteen
NORMAL
2023-05-19T06:38:07.128081+00:00
2023-05-19T06:38:24.161135+00:00
124
false
# TL;DR\n``` java []\nclass Solution {\n private static boolean isPrime(int n) {\n for (int i = 2; i <= n / 2; i++) {\n if (n % i == 0) return false;\n }\n return true;\n }\n\n private static long factorial(int n, long mod) {\n long f = 1;\n for (int i = 2; i <= n; i++) {\n f = (f * i) % mod;\n }\n return f;\n }\n\n public int numPrimeArrangements(int n) {\n int mod = (int) 1e9 + 7;\n int count = 0;\n for (int i = 2; i <= n; i++) {\n if (isPrime(i)) count++;\n }\n return (int) ((factorial(count, mod) * factorial(n - count, mod)) % mod);\n }\n}\n```\n``` cpp []\nclass Solution {\nprivate:\n bool isPrime(int n) {\n for (int i = 2; i <= n / 2; i++) {\n if (n % i == 0) return false;\n }\n return true;\n }\n\n long long factorial(int n, long long mod) {\n long long f = 1;\n for (int i = 2; i <= n; i++) {\n f = (f * i) % mod;\n }\n return f;\n }\n\npublic:\n int numPrimeArrangements(int n) {\n const int mod = 1000000007;\n int count = 0;\n for (int i = 2; i <= n; i++) {\n if (isPrime(i)) count++;\n }\n return (factorial(count, mod) * factorial(n - count, mod)) % mod;\n }\n};\n\n```\n``` python []\nclass Solution:\n def isPrime(self, n: int) -> bool:\n for i in range(2, n // 2 + 1):\n if n % i == 0:\n return False\n return True\n\n def factorial(self, n: int, mod: int) -> int:\n f = 1\n for i in range(2, n + 1):\n f = (f * i) % mod\n return f\n\n def numPrimeArrangements(self, n: int) -> int:\n mod = 10 ** 9 + 7\n count = 0\n for i in range(2, n + 1):\n if self.isPrime(i):\n count += 1\n return (self.factorial(count, mod) * self.factorial(n - count, mod)) % mod\n\n```\n\n---\n#### \u26A0\uFE0F Please upvote if you like this solution. \uD83D\uDE43\n---\n\n# Approach\n\nTo solve this problem, we can make use of the properties of permutations and prime numbers.\n\nA permutation is an arrangement of objects in a specific order. The number of permutations of a set of `n` objects is `n!` (n factorial), which is the product of all positive integers less than or equal to `n`. This is because for each position, we can choose from `n` objects, then `n-1` for the next position, and so on.\n\nWe start by determining the number of prime numbers within `n`. We can count the prime numbers using a simple function that checks divisibility.\n\nOnce we know the count of prime numbers (let\'s denote it as `p`), we also know the count of non-prime numbers, which is `n - p`.\n\nNotice that the count of prime indices within `n` is exactly `p`, and the count of non-prime indices is `n - p`.\n\nWe can thus generate `p!` permutations of prime numbers at prime positions and `(n - p)!` permutations of non-prime numbers at non-prime positions. \n\nSince these two sets of positions (prime and non-prime) do not intersect, we can simply multiply these two quantities to get the total number of valid arrangements. \n\n# Complexity \n\n- Time complexity: $$O(n^2)$$\n \n- Space complexity: $$O(1)$$
1
0
['Combinatorics', 'C++', 'Java', 'Python3']
0
prime-arrangements
easy to understand || must see
easy-to-understand-must-see-by-akshat061-w3vu
Code\n\nclass Solution {\npublic:\n const int mod = 1e9+7;\n vector<bool>dp;\n unordered_map<long long int,long long int>memo;\n int numPrimeArrange
akshat0610
NORMAL
2023-04-19T09:02:18.998246+00:00
2023-04-19T09:02:18.998287+00:00
672
false
# Code\n```\nclass Solution {\npublic:\n const int mod = 1e9+7;\n vector<bool>dp;\n unordered_map<long long int,long long int>memo;\n int numPrimeArrangements(int n) \n {\n dp.resize(n+1,true); //initally considering all to be prime\n dp[0] = false;\n dp[1] = false;\n\n for(int i=2;i<dp.size();i++)\n {\n //if the curr num is prime\n if(dp[i] == true)\n {\n for(int j=i+i;j<dp.size();j+=i)\n {\n dp[j] = false;\n }\n }\n }\n int composite = 0;\n int prime = 0;\n for(int i=1;i<dp.size();i++)\n {\n if(dp[i] == true) prime++;\n else composite++;\n }\n //cout<<"prime = "<<prime<<endl;\n //cout<<"composite = "<<composite<<endl;\n\n int val1 = fact(prime)%mod;\n //cout<<"val1 = "<<val1<<endl;\n\n int val2 = fact(composite)%mod;\n //cout<<"val2 = "<<val2<<endl;\n\n int ans = (1LL * val1 * val2)%mod;\n return ans;\n }\n int fact(int num)\n {\n if(num == 1)\n return 1;\n\n if(num == 0)\n return 1;\n \n if(memo.find(num) != memo.end())\n {\n return memo[num];\n }\n\n return memo[num] = (1LL * num * fact(num-1))%mod;\n }\n \n};\n\n```
1
0
['Math', 'C++']
1
prime-arrangements
Easiest cpp solution || sieve algo ||permutation combination
easiest-cpp-solution-sieve-algo-permutat-nde8
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
atmajit24
NORMAL
2023-01-29T20:13:29.378511+00:00
2023-01-29T20:13:29.378583+00:00
50
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(nloglogn)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\no(n)\n# Code\n```\nclass Solution {\npublic:\n int numPrimeArrangements(int n) {\n bool prime[n+1];\n memset(prime,true,sizeof(prime));\n prime[0]=prime[1]=false;\n for(int i=2;i*i<=n;i++){\n if(prime[i]){\n for(int j=i*2;j<=n;j+=i){\n prime[j]=false;\n }\n }\n }\n int cnt=0,cnt1=0;;\n for(int i=1;i<=n;i++){\n if(prime[i]){\n cnt++;\n }else{\n cnt1++;\n }\n }\n long long ans=1,k=1e9+7;\n for(int i=cnt;i>1;i--){\n ans=(ans*i)%k;\n }\n for(int i=cnt1;i>1;i--){\n ans=(ans*i)%k;\n }\n return ans;\n }\n};\n```
1
0
['C++']
0
prime-arrangements
[Accepted] Swift
accepted-swift-by-vasilisiniak-vheh
\nclass Solution {\n func numPrimeArrangements(_ n: Int) -> Int {\n \n func p(_ x: Int) -> Bool {\n guard x > 1 else { return false
vasilisiniak
NORMAL
2023-01-13T09:04:44.328158+00:00
2023-01-13T09:04:44.328218+00:00
103
false
```\nclass Solution {\n func numPrimeArrangements(_ n: Int) -> Int {\n \n func p(_ x: Int) -> Bool {\n guard x > 1 else { return false }\n guard x > 3 else { return true }\n return (2...(x / 2)).allSatisfy { x % $0 != 0 }\n }\n \n func f(_ x: Int) -> Int {\n guard x > 0 else { return 1 }\n return (1...x).reduce(into: 1) { $0 = ($0 * $1) % 1000000007 }\n }\n \n let pc = (1...n).filter(p).count\n let nc = n - pc\n \n return f(pc) * f(nc) % 1000000007\n }\n}\n```
1
0
['Swift']
0
prime-arrangements
c++ | easy | short
c-easy-short-by-venomhighs7-7jca
\n# Code\n\nclass Solution {\npublic:\n int fact(int q)\n\t{\n\t\tlong a=1;\n\t\tint i=1;\n\t\twhile(i<=q)\n\t\t{\n\t\t\ta=(a*i)%m;\n\t\t\ti++;\n\t\t}\n\t\tr
venomhighs7
NORMAL
2022-10-17T00:33:18.409906+00:00
2022-10-17T00:33:18.409955+00:00
703
false
\n# Code\n```\nclass Solution {\npublic:\n int fact(int q)\n\t{\n\t\tlong a=1;\n\t\tint i=1;\n\t\twhile(i<=q)\n\t\t{\n\t\t\ta=(a*i)%m;\n\t\t\ti++;\n\t\t}\n\t\treturn a%m;\n\t}\n\tint m=1000000007;\n\tint prime(int p)\n\t{\n\t\tint ans=0;\n\t\tfor(int i=2;i<=p;i++)\n\t\t{\n\t\t\tint j=2;\n\t\t\twhile(j<i)\n\t\t\t{\n\t\t\t\tif(i%j==0)break;\n\t\t\t\tj++;\n\t\t\t}\n\t\t\tif(j==i) ans++;\n\t\t}\n\t\tans=ans%m;\n\t\treturn ans;\n\t}\n\tint numPrimeArrangements(int n) \n\t{\n\t\tint x=prime(n);\n\t\tlong y=fact(x);\n\t\tlong z=fact(n-x);\n\t\tlong s=(y*z)%m;\n\t\treturn s;\n\t}\n};\n```
1
0
['C++']
0
prime-arrangements
Soluson fa brodas
soluson-fa-brodas-by-vjk008-2mmj
Soluson fa brodas in java usin MassifIntejah\n\nimport java.math.BigInteger;\nclass Solution {\n \n public int numPrimeArrangements(int n) {\n int
vjk008
NORMAL
2022-09-22T14:23:27.973835+00:00
2022-09-22T14:23:27.973893+00:00
829
false
Soluson fa brodas in java usin MassifIntejah\n```\nimport java.math.BigInteger;\nclass Solution {\n \n public int numPrimeArrangements(int n) {\n int np = numOfPrimes(n);\n int nnp = n-np;\n BigInteger ansa = fact(np).multiply(fact(nnp));\n ansa = ansa.mod(BigInteger.valueOf(1000000007));\n return ansa.intValue();\n }\n \n private BigInteger fact(int n){\n BigInteger ret = BigInteger.valueOf(1);\n while(n > 1){\n ret = ret.multiply(BigInteger.valueOf(n));\n n--;\n }\n return ret;\n }\n \n private int numOfPrimes(int n){\n int[] primes = new int[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};\n int count = 0;\n for(int each : primes){\n if(each > n){\n break;\n }\n count++;\n }\n return count;\n }\n \n}\n```
1
0
['Java']
0
prime-arrangements
EZ Python Solution for Bruddas
ez-python-solution-for-bruddas-by-r0adma-0ydw
\nimport math\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53
r0adman
NORMAL
2022-09-22T13:59:10.138948+00:00
2022-09-22T13:59:10.138989+00:00
112
false
```\nimport math\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n \n primesn = []\n \n if(n==2):\n return 1\n for i in range(len(primes)):\n if(primes[i]<=n):\n primesn.append(primes[i])\n \n \n return (math.factorial(len(primesn))*math.factorial(n-len(primesn))) % (10**9 + 7)\n ```
1
1
[]
0
prime-arrangements
Python Seive + Maths
python-seive-maths-by-vigneswar_a-414y
py\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n \n table = [True] * (n+1)\n count = 0\n \n for i in
Vigneswar_A
NORMAL
2022-09-14T16:27:21.048115+00:00
2022-09-14T16:27:21.048161+00:00
550
false
```py\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n \n table = [True] * (n+1)\n count = 0\n \n for i in range(2, n+1):\n if table[i] is True:\n for i in range(i*i, n+1, i):\n table[i] = False\n count += 1\n \n\n return (factorial(count)*factorial(n-count))%1000000007\n```
1
0
['Python']
0
prime-arrangements
Prime Arrangement-Sieve of erotosthenes
prime-arrangement-sieve-of-erotosthenes-p9gc0
\nclass Solution {\n long mod = (long)(1e9+7);\n public int numPrimeArrangements(int n) {\n if(n==1){\n return 1;\n }\n \n
ayushkumarsingh594
NORMAL
2022-08-03T17:40:06.377658+00:00
2022-08-03T17:40:06.377692+00:00
368
false
```\nclass Solution {\n long mod = (long)(1e9+7);\n public int numPrimeArrangements(int n) {\n if(n==1){\n return 1;\n }\n \n boolean[] arr = new boolean[n+1];\n Arrays.fill(arr,true);\n arr[0]=false;\n arr[1]=false;\n \n for(int i=2;i<=Math.sqrt(n);i++){\n \n for(int j=i*i;j<=n;j+=i){\n if(arr[i]==false){\n continue;\n }\n arr[j]=false;\n }\n \n }\n long prime = 0;\n long notPrime=0;\n for(int k=1;k<arr.length;k++){\n if(arr[k]==true){\n prime++;\n }\n else{\n notPrime++;\n }\n }\n\n long x = factorial(prime)%mod;\n long y = factorial(notPrime)%mod;\n long t = (x*y)%mod;\n return (int)t ;\n \n }\n \n public long factorial(long i){\n if(i<=1){\n return i;\n }\n return (i*(factorial(i-1)%mod))%mod;\n }\n}\n```\nIf you find helpful please upvote.
1
0
['Java']
0
prime-arrangements
c++ || simple code || 0 ms
c-simple-code-0-ms-by-vinaysingh20-y8lb
\tclass Solution \n\t{\n\tpublic:\n\n\t\tint fact(int q)\n\t\t{\n\t\t\tlong a=1;\n\t\t\tint i=1;\n\t\t\twhile(i<=q)\n\t\t\t{\n\t\t\t\ta=(ai)%m;\n\t\t\t\ti++;\n\
vinaysingh20
NORMAL
2022-08-01T19:44:33.520172+00:00
2022-08-01T19:44:33.520217+00:00
533
false
\tclass Solution \n\t{\n\tpublic:\n\n\t\tint fact(int q)\n\t\t{\n\t\t\tlong a=1;\n\t\t\tint i=1;\n\t\t\twhile(i<=q)\n\t\t\t{\n\t\t\t\ta=(a*i)%m;\n\t\t\t\ti++;\n\t\t\t}\n\t\t\treturn a%m;\n\t\t}\n\t\tint m=1000000007;\n\t\tint prime(int p)\n\t\t{\n\t\t\tint ans=0;\n\t\t\tfor(int i=2;i<=p;i++)\n\t\t\t{\n\t\t\t\tint j=2;\n\t\t\t\twhile(j<i)\n\t\t\t\t{\n\t\t\t\t\tif(i%j==0)break;\n\t\t\t\t\tj++;\n\t\t\t\t}\n\t\t\t\tif(j==i) ans++;\n\t\t\t}\n\t\t\tans=ans%m;\n\t\t\treturn ans;\n\t\t}\n\t\tint numPrimeArrangements(int n) \n\t\t{\n\t\t\tint x=prime(n);\n\t\t\tlong y=fact(x);\n\t\t\tlong z=fact(n-x);\n\t\t\tlong s=(y*z)%m;\n\t\t\treturn s;\n\t\t}\n\t};
1
0
[]
0
prime-arrangements
Most efficient C solution
most-efficient-c-solution-by-chengordon8-fifk
\nlong int fact(long int a){\n if(a==0 || a==1) return 1;\n long int f=a; \n f=f*fact(a-1); \n \n return f%(1000000007);\n}\n\nint numPrimeA
chengordon8
NORMAL
2022-07-30T16:38:12.614265+00:00
2022-07-30T16:38:39.834637+00:00
236
false
```\nlong int fact(long int a){\n if(a==0 || a==1) return 1;\n long int f=a; \n f=f*fact(a-1); \n \n return f%(1000000007);\n}\n\nint numPrimeArrangements(int n){\n int i;\n int prime[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};\n long int composites=0,primes=0;\n \n for(i=0;i<25;i++)\n if(n>=prime[i])\n primes++;\n composites=n-primes;\n return (fact(primes)*fact(composites))%1000000007; \n}\n```
1
0
[]
0
prime-arrangements
Java | Reuse LC 204 | Explained
java-reuse-lc-204-explained-by-prashant4-pnvo
Prerequisite: LC 204. Count Primes \nIdea:\n Reuse LC 204 to count primes upto n.\n This implementation is long but is efficient. Use any implementation of your
prashant404
NORMAL
2022-03-02T02:31:32.927579+00:00
2022-03-02T02:37:44.690861+00:00
207
false
**Prerequisite:** [LC 204. Count Primes](https://leetcode.com/problems/count-primes/discuss/1141636/Java-or-Easy-or-Sieve-of-Eratosthenes) \n**Idea:**\n* Reuse LC 204 to count primes upto n.\n* This implementation is long but is efficient. Use any implementation of your choice to count primes up to n\n* If there are p primes up to n, then they can be placed at any of the p positions, so there are p! ways to permute them\n* The remaining `n - p` composites can be placed at any of the remaining `n - p` positions, so there are `(n - p)!` ways to permute them\n* So total permutations = p! x (n - p)!\n* Example: n = 5\n```\nPrimes = 3, 5 can be placed at positions = 3 or 5\nRemaining = 1, 2, 4 can be placed at positions = 1, 2, 4\nArrangements = 2! x 3! = 2 x 6 = 12 [Ans]\n```\n```\nprivate static final int MOD = (int) (1e9 + 7);\n\npublic int numPrimeArrangements(int n) {\n\tif (n < 3)\n\t\treturn 1;\n\tvar count = countPrimes(n);\n\treturn (int) (factorial(count) * factorial(n - count) % MOD);\n}\n\npublic int countPrimes(int n) {\n\tvar count = 2;\n\tvar sieve = new HashSet<Integer>();\n\taddToSieve(sieve, 3, n);\n\n\tfor (var j = 5; j <= n; j += 2)\n\t\tcount += checkPrime(j, sieve, n);\n\treturn count;\n}\n\nprivate void addToSieve(Set<Integer> sieve, int prime, int n) {\n\tsieve.add(prime);\n\tfor (var i = prime * prime; i <= n; i += prime)\n\t\tsieve.add(i);\n}\n\nprivate int checkPrime(int j, Set<Integer> sieve, int n) {\n\tif (sieve.contains(j))\n\t\treturn 0;\n\tvar i = 5;\n\tvar w = 2;\n\n\twhile (i * i <= j) {\n\t\tif (j % i == 0)\n\t\t\treturn 0;\n\t\ti += w;\n\t\tw = 6 - w;\n\t}\n\n\taddToSieve(sieve, j, n);\n\treturn 1;\n}\n\nprivate long factorial(int a) {\n\tvar fac = 1L;\n\tfor (var i = 2; i <= a; i++)\n\t\tfac = fac * i % MOD;\n\treturn fac;\n}\n```\n***Please upvote if this helps***
1
0
['Java']
0
prime-arrangements
Getting TLE CAN ANYONE HELP ...?
getting-tle-can-anyone-help-by-82877hars-877r
\nclass Solution {\n int v=pow(10,9)+7;\n private:\n long long int fact(int n)\n {\n if(n==0 || n==1)\n {\n
82877harsh
NORMAL
2021-12-15T15:06:20.194466+00:00
2021-12-15T15:06:20.194506+00:00
82
false
````\nclass Solution {\n int v=pow(10,9)+7;\n private:\n long long int fact(int n)\n {\n if(n==0 || n==1)\n {\n return 1;\n }\n return n*fact(n-1);\n \n }\npublic:\n int numPrimeArrangements(int n) {\n int primes[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};\n unordered_map<int,int>harsh;\n for(int i=0;i<=24;i++)\n {\n harsh[primes[i]]++;\n }\n int cnt=0;\n for(int i=1;i<=5;i++)\n {\n if(harsh.find(i)!=harsh.end())\n {\n cnt++;\n }\n }\n return fact(cnt)*fact(n-cnt)%v;\n }\n};\n````
1
0
[]
1
prime-arrangements
C# LINQ three-liner with precomputed primes
c-linq-three-liner-with-precomputed-prim-77kt
csharp\npublic class Solution\n{\n private static HashSet<int> _primes = new HashSet<int> { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 6
kimsey0
NORMAL
2021-11-24T13:54:20.163071+00:00
2021-11-24T13:54:20.163111+00:00
98
false
```csharp\npublic class Solution\n{\n private static HashSet<int> _primes = new HashSet<int> { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 };\n\n public int NumPrimeArrangements(int n)\n {\n var primes = Enumerable.Range(1, n).Count(_primes.Contains);\n return (int)Enumerable.Range(1, primes)\n .Concat(Enumerable.Range(1, n - primes))\n .Aggregate(1L, (a, n) => a * n % 1000000007);\n }\n}\n```
1
0
[]
1
prime-arrangements
O(n) time, O(1) space
on-time-o1-space-by-yayarokya-tm7r
\npublic class Solution {\n static readonly int modulo = 1_000_000_007;\n static readonly int[] primes = new int[] {\n 2, 3, 5, 7, 11, 13, 17, 19,
yayarokya
NORMAL
2021-10-11T06:20:59.450759+00:00
2021-10-11T06:21:50.616816+00:00
137
false
```\npublic class Solution {\n static readonly int modulo = 1_000_000_007;\n static readonly int[] primes = new int[] {\n 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, \n 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 \n };\n public int NumPrimeArrangements(int n) {\n var p = 0;\n long res = 1;\n for(; p < primes.Length && primes[p] <= n; ++p) {}\n for(int i = n - p; 0 < i; --i) {\n res = (res*i) % modulo;\n }\n for(int i = p; 0 < i; --i) {\n res = (res*i) % modulo;\n }\n return (int)res;\n }\n}\n```
1
1
[]
0
prime-arrangements
c++(0ms 100%) small and easy to understand
c0ms-100-small-and-easy-to-understand-by-skla
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Prime Arrangements.\nMemory Usage: 6 MB, less than 46.62% of C++ online submissions for Prime A
zx007pi
NORMAL
2021-06-02T15:16:53.993809+00:00
2021-06-02T15:18:59.629016+00:00
344
false
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Prime Arrangements.\nMemory Usage: 6 MB, less than 46.62% of C++ online submissions for Prime Arrangements.\n```\nclass Solution {\npublic:\n int numPrimeArrangements(int n) { \n vector<int>prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};\n long answer = 1, modulo = 1000000007, p = 0;\n \n for(auto &i : prime) \n if(i <= n) p++;\n else break;\n \n n -= p;\n \n for(long i = 1; i <= n; i++) answer = (answer * i) % modulo;\n for(long i = 1; i <= p; i++) answer = (answer * i) % modulo ;\n \n return answer;\n }\n};\n```
1
0
['C', 'C++']
0
prime-arrangements
100% faster than other C++ solutions
100-faster-than-other-c-solutions-by-dp5-otcz
\nclass Solution {\npublic:\n bool isPrime(int n){\n if(n==1){\n return false;\n }\n for(int i=2;i<n;i++){\n if(n%
dp55
NORMAL
2021-03-16T10:16:05.852831+00:00
2021-03-16T10:16:05.852866+00:00
301
false
```\nclass Solution {\npublic:\n bool isPrime(int n){\n if(n==1){\n return false;\n }\n for(int i=2;i<n;i++){\n if(n%i==0){\n return false;\n }\n }\n return true;\n }\n int Factorial(int n){\n long int f=1;\n while(n>0){\n f=(f*n)%1000000007;\n n--;\n }\n return f;\n }\n int numPrimeArrangements(int n) {\n int p=0;\n for(int i=2;i<=n;i++){\n if(isPrime(i)){\n p++;\n }\n }\n long int a = Factorial(p);\n long int b = Factorial(n-p);\n return (a*b)%1000000007;\n }\n};\n```
1
0
['C']
0
prime-arrangements
My Python Solution
my-python-solution-by-mbylzy-9nsk
\nclass Solution:\n ret = [1, 1, 2, 4, 12, 36, 144, 576, 2880, 17280, 86400, 604800, 3628800, 29030400, 261273600, 612735986, 289151874, 180670593, 445364737
mbylzy
NORMAL
2021-01-23T14:05:03.019705+00:00
2021-01-23T14:05:03.019736+00:00
78
false
```\nclass Solution:\n ret = [1, 1, 2, 4, 12, 36, 144, 576, 2880, 17280, 86400, 604800, 3628800, 29030400, 261273600, 612735986, 289151874, 180670593, 445364737, 344376809, 476898489, 676578804, 89209194, 338137903, 410206413, 973508979, 523161503, 940068494, 400684877, 13697484, 150672324, 164118783, 610613205, 44103617, 58486801, 462170018, 546040181, 197044608, 320204381, 965722612, 554393872, 77422176, 83910457, 517313696, 36724464, 175182841, 627742601, 715505693, 327193394, 451768713, 263673556, 755921509, 94744060, 600274259, 410695940, 427837488, 541336889, 736149184, 514536044, 125049738, 250895270, 39391803, 772631128, 541031643, 428487046, 567378068, 780183222, 228977612, 448880523, 892906519, 858130261, 622773264, 78238453, 146637981, 918450925, 514800525, 828829204, 243264299, 351814543, 405243354, 909357725, 561463122, 913651722, 732754657, 430788419, 139670208, 938893256, 28061213, 673469112, 448961084, 80392418, 466684389, 201222617, 85583092, 76399490, 500763245, 519081041, 892915734, 75763854, 682289015]\n def numPrimeArrangements(self, n: int) -> int:\n return Solution.ret[n-1]\n```
1
4
[]
1
prime-arrangements
[Java] Leetcode 204 Sieve of Eratosthenes + Factorial Static DP Beats 100%
java-leetcode-204-sieve-of-eratosthenes-w0lop
\nclass Solution {\n public static final int MOD = (int)1e9 + 7;\n public static Map<Integer, Long> dp = new HashMap<>();\n \n public int numPrimeAr
blackspinner
NORMAL
2021-01-04T07:27:28.810984+00:00
2021-01-04T07:29:51.765511+00:00
146
false
```\nclass Solution {\n public static final int MOD = (int)1e9 + 7;\n public static Map<Integer, Long> dp = new HashMap<>();\n \n public int numPrimeArrangements(int n) {\n int cnt = countPrimes(n);\n return (int)(factorial(cnt) * factorial(n - cnt) % MOD);\n }\n \n // Leetcode 204: Sieve of Eratosthenes\n private int countPrimes(int n) {\n boolean[] prime = new boolean[n + 1];\n int cnt = 1;\n Arrays.fill(prime, true);\n for (int p = 2; p * p <= n; p++) {\n if (prime[p]) {\n for (int i = p * p; i <= n; i += p) {\n if (prime[i]) {\n prime[i] = false;\n cnt++;\n }\n }\n }\n }\n return n - cnt;\n }\n \n private long factorial(int n) {\n if (n < 2) {\n return 1;\n }\n if (dp.containsKey(n)) {\n return dp.get(n);\n }\n long res = (long)n * factorial(n - 1) % MOD;\n dp.put(n, res);\n return res;\n }\n}\n```
1
0
[]
0
prime-arrangements
C++ solution 100% faster
c-solution-100-faster-by-stamosromas-jyhq
\nclass Solution {\npublic:\n int tot_primes(int n){\n int i,j,tot=2;\n bool fu;\n for(i=4; i<=n; i++){\n fu=true;\n
stamosromas
NORMAL
2020-12-23T22:00:03.463721+00:00
2020-12-23T22:00:03.463766+00:00
141
false
```\nclass Solution {\npublic:\n int tot_primes(int n){\n int i,j,tot=2;\n bool fu;\n for(i=4; i<=n; i++){\n fu=true;\n for(j=2; j<=int(sqrt(i)); j++){\n if(i%j==0){\n fu=false;\n break;\n }\n\n }\n if(fu)\n tot++;\n }\n return tot;\n}\n int numPrimeArrangements(int n) {\n if(n<=2)\n return 1;\n else if(n==3)\n return 2;\n long long unsigned int p=1;\n int nu=tot_primes(n),i;\n for(i=2; i<=nu; i++){\n p*=i;\n if(p>int(pow(10,9)+7))\n p%=int(pow(10,9)+7);\n }\n for(i=2; i<=n-nu; i++){\n p*=i;\n if(p>int(pow(10,9)+7))\n p%=int(pow(10,9)+7);\n }\n return p;\n }\n};\n```
1
0
[]
0
prime-arrangements
Python-Sieve of Eratosthenes (24ms)
python-sieve-of-eratosthenes-24ms-by-sai-1e9q
```\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n def fact(n):\n if n==0 or n==1:\n return 1\n
saisathwik1999
NORMAL
2020-12-20T06:32:51.812925+00:00
2020-12-20T07:48:01.525223+00:00
120
false
```\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n def fact(n):\n if n==0 or n==1:\n return 1\n else:\n return n*fact(n-1)\n prime=[True]*(n+1)\n for i in range(2,math.ceil(math.sqrt(n))+1):\n if prime[i]:\n for j in range(i*i,n+1,i):\n prime[j]=False\n summ=sum(prime)-2\n return fact(n-summ)*fact(summ)%(10**9+7)\n \n
1
0
[]
0
prime-arrangements
C++ 100% faster code
c-100-faster-code-by-soundsoflife-163n
\nclass Solution {\n long MOD = 1e9 + 7;\npublic:\n int numPrimeArrangements(int n) {\n vector<int> prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31
soundsoflife
NORMAL
2020-09-17T16:52:38.942880+00:00
2020-09-17T16:52:38.942924+00:00
132
false
```\nclass Solution {\n long MOD = 1e9 + 7;\npublic:\n int numPrimeArrangements(int n) {\n vector<int> prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,\n 97};\n long long ans = 1;\n int k = distance(prime.begin(), upper_bound(prime.begin(), prime.end(), n));\n for (int i = 1; i <= k; ++i) {\n ans = ans * i % MOD;\n }\n for (int i = 1; i <= n - k; ++i) {\n ans = ans * i % MOD;\n }\n return ans;\n }\n};\n```
1
0
[]
0
prime-arrangements
java 5 ms solution
java-5-ms-solution-by-anwesh2-x7j9
Get the prime numbers between 1 to n. Then arrangement can be the product of factorial of prime numbers and non prime numbers.\n\nimport java.math.*;\nclass Sol
anwesh2
NORMAL
2020-08-30T13:36:57.285045+00:00
2020-08-30T13:36:57.285091+00:00
153
false
Get the prime numbers between 1 to n. Then arrangement can be the product of factorial of prime numbers and non prime numbers.\n```\nimport java.math.*;\nclass Solution {\n \n public BigInteger factorial(int k) {\n if (k == 0) {\n return BigInteger.ONE;\n }\n return BigInteger.valueOf(k).multiply(factorial(k - 1));\n }\n \n public BigInteger getModNumber(int n, int a) {\n BigDecimal k = BigDecimal.valueOf(10.0);\n k = k.pow(9).add(BigDecimal.valueOf(7.0));\n return k.toBigInteger();\n }\n public int numPrimeArrangements(int n) {\n int count = 0; \n if (n == 1) {\n return 1;\n }\n \n for (int i = 2; i <= n; i++) {\n int t = (int)Math.sqrt(i * 1.0);\n int k = 0; \n for (int j = 2; j <= t; j++) {\n if (i % j == 0) {\n k++;\n break;\n }\n \n }\n if (k == 0) {\n count++;\n }\n }\n \n BigInteger calc = factorial(count).multiply(factorial(n - count));\n return calc.mod(getModNumber(9, 7)).intValue();\n }\n}\n```
1
0
[]
0
prime-arrangements
C++ solution using sieve property faster than 100% time
c-solution-using-sieve-property-faster-t-3mda
\nclass Solution {\npublic:\n int hell=1000000007;\n long long fact(long long n)\n {\n if(n==0 || n==1) return 1;\n return ((n%hell)*(fac
IamDon
NORMAL
2020-07-10T08:26:17.069931+00:00
2020-07-10T08:26:17.069972+00:00
265
false
```\nclass Solution {\npublic:\n int hell=1000000007;\n long long fact(long long n)\n {\n if(n==0 || n==1) return 1;\n return ((n%hell)*(fact(n-1)%hell))%hell;\n }\n int numPrimeArrangements(int n) \n {\n vector<int> sieve(n+1, -1);\n sieve[1] = 1;\n for(int i=2; i*i<=n; i++)\n {\n for(int j=i+i; j<=n; j+=i)\n {\n sieve[j] = 1;\n }\n }\n long long prime=0, notPrime=0;\n for(int i=1; i<=n; i++)\n {\n (sieve[i]==-1)?prime++:notPrime++;\n }\n prime = fact(prime)%hell;\n notPrime = fact(notPrime)%hell;\n int ans=(prime*notPrime)%hell;\n return ans;\n }\n};\n```
1
0
['C', 'C++']
0
prime-arrangements
Java Single mod-BigInteger
java-single-mod-biginteger-by-bhisham199-58mh
import java.math.BigInteger;\nclass Solution {\n BigInteger mod=BigInteger.valueOf(1000000007);\n public int numPrimeArrangements(int n) {\n int pr
bhisham1999
NORMAL
2020-06-24T13:33:18.770444+00:00
2020-06-24T13:33:18.770480+00:00
143
false
import java.math.BigInteger;\nclass Solution {\n BigInteger mod=BigInteger.valueOf(1000000007);\n public int numPrimeArrangements(int n) {\n int prime=primeCount(n);\n return ((fact(prime).multiply(fact(n-prime))).mod(mod)).intValue();\n }\n public int primeCount(int n){\n int c=0;\n for(int i=2;i<=n;i++){\n if(i==2 || i==3 || i==5 || i==7 || i==11 || i==13 || i==17 || i==19 || i==23 || i== 29 || i==31 || i==37 || i== 41 || i==43 || i==47 || i==53 || i==59 || i==61 || i==67 || i==71 || i==73 || i==79 || i==83 || i==89 || i==97)\n c++;\n }\n return c;\n }\n public BigInteger fact(int n){\n BigInteger f=BigInteger.ONE;\n for(int i=1;i<=n;i++)\n f=f.multiply(BigInteger.valueOf(i));\n return f;\n }\n}
1
0
[]
0
prime-arrangements
Standard Java Solution
standard-java-solution-by-sriharik-l7nq
Theory \nWe need to find how many prime numbers there are from [1, N]. We also need to compute how many non-primes there are between [1, N], which can be derive
sriharik
NORMAL
2020-06-16T00:07:47.698851+00:00
2020-06-16T00:07:47.698882+00:00
120
false
### Theory \nWe need to find how many prime numbers there are from [1, N]. We also need to compute how many non-primes there are between [1, N], which can be derived from `n - numPrimes = nonPrimes`. Then our answer will be `factorial(nonPrimes) * factorial(numPrimes)`.\n\n### Solution\n\n```\n private static final int MOD = 1000000007;\n public int numPrimeArrangements(int n) {\n int primeCount = 0, nonPrimes = 0;\n for (int i = 2; i <= n; i++) if(isPrime(i)) primeCount++;\n nonPrimes = n - primeCount;\n return (int) ((factorial(primeCount) * factorial(nonPrimes)) % MOD); \n }\n \n private boolean isPrime(int n) { \n if (n <= 1) return false; \n for (int i = 2; i <= Math.sqrt(n); i++) if (n % i == 0) return false; \n return true; \n }\n \n private long factorial(int x) {\n long result = 1;\n for (int i = x; i > 0; i--) result = ((i * result) % MOD);\n return result;\n }\n```\n
1
0
[]
0
prime-arrangements
[Java] Simple sieve in O(n log log n)
java-simple-sieve-in-on-log-log-n-by-rok-ekrn
\nclass Solution {\n private static final long MOD = (long) 1e9 + 7;\n \n public int numPrimeArrangements(int n) {\n int primesCount = countNumb
roka
NORMAL
2020-05-30T19:23:54.617878+00:00
2020-05-30T19:23:54.617910+00:00
96
false
```\nclass Solution {\n private static final long MOD = (long) 1e9 + 7;\n \n public int numPrimeArrangements(int n) {\n int primesCount = countNumberOfPrimes(n);\n long result = factorial(primesCount) * factorial(n - primesCount) % MOD;\n \n return (int) result;\n }\n \n private int countNumberOfPrimes(int n) {\n int answer = 0;\n boolean[] isNotPrime = new boolean[n + 1];\n \n for (int i = 2; i <= n; i++) {\n if (isNotPrime[i])\n continue;\n \n answer++;\n for (int j = i * i; j <= n; j += i)\n isNotPrime[j] = true;\n }\n \n return answer;\n }\n \n private long factorial(int n) {\n long answer = 1;\n while (n > 1) {\n answer = (answer * n) % MOD;\n n--;\n }\n \n return answer;\n }\n}\n```
1
0
[]
1
prime-arrangements
javaScript
javascript-by-valaamm-1g6v
\nvar numPrimeArrangements = function(n) {\n let primes = 0;\n let all = new Array(n).fill(2);\n for (let i = 2; i <= n; i++) {\n if (all[i - 1]
valaamm
NORMAL
2020-05-21T20:11:19.693487+00:00
2020-05-21T20:35:43.266891+00:00
131
false
```\nvar numPrimeArrangements = function(n) {\n let primes = 0;\n let all = new Array(n).fill(2);\n for (let i = 2; i <= n; i++) {\n if (all[i - 1] === 2) {\n primes += 1;\n };\n for (let k = 2; k < n; k++) {\n if (!all[i * k - 1]) {\n break;\n };\n all[i * k - 1] = 1;\n };\n };\n all = all.length - primes;\n let result = 1;\n let modulo = 10 ** 9 + 7;\n for (let i = 1; i <= primes; i++) {\n result = (result * i) % modulo;\n };\n for (let i = 1; i <= all; i++) {\n result = (result * i) % modulo;\n };\n return result;\n};\n```
1
0
[]
0
prime-arrangements
JAVA | 100 % TIME AND SPACE | USING SIEVE |
java-100-time-and-space-using-sieve-by-a-1uby
\'\'\'\nclass Solution {\n \n static final int mod=1000000007;\n \n public int numPrimeArrangements(int n) {\n \n boolean p[]=new bool
avi-goud
NORMAL
2020-05-11T13:36:34.471575+00:00
2020-05-11T13:36:34.471624+00:00
112
false
\'\'\'\nclass Solution {\n \n static final int mod=1000000007;\n \n public int numPrimeArrangements(int n) {\n \n boolean p[]=new boolean[n+1];\n \n \n \n isprime(p,n+1);\n \n int x=2;\n int count=0;\n \n while(x<=n)\n {\n if(p[x])\n {count++;\n \n }\n x++;\n }\n \n return (int)((fact(count)*fact(n-count))%mod);\n \n \n }\n \n \n public long fact(int num){\n \n long res=1;\n \n for(int i=num;i>=1;i--)\n res=(res*i)%mod;\n \n return res;\n }\n \n \n public void isprime(boolean p[],int num){\n \n Arrays.fill(p,true);\n \n for(int i=2;i*i<num;i++)\n {\n if(p[i])\n {\n for(int j=i*i;j<num;j+=i)\n p[j]=false;\n }\n\n } \n }\n}\n\'\'\'
1
0
[]
0
prime-arrangements
C# faster than 100%, less than 100% Mem, O(n)
c-faster-than-100-less-than-100-mem-on-b-tld9
Runtime: 32 ms\nMemory Usage: 14.8 MB\n\n```\n public int NumPrimeArrangements(int n) {\n int primeCount = CountOfPrimes(n);\n int otherCount =
ve7545
NORMAL
2020-03-17T20:09:58.465288+00:00
2020-03-17T20:09:58.465346+00:00
182
false
Runtime: 32 ms\nMemory Usage: 14.8 MB\n\n```\n public int NumPrimeArrangements(int n) {\n int primeCount = CountOfPrimes(n);\n int otherCount = n - primeCount;\n \n long result = 1;\n int mod = (int)Math.Pow(10,9) + 7;\n \n while(otherCount > 0)\n {\n result *= otherCount;\n if (result > mod)\n { result = result % mod; }\n otherCount--;\n }\n \n while(primeCount > 0)\n {\n result *= primeCount;\n if (result > mod)\n { result = result % mod; }\n primeCount--;\n }\n \n return (int)result;\n }\n \n private int CountOfPrimes(int n)\n {\n int[] primes = new int[n+1];\n int count = 0;\n for(int i=2; i<= n; i++)\n {\n if (primes[i] != 0) { continue; }\n count++;\n \n for(int j=i; j<= n; j+=i)\n {\n primes[j] = -1;\n }\n }\n \n return count;\n }
1
1
[]
0
prime-arrangements
Python solution with explanation
python-solution-with-explanation-by-b0mb-kv30
There are N number of primes from 1 to n and there are exactly N number of prime indexes from index 1 to n. So the the number of permutations for prime numbers
b0mb4rdi3r
NORMAL
2020-03-17T10:55:40.971682+00:00
2020-03-17T10:55:40.971733+00:00
439
false
There are N number of primes from 1 to n and there are exactly N number of prime indexes from index 1 to n. So the the number of permutations for prime numbers are N! Similarly the number of permutations for nonprime numbers are (n-N)! so the total number of permutations are N!*(n-N)!\n\nFor example: from the given test case: [1,2,3,4,5]. {2, 3 and 5} are fixed on prime index slots, we can only swap them around. There are 3 x 2 x 1 = 3! ways to pick for each index slot. [[2,3,5], [2,5,3], [3,2,5], [3,5,2], [5,2,3], [5,3,2]], 2x1 for {1,4} [[1,4], [4,1]] so the total is 3!*2!\n```\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n CONST=int(math.pow(10,9))+7\n \n #By definition, a prime number is only divisiable by 1 and itself\n def isPrime(num):\n if num > 1:\n # check for factors\n for i in range(2,num):\n if (num % i) == 0:\n return False\n break\n else:\n return True\n else:\n return False\n \n #Count the number of prime and non prime from 1 to n(inclusive)\n prime=0\n for num in range(1,n+1):\n if isPrime(num): prime+=1\n \n \n #Calculate factorial of n using dp\n def fact(n):\n F=[-1]*101\n F[0]=1\n F[1]=1\n F[2]=2\n if n>2:\n F[n]=fact(n-1)*n\n return F[n]\n \n return fact(prime)*fact(n-prime)%CONST\n```
1
0
[]
1
prime-arrangements
Python Intuitive 100%
python-intuitive-100-by-deepak_goswami-9yc3
```python\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n def Fact(n):\n if n==1:\n return 1\n
deepak_goswami
NORMAL
2020-02-25T14:18:53.483129+00:00
2020-02-25T14:18:53.483161+00:00
232
false
```python\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n def Fact(n):\n if n==1:\n return 1\n else:\n return n*Fact(n-1)\n def isprime(n):\n for i in range(2,int(math.sqrt(n))+1):\n if n%i==0:\n return False\n return True\n def Prime(n):\n count=0\n for i in range(2,n+1):\n if isprime(i):\n count+=1\n return count\n if n<3:\n return 1\n a = Prime(n)\n b = n - a\n return (Fact(a)*Fact(b)%1000000007+1000000007)%1000000007
1
0
[]
0
prime-arrangements
Python O( n*sqrt(n) ) sol. by prime judgement. 90%+ [ With explanation ]
python-o-nsqrtn-sol-by-prime-judgement-9-ff0b
Python O( n * sqrt(n) ) sol. by prime judgement.\n\n---\n\nHint:\nBuild a helper function to judge whether a number k is a prime or not.\n\n---\n\nNote:\nRememb
brianchiang_tw
NORMAL
2020-02-15T07:18:41.373315+00:00
2020-02-15T07:18:51.201611+00:00
310
false
Python O( n * sqrt(n) ) sol. by prime judgement.\n\n---\n\nHint:\nBuild a helper function to judge whether a number k is a prime or not.\n\n---\n\nNote:\nRemember to take **modulo** (**10^9 + 7**), which is [defined by description](https://leetcode.com/problems/prime-arrangements/).\n\n---\n\n```\nfrom math import factorial\nfrom math import sqrt\nclass Solution:\n \n def judge_prime( self, k:int) -> bool:\n \'\'\'\n Input: positive integer\n Output: True for prime number. Otherwise, retunr False\n \'\'\'\n \n if k == 1:\n return False\n \n if k == 2:\n return True\n \n for i in range(2, int( sqrt(k)+1 ) ):\n \n if k % i == 0:\n return False\n \n return True\n \n \n \n def numPrimeArrangements(self, n: int) -> int:\n \'\'\'\n Input: n\n Output: Prime arrangements for n\n \'\'\'\n \n \n # modulo constant defined by description\n mod_constant = int(1e9)+7\n \n counter_of_prime = 0\n \n # check positive integer from 1 to n\n for i in range(1,n+1):\n \n if self.judge_prime(i):\n counter_of_prime += 1\n \n # Let q as counter_of_prime\n # then all permutation\n # = ( q! * ( n-q)! ) mod (10^9 + 7)\n all_permutation = ( factorial(counter_of_prime) * factorial(n-counter_of_prime) ) % mod_constant\n \n return all_permutation\n```\n\n---\n\nReference:\n\n[1] [Wiki: Prime Number](https://en.wikipedia.org/wiki/Prime_number)\n\n[2] [Python official docs about math.sqrt()](https://docs.python.org/3/library/math.html#math.sqrt)\n\n[3] [Python official docs about math.factorial()](https://docs.python.org/3/library/math.html#math.factorial)
1
0
['Math', 'Iterator', 'Python']
0
prime-arrangements
Java solution beats 100% time & space
java-solution-beats-100-time-space-by-ak-d3tu
Example n = 5\n1 2 3 4 5\n\n1 & 4 is not prime number => number of choice is 2 * 1;\n2 & 3 & 5 is prime number => number of choice is 3 * 2 * 1\n\nBased
akiramonster
NORMAL
2020-02-01T21:04:37.198451+00:00
2020-02-01T21:06:42.345894+00:00
106
false
Example n = 5\n1 2 3 4 5\n\n1 & 4 is not prime number => number of choice is 2 * 1;\n2 & 3 & 5 is prime number => number of choice is 3 * 2 * 1\n\nBased on this intuition, I count number of prime number from [1, n]. Number of non-prime is n-nbrPrime.\nThe result is factorial(nbrPrime) * factorial(nonPrime)\n\n\n```\nclass Solution {\n\tprivate final int mod = 1000000007;\n\n\tpublic int numPrimeArrangements(int n) {\n\t\tint nbrPrime = 0;\n\t\tfor (int i = 1; i <= n; i++) {\n\t\t\tif (isPrime(i)) nbrPrime++;\n\t\t}\n\t\tint nonPrime = n - nbrPrime;\n\t\tlong res = 1;\n\t\tfor (int i = 2; i <= Math.max(nbrPrime, nonPrime); i++) {\n\t\t\tif (i <= Math.min(nbrPrime, nonPrime)) {\n\t\t\t\tres = (res * i * i) % mod;\n\t\t\t} else {\n\t\t\t\tres = (res * i) % mod;\n\t\t\t}\n\t\t}\n\t\treturn (int) res;\n\t}\n\n\tpublic boolean isPrime(int n) {\n\t\tif (n < 2) return n == 2;\n\t\tfor (int i = 2; i <= n / 2; i++) {\n\t\t\tif (n % i == 0) return false;\n\t\t}\n\t\treturn true;\n\t}\n}\n```
1
0
[]
0
prime-arrangements
1st week learning Python. Write things in the easiest way.
1st-week-learning-python-write-things-in-z4bk
\nclass Solution(object):\n def numPrimeArrangements(self, n):\n """\n :type n: int\n :rtype: int\n """\n #Step 1. List al
sophiesu0827
NORMAL
2020-01-15T22:46:42.633810+00:00
2020-01-15T22:58:04.060011+00:00
226
false
```\nclass Solution(object):\n def numPrimeArrangements(self, n):\n """\n :type n: int\n :rtype: int\n """\n #Step 1. List all non-prime numbers < n\n nonprime=[1]\n if n>1:\n for i in range(2,n+1):\n for j in range(2,i):\n if i%j == 0:\n nonprime.append(i)\n break\n i=i+1\n \n #Step 2. Take the complement of the non-prime numbers to get a list of prime numbers\n prime=[p for p in range(1,n+1) if p not in nonprime]\n \n #Step 3. Define a factorial function\n def fact(c):\n fact=1\n for i in range(1,c+1):\n fact *= i\n return fact\n \n #Step 4.The final result should be equal to prime_count!*non_prime_count! Don\'t forget to use modulo because numbers can get explosive.\n return fact(len(prime))*fact(n-len(prime)) % (10**9+7)\n```
1
1
[]
0
prime-arrangements
My C solution
my-c-solution-by-yingjie_song-543n
\nint numPrimeArrangements(int n){\n int prime[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};\n i
yingjie_song
NORMAL
2020-01-06T22:28:54.459530+00:00
2020-01-06T22:28:54.459580+00:00
110
false
```\nint numPrimeArrangements(int n){\n int prime[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};\n int i=0;\n for(; i<25; i++){\n if(prime[i] > n) break;\n }\n int np = i; // # of prime numbers\n int nq = n-i; // # of non-prime numbers\n \n // calculate factorials, but I don\'t want to exceed the limit of int\n int res = 1;\n int tmp;\n for(int j=2; j<=np; j++){\n tmp = res;\n for(int k=0; k<j-1; k++){ // add res to itself j-1 times, which is the same as multiply by j\n res = (res + tmp) % 1000000007;\n }\n }\n for(int j=2; j<=nq; j++){\n tmp = res;\n for(int k=0; k<j-1; k++){\n res = (res + tmp) % 1000000007;\n }\n }\n return res; // res = 1*2*...*np*2*...*nq\n}\n```
1
0
[]
0
prime-arrangements
Python simple and fast solution
python-simple-and-fast-solution-by-jingh-ks7n
\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n return (math.factorial(self.count_primes(n)) * math.factorial(n - self.count_prim
jinghuayao
NORMAL
2019-11-10T01:22:18.015378+00:00
2019-11-10T01:22:18.015429+00:00
138
false
```\nclass Solution:\n def numPrimeArrangements(self, n: int) -> int:\n return (math.factorial(self.count_primes(n)) * math.factorial(n - self.count_primes(n))) % (10**9 + 7)\n \n def prime(self, num):\n if num == 1:\n return False\n for i in range(2, int(math.sqrt(num)) + 1):\n if num % i == 0:\n return False\n return True\n \n def count_primes(self, n):\n res = 0\n for i in range(1, n+1):\n if self.prime(i):\n res += 1\n return res\n```\n
1
0
[]
0
prime-arrangements
Functional Programming with Scala
functional-programming-with-scala-by-zst-vti8
\nobject Solution {\n lazy val primes: Stream[Int] = 2 #:: Stream.from(3)\n .filter(x => {\n primes.takeWhile(_ <= math.sqrt(x)).exists(x%_
zsterrylau
NORMAL
2019-11-03T00:32:28.829914+00:00
2019-11-03T00:32:28.829967+00:00
129
false
```\nobject Solution {\n lazy val primes: Stream[Int] = 2 #:: Stream.from(3)\n .filter(x => {\n primes.takeWhile(_ <= math.sqrt(x)).exists(x%_ == 0) == false\n })\n\n val MOD = math.pow(10,9).toInt+7\n \n lazy val factorials: Stream[BigInt] = 1 #:: factorials.zip(Stream.from(1))\n .map(x => x._1 * x._2)\n \n def numPrimeArrangements(n: Int): Int = {\n val primeCount = primes.takeWhile(_ <= n).size\n \n ((factorials(n-primeCount) * factorials(primeCount)) % MOD).toInt\n }\n}\n```
1
0
[]
1
prime-arrangements
Python 3 + explanation. Faster than 79.7%, less memory than 100%
python-3-explanation-faster-than-797-les-zsel
This problem can be restated as counting the number of ways we can permute the prime numbers and the non prime numbers.\nThere are n! ways to permute n objects.
aragil
NORMAL
2019-10-07T05:56:26.209042+00:00
2019-10-07T05:56:26.209095+00:00
221
false
This problem can be restated as counting the number of ways we can permute the prime numbers and the non prime numbers.\nThere are n! ways to permute n objects. So we will have prime_numbers! ways to permute the prime numbers and non_prime! ways\nto permute the non_prime numbers and then we multiply those two to find the total. The problem is reduced to finding the number\nof the prime numbers between 1 to n.\n\n```\nclass Solution:\n def prime_numbers(self, n):\n primes = 0\n is_prime = True\n for number in range(2, n+1):\n for i in range(2, number):\n if number % i == 0:\n is_prime = False\n break\n if is_prime:\n primes += 1\n is_prime = True\n return primes\n\n def factorial(self, n):\n from functools import reduce\n from operator import mul\n return reduce(mul, range(1,n+1), 1)\n \n def numPrimeArrangements(self, n: int) -> int:\n prime_count = self.prime_numbers(n)\n non_prime_count = n - prime_count\n return (self.factorial(prime_count) * self.factorial(non_prime_count)) % (10**9 + 7)\n```
1
0
[]
0
prime-arrangements
Java beat 100%, worst problem so far
java-beat-100-worst-problem-so-far-by-da-a14x
\nclass Solution {\n static int MOD=(int)1e9+7;\n static int[] PRIMES=new int[] {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};\
DaviLu
NORMAL
2019-09-07T15:56:12.873536+00:00
2019-09-07T15:56:12.873569+00:00
223
false
```\nclass Solution {\n static int MOD=(int)1e9+7;\n static int[] PRIMES=new int[] {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};\n public int numPrimeArrangements(int n) {\n int count=0;\n for(int prime:PRIMES) {\n if(prime<=n) {\n count++;\n } else {\n break;\n }\n }\n return (int) ((factorial(count)*factorial(n-count))%MOD);\n }\n \n long factorial(int k) {\n long factorial=1;\n for(int i=1;i<=k;i++) {\n factorial*=i;\n factorial%=MOD;\n }\n return factorial;\n }\n}\n```
1
1
[]
0
prime-arrangements
Java Easy
java-easy-by-miaoz-vc25
\nclass Solution \n{\n public int numPrimeArrangements(int n) \n {\n int c_prime = prime_count(n);\n \n long r = 1;\n for (int
miaoz
NORMAL
2019-09-06T01:52:28.831146+00:00
2019-09-06T01:52:28.831229+00:00
212
false
```\nclass Solution \n{\n public int numPrimeArrangements(int n) \n {\n int c_prime = prime_count(n);\n \n long r = 1;\n for (int i = 2; i <= c_prime; ++i)\n {\n r = r * i;\n r %= 1000000007;\n }\n for (int i = 2; i <= n - c_prime; ++i)\n {\n r = r * i;\n r %= 1000000007;\n }\n \n return (int) r;\n }\n \n // find how many prime numbers in [1, n]\n int prime_count(int n)\n {\n if (n < 2) return 0;\n int count = 1;\n for (int i = 3; i <= n; ++i)\n {\n boolean is_prime = true;\n for (int j = 2; j * j <= i; ++j)\n {\n if (i % j == 0)\n {\n is_prime = false;\n break;\n }\n }\n if (is_prime) ++count;\n }\n \n return count;\n }\n}\n```
1
0
[]
0
prime-arrangements
Java Solution ( PrimeCount ! ) * (NonPrimeCount !)
java-solution-primecount-nonprimecount-b-jg8l
\nclass Solution {\n public int numPrimeArrangements(int n) {\n if(n <= 2) {\n return 1;\n }\n int count = countPrime(n);\n
yrq
NORMAL
2019-09-05T20:00:45.338176+00:00
2019-09-05T20:00:45.338211+00:00
201
false
```\nclass Solution {\n public int numPrimeArrangements(int n) {\n if(n <= 2) {\n return 1;\n }\n int count = countPrime(n);\n long sum = 1;\n for(int i = 2; i <= count; i++) {\n sum = (sum * i) % 1000000007;\n }\n for(int i = n - count; i >= 2; i--) {\n sum = (sum * i) % 1000000007;\n } \n return (int)(sum % 1000000007);\n }\n\n private int countPrime(int n) {\n int count = 0;\n boolean[] v = new boolean[n + 1]; \n for(int i = 2; i * i <= n; i++) {\n for(int j = 2; j * i <= n; j++) {\n v[i * j] = true;\n }\n }\n for(int i = 2; i <= n; i++) {\n if(!v[i] && isPrime(i)) {\n count++;\n }\n }\n return count;\n }\n \n private boolean isPrime(int val) {\n for(int i = 2; i * i <= val; i++) {\n if(val % i == 0) {\n return false;\n }\n }\n return true;\n }\n}\n```
1
0
[]
0
prime-arrangements
[JavaScript] 48ms with 2 lines for function code
javascript-48ms-with-2-lines-for-functio-a8er
3 lines to declare data\n- 2 lines for functions\n\njs\nconst MOD_BASE = 10**9 + 7;\nconst primeCount = [0,0,1,2,2,3,3,4,4,4,4,5,5,6,6,6,6,7,7,8,8,8,8,9,9,9,9,9
poppinlp
NORMAL
2019-09-04T10:36:54.087171+00:00
2019-09-04T10:36:54.087203+00:00
292
false
- 3 lines to declare data\n- 2 lines for functions\n\n```js\nconst MOD_BASE = 10**9 + 7;\nconst primeCount = [0,0,1,2,2,3,3,4,4,4,4,5,5,6,6,6,6,7,7,8,8,8,8,9,9,9,9,9,9,10,10,11,11,11,11,11,11,12,12,12,12,13,13,14,14,14,14,15,15,15,15,15,15,16,16,16,16,16,16,17,17,18,18,18,18,18,18,19,19,19,19,20,20,21,21,21,21,21,21,22,22,22,22,23,23,23,23,23,23,24,24,24,24,24,24,24,24,25,25,25,25];\nconst permutationCache = [1, 1];\nconst helper = n => (permutationCache[n] === undefined && (permutationCache[n] = (n * helper(n - 1)) % MOD_BASE), permutationCache[n]);\nconst numPrimeArrangements = n => Number((BigInt(helper(primeCount[n])) * BigInt(helper(n - primeCount[n]))) % BigInt(MOD_BASE));\n```
1
1
['JavaScript']
0
prime-arrangements
[Java] Counting solution 0ms
java-counting-solution-0ms-by-pnimit-vkeg
\nclass Solution {\n public int numPrimeArrangements(int n) {\n int [] primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,9
pnimit
NORMAL
2019-09-04T04:12:43.576083+00:00
2019-09-04T04:13:31.843706+00:00
380
false
```\nclass Solution {\n public int numPrimeArrangements(int n) {\n int [] primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};\n int primeCount = n >= 97 ? primes.length : 0;\n \n for(int i = 0; i < primes.length; ++i){\n if(primes[i] >= n){\n primeCount = primes[i] > n ? i : i + 1;\n break;\n }\n }\n \n return (int) (fact(primeCount) * fact(n - primeCount) % 1000000007);\n }\n \n public long fact(int num){\n if(num <= 1) return 1;\n return (num * fact(num - 1)) % 1000000007;\n }\n}\n\n```
1
0
['Java']
0
prime-arrangements
Easy to understand C# Solution with comments
easy-to-understand-c-solution-with-comme-ftng
\tpublic int CountPrimes(int n)\n {\n // using Sieve Of Eratosthenes\n\n int counter = 0;\n bool[] primes = new bool[n+1
khaled_acmilan
NORMAL
2019-09-02T17:41:03.187524+00:00
2019-09-02T17:41:03.187559+00:00
154
false
\tpublic int CountPrimes(int n)\n {\n // using Sieve Of Eratosthenes\n\n int counter = 0;\n bool[] primes = new bool[n+1];\n\n primes = Enumerable.Repeat(true, n+1).ToArray();\n \n for (int i = 2; i * i <= primes.Length; i++)\n {\n for (int j = i * i; j <= n; j+= i)\n {\n primes[j] = false;\n }\n }\n\n for (int i = 2; i < primes.Length; i++)\n {\n if (primes[i])\n counter++;\n }\n \n return counter;\n }\n\n readonly int MOD = (int)Math.Pow(10, 9) + 7;\n\n public int NumPrimeArrangements(int n)\n { \n int noOfprimes = CountPrimes(n);\n decimal factorialPrimes = Factorial(noOfprimes);\n decimal factoralNoPrimes = Factorial(n - noOfprimes);\n\n // prime numbers are allowed to only shuffle in the prime positions\n\n\n // Non - prime numbers are allowed to only shuffle in the non-prime positions\n\n\n decimal product = (factorialPrimes * factoralNoPrimes) % MOD;\n\n return (int)product;\n }\n\n public decimal Factorial(int n)\n {\n if (n <= 1) return 1;\n return n * Factorial(n - 1) % MOD;\n }
1
0
[]
0
prime-arrangements
[python] *understanding the question* with solution and comments
python-understanding-the-question-with-s-33af
Original Question:\nReturn the number of permutations of 1 to n so that prime numbers are at prime indices (1-indexed.)\n\n(Recall that an integer is prime if a
mcclay
NORMAL
2019-09-02T00:51:09.474378+00:00
2019-09-02T02:34:08.734918+00:00
100
false
**Original Question:**\nReturn the number of permutations of 1 to n so that prime numbers are at prime indices (1-indexed.)\n\n(Recall that an integer is prime if and only if it is greater than 1, and cannot be written as a product of two positive integers both smaller than it.)\n\nSince the answer may be large, return the answer modulo 10^9 + 7.\n\n**More Descriptive Example:**\nreturn the (factorial of length n_prime) multiplyed by (factorial of length n_non_prime) mod (10^9 + 7)\n\nexample:\nn = 5\nn_array = [1,2,3,4,5]\nn_non_prime = [1,4]\nlength_n_non_prime = 2 \nn_prime = [2,3,5]\nlength_n_prime = 3 \n3! * 2! mod (10^9 + 7)\n\n**My Solution:**\n```python\n# Runtime: 40 ms, faster than 75.00%\n# Memory Usage: 13.5 MB, less than 100.00%\nfrom functools import reduce\nfrom operator import mul\n\nclass Solution:\n def numPrimeArrangements(self, n):\n if n == 1:\n return 1\n\t\t\t\n\t\t# the list of primes from 1 to 100\n prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n\t\t\n\t\t# the number of primes to n\n number_primes = len(list(filter(lambda x: x <= n, prime)))\n \n\t\t# factorial of prime\n\t\tfact_prime = reduce(mul, list(range(1,number_primes + 1)))\n\t\t\n\t\t# factorial of non prime \n fact_non = reduce(mul, list(range(1, 1 + n - number_primes)))\n\t\t\n return fact_prime * fact_non % (10 ** 9 + 7)\n```
1
0
[]
0
prime-arrangements
[C++](Beats 100%)Simple solution with comments
cbeats-100simple-solution-with-comments-ijkty
We count the total number of primes till n. These are the positions where only primes can be permuted. But we also need to permute the remaining items to get th
dev1988
NORMAL
2019-09-01T17:07:47.663858+00:00
2019-09-01T17:07:47.663903+00:00
226
false
We count the total number of primes till n. These are the positions where only primes can be permuted. But we also need to permute the remaining items to get the final result.\n\nWe calculate factorial of all required, instead of calculating it twice.\n\nCode:\n```\nclass Solution {\n #define MOD 1000000007\npublic:\n int numPrimeArrangements(int n) {\n int count = countPrimes(n+1);\n int res;\n vector<long int> a(101, 0);\n fact(max(count, n-count), a);\n\t\t//result is product of count! * remaining!\n res = (a[count]*a[n-count])%MOD;\n return res;\n }\n //Helper method to calculate factorials\n void fact(int n, vector<long int> &a){\n a[0] = 1;\n for(int i = 1; i <= n; i++){\n a[i] = (i*a[i-1])%MOD;\n } \n }\n //Find total prime numbers count < n\n int countPrimes(int n) {\n vector<bool> prime(n, true);\n \n for(int i = 2; i*i < n; i++){\n if(!prime[i]) continue;\n for(int j = i*i; j < n; j += i){\n prime[j] = false;\n }\n }\n \n int count = 0;\n for(int i = 2; i < n; i++){\n if(prime[i]) count++;\n }\n return count;\n }\n};\n```
1
0
['C']
0
prime-arrangements
[Python/Java] count primes
pythonjava-count-primes-by-luolingwei-h7ka
\'\'\'\nclass Solution:\n\n\t# Python 36 ms\n def numPrimeArrangements(self, n):\n def countprime(n):\n A=[1](n+1)\n A[0],A[1]=0
luolingwei
NORMAL
2019-09-01T13:52:37.589649+00:00
2019-09-01T13:52:37.589684+00:00
113
false
\'\'\'\nclass Solution:\n\n\t# Python 36 ms\n def numPrimeArrangements(self, n):\n def countprime(n):\n A=[1]*(n+1)\n A[0],A[1]=0,0\n for i in range(2,int(n**0.5)+1):\n if A[i]:\n A[i*i:n+1:i]=[0]*len(A[i*i:n+1:i])\n return sum(A)\n primes=countprime(n)\n return (math.factorial(primes)*math.factorial(n-primes))%(10**9+7)\n\t\t\n\t# Java 0 ms\n\tpublic int numPrimeArrangements(int n) {\n int primes=countprimes(n);\n long res=1;\n for (int j=1;j<=primes;j++)\n res=(res*j)%1000000007;\n for (int j=1;j<=n-primes;j++)\n res=(res*j)%1000000007;\n return (int)res;\n }\n\n public int countprimes(int n) {\n int [] A=new int[n+1];\n int primes=0;\n Arrays.fill(A,1);\n A[0]=0;A[1]=0;\n for (int i=2;i*i<=n;i++){\n if (A[i]==1){\n for (int j=i*i;j<=n;j+=i){\n A[j]=0;\n }\n }\n }\n for (int i=0;i<=n;i++){\n primes+=A[i];\n }\n return primes;\n }\n\'\'\'
1
0
[]
0
rle-iterator
Java Straightforward Solution, O(n) time, O(1) space
java-straightforward-solution-on-time-o1-qduf
\nclass RLEIterator {\n int index;\n int [] A;\n public RLEIterator(int[] A) {\n this.A = A;\n index = 0;\n }\n \n public int ne
mrhhsmr
NORMAL
2018-09-09T03:24:03.489496+00:00
2018-10-01T05:24:36.888393+00:00
16,544
false
```\nclass RLEIterator {\n int index;\n int [] A;\n public RLEIterator(int[] A) {\n this.A = A;\n index = 0;\n }\n \n public int next(int n) {\n while(index < A.length && n > A[index]){\n n = n - A[index];\n index += 2;\n }\n \n if(index >= A.length){\n return -1;\n }\n \n A[index] = A[index] - n;\n return A[index + 1];\n }\n}\n```\nNote: input array doesn\'t count as extra space. I would say this problem is a O(n) space problem, but the solution I use is only O(1) space, since I didn\'t allocate any extra space.
115
6
[]
14
rle-iterator
Three solutions you would want to discuss in an interview (O(n), O(logn) and O(1) lookup)
three-solutions-you-would-want-to-discus-cgem
First is straight forward. O(n) space O(1) for init and O(n) next (O(1) amortized).\n\n\n\tclass RLEIterator {\n\n\t\tint[] A;\n\t\tint curA = 0;\n\t\tpublic RL
a1m
NORMAL
2018-12-13T07:08:49.561923+00:00
2018-12-13T07:08:49.561988+00:00
5,105
false
First is straight forward. `O(n)` space `O(1)` for init and `O(n)` next (`O(1)` amortized).\n\n\n\tclass RLEIterator {\n\n\t\tint[] A;\n\t\tint curA = 0;\n\t\tpublic RLEIterator(int[] A) {\n\t\t\tthis.A = A;\n\t\t}\n\n\t\tpublic int next(int n) {\n\t\t\tint num=0;\n\t\t\twhile(n > 0) {\n\t\t\t\tif (curA >= A.length) return -1;\n\t\t\t\tn = n - A[curA];\n\t\t\t\tif (n <= 0) {\n\t\t\t\t\tnum = A[curA+1];\n\t\t\t\t\tA[curA] = -n;\n\t\t\t\t} else {\n\t\t\t\t\tcurA += 2;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn num;\n\t\t}\n\t}\n\nSecond is a binary search. `O(n)` space `O(n)` init and `O(log n)` next.\n\n\tclass RLEIterator {\n\n\t\tlong[] count;\n\t\tint[] num;\n\n\t\tlong cur = 0; \n\t\tint low = 0;\n\t\tpublic RLEIterator(int[] A) {\n\t\t\tcount = new long[A.length/2];\n\t\t\tnum = new int[A.length/2];\n\t\t\tfor (int i=0; i<A.length; i+=2) {\n\t\t\t\tcount[i/2] = A[i] + (i>1? count[i/2 - 1] : 0);\n\t\t\t\tnum[i/2] = A[i+1];\n\t\t\t}\n\t\t}\n\n\t\tpublic int next(int n) {\n\t\t\tif (count.length==0) return -1;\n\t\t\tcur += n;\n\t\t\tif (cur > count[count.length-1]) return -1;\n\t\t\tint l=low, h=count.length-1;\n\n\t\t\twhile(l < h) {\n\t\t\t\tint mid = l + (h-l)/2;\n\t\t\t\tif (count[mid] >= cur) {\n\t\t\t\t\th = mid;\n\t\t\t\t} else {\n\t\t\t\t\tl = mid+1;\n\t\t\t\t}\n\t\t\t}\n\t\t\tlow = l;\n\t\t\treturn num[l];\n\t\t}\n\t}\n\nThe third option is to write out the actual array. And then use a pointer to directly index the array to see which number is in the specific field. Init runs in `O(k)` space and time, where `k` is the size of the unwrapped array. As `k` is very big in Leetcodes test cases this solution is not accepted! However, next then runs in `O(1)` which might be great for specific use cases!\n\nAll in all the binary search solution should be preferable in many cases.
76
6
[]
6
rle-iterator
[Java/Python 3] straightforward code with comment -- O(n) time and O(1) extra space
javapython-3-straightforward-code-with-c-lss3
java\n private int idx = 0;\n private int[] A;\n\t\t\n public RLEIterator(int[] A) { this.A = A; } // Injected A[]\n \n public int next(int n) {\
rock
NORMAL
2018-09-09T03:19:33.441970+00:00
2020-07-02T15:10:59.071195+00:00
6,842
false
```java\n private int idx = 0;\n private int[] A;\n\t\t\n public RLEIterator(int[] A) { this.A = A; } // Injected A[]\n \n public int next(int n) {\n while (idx < A.length && n > A[idx]) { // exhaust as many terms as possible.\n n -= A[idx]; // exhaust A[idx + 1] for A[idx] times. \n idx += 2; // move to next term.\n }\n if (idx < A.length) { // not exhaust all terms.\n A[idx] -= n;\n return A[idx + 1];\n }\n return -1; // exhaust all terms but still not enough.\n }\n```\nSimilar but clear logic version - credit to **@chenzuojing**\n```java\n private int[] A;\n private int index;\n \n public RLEIterator0(int[] A) {\n this.A = A; // Injected A[]\n }\n \n public int next(int n) {\n while (index < A.length) { // all elements exhausted?\n if (n <= A[index]) { // find the corresponding value.\n A[index] -= n; // deduct n from A[index].\n return A[index + 1]; // A[index + 1] is the nth value.\n }\n n -= A[index]; // not find the value yet, deduct A[index] from n.\n index += 2; // next group of same values.\n }\n return -1; // not enough values remaining.\n }\n```\n```python\n\n def __init__(self, A: List[int]):\n self.A = A\n self.index = 0\n \n def next(self, n: int) -> int:\n while self.index < len(self.A):\n if n <= self.A[self.index]:\n self.A[self.index] -= n\n return self.A[self.index + 1]\n n -= self.A[self.index]\n self.index += 2\n return -1\n```
43
4
[]
6
rle-iterator
Simple C++ solution
simple-c-solution-by-caspar-chen-hku-5mxi
\nclass RLEIterator {\npublic:\n \n int curInd;\n vector<int> seq;\n \n RLEIterator(vector<int>& A) {\n seq = A;\n curInd = 0;\n
caspar-chen-hku
NORMAL
2020-05-22T09:13:00.549954+00:00
2020-05-22T09:13:00.550005+00:00
3,062
false
```\nclass RLEIterator {\npublic:\n \n int curInd;\n vector<int> seq;\n \n RLEIterator(vector<int>& A) {\n seq = A;\n curInd = 0;\n }\n \n int next(int n) {\n while (curInd < seq.size()){\n if (seq[curInd]>=n){\n seq[curInd]-=n;\n return seq[curInd+1];\n }else{\n n -= seq[curInd];\n curInd += 2;\n }\n }\n return -1;\n }\n};\n```
34
1
[]
3
rle-iterator
Simple Python Pointer Solution
simple-python-pointer-solution-by-zcc230-j9ud
\nclass RLEIterator:\n\n def __init__(self, A: List[int]):\n \n self.values = A\n self.i = 0\n\n def next(self, n: int) -> int:\n\n
zcc230
NORMAL
2019-06-25T18:40:04.532977+00:00
2019-06-25T18:40:04.533051+00:00
2,504
false
```\nclass RLEIterator:\n\n def __init__(self, A: List[int]):\n \n self.values = A\n self.i = 0\n\n def next(self, n: int) -> int:\n\n while n>0 and self.i<len(self.values):\n if n>self.values[self.i]:\n n -= self.values[self.i]\n self.i+=2\n else:\n self.values[self.i] -= n\n return self.values[self.i+1]\n return -1\n\n```\n
23
0
[]
2
rle-iterator
Python Binary Search
python-binary-search-by-ggplay-383u
\nclass RLEIterator:\n\n \n # 5:33pm 1/19/2021 \n # 5:43pm 80% \n # sounds similar to log rate limiter and 528 random pick with weight\n
ggplay
NORMAL
2021-01-19T23:46:25.011176+00:00
2021-01-19T23:46:44.333466+00:00
1,260
false
```\nclass RLEIterator:\n\n \n # 5:33pm 1/19/2021 \n # 5:43pm 80% \n # sounds similar to log rate limiter and 528 random pick with weight\n # we need to record current n\n def __init__(self, A: List[int]):\n self.ind = []\n self.vals = []\n self.cur_n = 0\n \n i = 0\n cur_acc = 0\n \n while i < len(A):\n if A[i] != 0:\n cur_acc += A[i]\n self.ind.append(cur_acc)\n self.vals.append(A[i+1])\n i += 2\n \n\n def next(self, n: int) -> int:\n self.cur_n += n\n \n if not self.ind:\n return -1\n \n ind = bisect.bisect_left(self.ind, self.cur_n)\n \n if ind == len(self.ind):\n return -1\n \n return self.vals[ind]\n\n\n# Your RLEIterator object will be instantiated and called as such:\n# obj = RLEIterator(A)\n# param_1 = obj.next(n)\n```
14
0
[]
2
rle-iterator
Super Easy Python Solution
super-easy-python-solution-by-reverse_li-a17i
\nfrom itertools import accumulate\nimport bisect \n\nclass RLEIterator:\n\n def __init__(self, A: List[int]):\n self.count = A[::2]\n self.val
reverse_link_list
NORMAL
2019-07-21T06:02:17.461925+00:00
2019-07-21T06:02:17.461956+00:00
916
false
```\nfrom itertools import accumulate\nimport bisect \n\nclass RLEIterator:\n\n def __init__(self, A: List[int]):\n self.count = A[::2]\n self.value = A[1::2]\n self.start = -1\n self.acc = list(accumulate(self.count))\n \n\n def next(self, n: int) -> int:\n self.start += n\n index = bisect.bisect(self.acc, self.start)\n if index >= len(self.count):\n return -1\n return self.value[index]\n```
11
1
[]
0
rle-iterator
C++ Super Simple and Short Solution
c-super-simple-and-short-solution-by-yeh-1kru
\nclass RLEIterator {\npublic:\n RLEIterator(vector<int>& encoding) {\n rle = encoding;\n size = encoding.size();\n }\n \n int next(in
yehudisk
NORMAL
2021-07-14T15:09:58.035908+00:00
2021-07-14T15:09:58.035954+00:00
1,055
false
```\nclass RLEIterator {\npublic:\n RLEIterator(vector<int>& encoding) {\n rle = encoding;\n size = encoding.size();\n }\n \n int next(int n) {\n while (idx < size) {\n \n if (n <= rle[idx]) {\n rle[idx] -= n;\n return rle[idx+1];\n }\n \n else {\n n -= rle[idx];\n idx += 2;\n }\n }\n \n return -1;\n }\n \nprivate:\n vector<int> rle;\n int idx = 0,size;\n};\n```\n**Like it? please upvote!**
9
0
['C']
0
rle-iterator
Python3 recursive with comments. 32ms 100%
python3-recursive-with-comments-32ms-100-yy9k
```\nclass RLEIterator:\n\n def init(self, A: List[int]):\n self.A = A\n self.current = 0\n\n def next(self, n: int) -> int:\n # If w
dkbnz
NORMAL
2020-03-19T17:13:10.775593+00:00
2020-03-19T17:13:10.775632+00:00
541
false
```\nclass RLEIterator:\n\n def __init__(self, A: List[int]):\n self.A = A\n self.current = 0\n\n def next(self, n: int) -> int:\n # If we have exhausted all numbers in the sequence, return -1\n if self.current == len(self.A):\n return -1\n \n # Check if we are going to exhaust all of the current number we are looking at.\n if self.A[self.current] < n:\n n -= self.A[self.current] # Decrement n by the amount of the current number.\n self.current += 2 # Look at the next number.\n return self.next(n) # Return the next n in the sequence.\n\n # If we are not going to exhaust all of the current number,\n # decrement the amount exhausted by n and return the number.\n self.A[self.current] -= n\n return self.A[self.current + 1]\n\t\t
8
0
[]
1
rle-iterator
java faster than 100%
java-faster-than-100-by-20136916-9ieo
Runtime: 4 ms, faster than 100.00% of Java online submissions for RLE Iterator.\nMemory Usage: 39.4 MB, less than 47.86% of Java online submissions for RLE Iter
20136916
NORMAL
2021-08-22T06:12:14.834864+00:00
2021-08-22T06:13:55.810137+00:00
886
false
Runtime: 4 ms, faster than 100.00% of Java online submissions for RLE Iterator.\nMemory Usage: 39.4 MB, less than 47.86% of Java online submissions for RLE Iterator.\n\n```\nclass RLEIterator {\n int index;\n int[] encoding;\n public RLEIterator(int[] encoding) {\n index = 0;\n this.encoding = encoding;\n }\n \n public int next(int n) {\n while (index < encoding.length && n > encoding[index]) {\n n -= encoding[index];\n index += 2;\n \n }\n if (index >= encoding.length) {\n return -1;\n }\n encoding[index] -= n;\n return encoding[index + 1]; \n }\n}\n\n/**\n * Your RLEIterator object will be instantiated and called as such:\n * RLEIterator obj = new RLEIterator(encoding);\n * int param_1 = obj.next(n);\n */\n```
6
0
['Java']
1
rle-iterator
JavaScript Solution - O(n) Time and O(1) Space
javascript-solution-on-time-and-o1-space-maf9
\nvar RLEIterator = function(encoding) {\n this.index = 0;\n this.encoding = encoding;\n};\n\nRLEIterator.prototype.next = function(n) {\n while (n > 0
Deadication
NORMAL
2021-07-01T23:07:00.687645+00:00
2021-07-02T00:48:37.401617+00:00
419
false
```\nvar RLEIterator = function(encoding) {\n this.index = 0;\n this.encoding = encoding;\n};\n\nRLEIterator.prototype.next = function(n) {\n while (n > 0 && this.index < this.encoding.length) {\n\n const count = this.encoding[this.index];\n const num = this.encoding[this.index + 1];\n\n if (count < n) {\n n -= count;\n this.index += 2;\n } else {\n this.encoding[this.index] = count - n;\n return num;\n }\n }\n \n return -1;\n};\n ```
6
0
['JavaScript']
0
rle-iterator
Python binary search O(lgn) and linear search O(n)
python-binary-search-olgn-and-linear-sea-k1a9
If we don\'t take initialization into account, the time complexity of the binary search would be O(lgn)\n\nLinear seach O(n)\n\nclass RLEIterator:\n\n def __
savikx
NORMAL
2021-06-24T02:22:30.683103+00:00
2021-06-24T02:32:23.734039+00:00
537
false
If we don\'t take initialization into account, the time complexity of the binary search would be O(lgn)\n\nLinear seach O(n)\n```\nclass RLEIterator:\n\n def __init__(self, encoding: List[int]):\n self.a = encoding\n self.idx = 0\n \n\n def next(self, n: int) -> int:\n while self.idx + 1 < len(self.a):\n f, num = self.a[self.idx], self.a[self.idx + 1]\n if n > f:\n n -= f\n self.idx += 2\n elif n == f:\n self.idx += 2\n return num\n else:\n self.a[self.idx] -= n\n return num\n return -1\n```\nBineary search O(lgn)\n```\nclass RLEIterator:\n\n def __init__(self, encoding: List[int]):\n self.idx = i = 0\n self.arr = [] # accumulative sum of the number of elements\n self.num = []\n count = 0\n while i < len(encoding):\n f, num = encoding[i], encoding[i + 1]\n i += 2\n if f == 0: # skip 0\n continue\n count += f\n self.arr.append(count)\n self.num.append(num)\n \n\n def next(self, n: int) -> int:\n self.idx += n\n i = bisect.bisect_left(self.arr, self.idx)\n return self.num[i] if i < len(self.arr) else -1\n \n```
6
0
['Binary Tree', 'Python']
1
rle-iterator
Python3 simple solution - RLE Iterator
python3-simple-solution-rle-iterator-by-yl91y
\nclass RLEIterator:\n\n def __init__(self, A: List[int]):\n self.A = A[::-1]\n\n def next(self, n: int) -> int:\n acc = 0\n while ac
r0bertz
NORMAL
2020-06-11T06:35:40.644571+00:00
2020-06-11T06:35:40.644603+00:00
1,551
false
```\nclass RLEIterator:\n\n def __init__(self, A: List[int]):\n self.A = A[::-1]\n\n def next(self, n: int) -> int:\n acc = 0\n while acc < n:\n if not self.A:\n return -1\n times, num = self.A.pop(), self.A.pop()\n acc += times\n if acc > n:\n self.A.append(num)\n self.A.append(acc - n)\n return num\n```
6
0
['Python', 'Python3']
3
rle-iterator
fastest solution possible in python
fastest-solution-possible-in-python-by-d-rm0z
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
droj
NORMAL
2022-11-10T04:42:35.099750+00:00
2022-11-10T04:42:35.099788+00:00
1,071
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:1\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass RLEIterator:\n\n def __init__(self, encoding: List[int]):\n self.lst=encoding\n\n def next(self, n: int) -> int:\n num=-1\n while self.lst and n>0:\n if n<=self.lst[0]:\n self.lst[0]-=n \n n=0\n num=self.lst[1]\n else:\n n-=self.lst[0]\n self.lst[0]=0\n if self.lst[0]==0:\n self.lst.pop(0)\n self.lst.pop(0)\n if n>0:\n return -1\n return num\n```
5
0
['Python3']
1
rle-iterator
[JAVA] | O(n) time O(1) space | Recursive Approach
java-on-time-o1-space-recursive-approach-ptlb
\nint index;\nint[] A;\npublic RLEIterator(int[] A) {\n\tthis.A = A;\n\tindex = 0;\n}\n\npublic int next(int n) {\n\tif (index >= A.length) {\n\t\treturn -1;\n\
kushagragangwarr
NORMAL
2021-04-02T16:17:34.618157+00:00
2021-04-02T16:17:34.618190+00:00
679
false
```\nint index;\nint[] A;\npublic RLEIterator(int[] A) {\n\tthis.A = A;\n\tindex = 0;\n}\n\npublic int next(int n) {\n\tif (index >= A.length) {\n\t\treturn -1;\n\t}\n\n\tif (n <= A[index]) {\n\t\tA[index] -= n;\n\t\treturn A[index + 1];\n\t} else {\n\t\tindex += 2;\n\t\treturn next(n - A[index - 2]);\n\t}\n}\n```
5
0
['Recursion', 'Java']
0
rle-iterator
Short concise Java solution, beats 97%.
short-concise-java-solution-beats-97-by-xwxl9
Intuition\nyou cannot decode the original array and loop through it since occurance are of the order 10^9\n# Approach\nProcess the original encoding array, main
dm76518
NORMAL
2023-12-28T12:17:06.479430+00:00
2023-12-28T12:17:06.479456+00:00
274
false
# Intuition\nyou cannot decode the original array and loop through it since occurance are of the order 10^9\n# Approach\nProcess the original encoding array, maintain a variable cursor. there will be two cases \nif n > encoding[cursor] , decrement n by encoding[cursor] and proceed\n\nif n <= encoding[cursor] , decrement encoding[cursor] by n and return encoding[cursor+1] (the actual element).\n\nif cursor exceeds arrays length return -1 \n# Complexity\n- Time complexity:\nO(N)\n- Space complexity:\nO(N)\n# Code\n```\nclass RLEIterator {\n int[] encodingArr;\n int cursor;\n public RLEIterator(int[] encoding) {\n this.encodingArr = new int[encoding.length];\n for(int i = 0;i<encoding.length;i++){\n encodingArr[i] = encoding[i];\n }\n this.cursor = 0;\n }\n \n public int next(int n) {\n while(cursor < encodingArr.length){\n if(encodingArr[cursor] >= n){\n encodingArr[cursor] -= n;\n return encodingArr[cursor+1];\n }else{\n n -= encodingArr[cursor];\n cursor +=2;\n }\n }\n return -1;\n\n }\n}\n\n/**\n * Your RLEIterator object will be instantiated and called as such:\n * RLEIterator obj = new RLEIterator(encoding);\n * int param_1 = obj.next(n);\n */\n```
4
0
['Java']
0
rle-iterator
Javascript recursion. Runtime O(N), space O(1);
javascript-recursion-runtime-on-space-o1-8ynv
\nvar RLEIterator = function(A) {\n this.A = A;\n this.index = 0;\n};\n\n/** \n * @param {number} n\n * @return {number}\n */\nRLEIterator.prototype.next
mois
NORMAL
2020-12-25T19:08:40.658729+00:00
2020-12-25T19:08:40.658769+00:00
274
false
```\nvar RLEIterator = function(A) {\n this.A = A;\n this.index = 0;\n};\n\n/** \n * @param {number} n\n * @return {number}\n */\nRLEIterator.prototype.next = function(n) {\n let diff = n - this.A[this.index];\n if(diff <= 0) {\n this.A[this.index]-=n;\n return this.A[this.index+1];\n } else {\n this.index += 2;\n if(this.index >= this.A.length) {\n return -1;\n }\n return this.next(diff);\n }\n};\n```
4
0
['Recursion', 'JavaScript']
0
rle-iterator
O(log(n)) Binary Search Beats 100% C++ 32 Line
ologn-binary-search-beats-100-c-32-line-x8kyw
\nclass RLEIterator {\npublic:\n RLEIterator(vector<int> A) {// O(n) time \n\t\tloc=0;\n sum=0;\n\t\tcnt.push_back(0);\n\t\tnum.push_back(-1); \n\t\tf
zhongzeng
NORMAL
2018-12-10T21:05:01.080941+00:00
2018-12-10T21:05:01.081006+00:00
1,010
false
```\nclass RLEIterator {\npublic:\n RLEIterator(vector<int> A) {// O(n) time \n\t\tloc=0;\n sum=0;\n\t\tcnt.push_back(0);\n\t\tnum.push_back(-1); \n\t\tfor( int i=0; i<A.size(); i+=2){\n\t\t\tcnt.push_back(cnt.back()+A[i]); \n\t\t\tnum.push_back(A[i+1]); \n\t\t}\n }\n \n int next(int n) {// O(log(n)) time\n\t\tsum+=n;\n\t\tif(cnt.back()<sum)\treturn -1;\n\t\tint b=loc, e=cnt.size()-1, mid;// (b,e]\n\t\twhile(b+1<e){\n\t\t\tmid=b+(e-b)/2;\n\t\t\tif(cnt[mid]<sum)\tb=mid;\n\t\t\telse\te=mid;\n\t\t}\n\t\tloc=b;// update begining point to speed up binary search \n\t\treturn num[e];\n }\n\t\nprotected:\n\tint loc;// current location \n\tlong sum;// total count \n\tvector<long> cnt;// count \n\tvector<int> num;// number value \n};\n```
4
0
[]
2