Skip to content

Commit 6bca845

Browse files
Solution Leetcode and Explaination Every single line
1 parent 9a29b13 commit 6bca845

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
✅ Java Code: countGoodArrays
2+
3+
class Solution {
4+
public int countGoodArrays(int n, int m, int k) {
5+
final long[][] factAndInvFact = getFactAndInvFact(n);
6+
final long[] fact = factAndInvFact[0];
7+
final long[] invFact = factAndInvFact[1];
8+
return (int) (m * modPow(m - 1, n - k - 1) % MOD * nCk(n - 1, k, fact, invFact) % MOD);
9+
}
10+
11+
private static final int MOD = 1_000_000_007;
12+
13+
private long modPow(long x, long n) {
14+
if (n == 0)
15+
return 1;
16+
if (n % 2 == 1)
17+
return x * modPow(x, n - 1) % MOD;
18+
return modPow(x * x % MOD, n / 2);
19+
}
20+
21+
private long[][] getFactAndInvFact(int n) {
22+
long[] fact = new long[n + 1];
23+
long[] invFact = new long[n + 1];
24+
long[] inv = new long[n + 1];
25+
fact[0] = invFact[0] = 1;
26+
inv[0] = inv[1] = 1;
27+
for (int i = 1; i <= n; ++i) {
28+
if (i >= 2)
29+
inv[i] = MOD - MOD / i * inv[MOD % i] % MOD;
30+
fact[i] = fact[i - 1] * i % MOD;
31+
invFact[i] = invFact[i - 1] * inv[i] % MOD;
32+
}
33+
return new long[][] {fact, invFact};
34+
}
35+
36+
private int nCk(int n, int k, long[] fact, long[] invFact) {
37+
return (int) (fact[n] * invFact[k] % MOD * invFact[n - k] % MOD);
38+
}
39+
}
40+
41+
42+
43+
📘Explanation
44+
45+
public int countGoodArrays(int n, int m, int k)
46+
🔹 n = array ka size
47+
🔹 m = allowed values 1 se m tak
48+
🔹 k = number of equal adjacent pairs required
49+
50+
final long[][] factAndInvFact = getFactAndInvFact(n);
51+
🔹 Factorials aur unka modular inverse calculate kar liya for combination calculation.
52+
53+
final long[] fact = factAndInvFact[0];
54+
final long[] invFact = factAndInvFact[1];
55+
🔹 Arrays ko alag variables me assign kar liya.
56+
57+
return (int) (m * modPow(m - 1, n - k - 1) % MOD * nCk(n - 1, k, fact, invFact) % MOD);
58+
🔹 Final answer:
59+
60+
Pehle element ke liye m choices
61+
62+
Baaki n-k-1 elements ke liye (m-1) choices (different from previous)
63+
64+
n-1 gaps me se k equal hone chahiye → nCk(n-1, k)
65+
66+
Supporting Methods:
67+
modPow(x, n) → Fast Power (Binary Exponentiation)
68+
69+
private long modPow(long x, long n)
70+
🔹 x^n % MOD calculate karta hai efficiently
71+
🔹 Recursive approach use kiya with modulo at each step
72+
73+
getFactAndInvFact(n)
74+
75+
private long[][] getFactAndInvFact(int n)
76+
🔹 Factorials aur unka inverse precompute karta hai:
77+
78+
fact[i] = i!
79+
80+
inv[i] = i⁻¹ mod MOD
81+
82+
invFact[i] = (i!)⁻¹ mod MOD
83+
84+
inv[i] = MOD - MOD / i * inv[MOD % i] % MOD;
85+
🔹 Fermat's Little Theorem use kiya inverse nikalne ke liye
86+
87+
nCk(n, k, fact, invFact)
88+
89+
private int nCk(int n, int k, long[] fact, long[] invFact)
90+
🔹 nCk (combination) = fact[n] * invFact[k] * invFact[n-k] % MOD
91+
92+
🧠 Example Input:
93+
n = 4, m = 3, k = 1
94+
Step by Step:
95+
96+
Pehle element ke liye 3 choices
97+
98+
4-1-1 = 2 elements = (m-1)^2 = 2^2 = 4
99+
100+
3 gaps, 1 equal = C(3,1) = 3
101+
→ Total = 3 * 4 * 3 = 36
102+
103+
🚀 Time Complexity:
104+
105+
O(n) for precomputing factorials
106+
107+
O(log n) for power calculation
108+
109+
O(1) for final answer after precomputation
110+
111+
🚀 Space Complexity:
112+
113+
O(n) for storing factorial and inverse arrays
114+
115+
🔗 Need more help or Java tricks?
116+
https://www.linkedin.com/in/saurabh884095/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int countGoodArrays(int n, int m, int k) {
3+
final long[][] factAndInvFact = getFactAndInvFact(n);
4+
final long[] fact = factAndInvFact[0];
5+
final long[] invFact = factAndInvFact[1];
6+
return (int) (m * modPow(m - 1, n - k - 1) % MOD * nCk(n - 1, k, fact, invFact) % MOD);
7+
}
8+
9+
private static final int MOD = 1_000_000_007;
10+
11+
private long modPow(long x, long n) {
12+
if (n == 0)
13+
return 1;
14+
if (n % 2 == 1)
15+
return x * modPow(x, n - 1) % MOD;
16+
return modPow(x * x % MOD, n / 2);
17+
}
18+
19+
private long[][] getFactAndInvFact(int n) {
20+
long[] fact = new long[n + 1];
21+
long[] invFact = new long[n + 1];
22+
long[] inv = new long[n + 1];
23+
fact[0] = invFact[0] = 1;
24+
inv[0] = inv[1] = 1;
25+
for (int i = 1; i <= n; ++i) {
26+
if (i >= 2)
27+
inv[i] = MOD - MOD / i * inv[MOD % i] % MOD;
28+
fact[i] = fact[i - 1] * i % MOD;
29+
invFact[i] = invFact[i - 1] * inv[i] % MOD;
30+
}
31+
return new long[][] {fact, invFact};
32+
}
33+
34+
private int nCk(int n, int k, long[] fact, long[] invFact) {
35+
return (int) (fact[n] * invFact[k] % MOD * invFact[n - k] % MOD);
36+
}
37+
}

0 commit comments

Comments
 (0)