Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
There are n uniquely-sized sticks whose lengths are integers from 1 to n. You want to arrange the sticks such that exactly k sticks are visible from the left. A stick is visible from the left if there are no longer sticks to the left of it.
[1,3,2,5,4], then the sticks with lengths 1, 3, and 5 are visible from the left.Given n and k, return the number of such arrangements. Since the answer may be large, return it modulo 109 + 7.
Example 1:
Input: n = 3, k = 2 Output: 3 Explanation: [1,3,2], [2,3,1], and [2,1,3] are the only arrangements such that exactly 2 sticks are visible. The visible sticks are underlined.
Example 2:
Input: n = 5, k = 5 Output: 1 Explanation: [1,2,3,4,5] is the only arrangement such that all 5 sticks are visible. The visible sticks are underlined.
Example 3:
Input: n = 20, k = 11 Output: 647427950 Explanation: There are 647427950 (mod 109 + 7) ways to rearrange the sticks such that exactly 11 sticks are visible.
Constraints:
1 <= n <= 10001 <= k <= nProblem summary: There are n uniquely-sized sticks whose lengths are integers from 1 to n. You want to arrange the sticks such that exactly k sticks are visible from the left. A stick is visible from the left if there are no longer sticks to the left of it. For example, if the sticks are arranged [1,3,2,5,4], then the sticks with lengths 1, 3, and 5 are visible from the left. Given n and k, return the number of such arrangements. Since the answer may be large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
3 2
5 5
20 11
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1866: Number of Ways to Rearrange Sticks With K Sticks Visible
class Solution {
public int rearrangeSticks(int n, int k) {
final int mod = (int) 1e9 + 7;
int[][] f = new int[n + 1][k + 1];
f[0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
f[i][j] = (int) ((f[i - 1][j - 1] + f[i - 1][j] * (long) (i - 1)) % mod);
}
}
return f[n][k];
}
}
// Accepted solution for LeetCode #1866: Number of Ways to Rearrange Sticks With K Sticks Visible
func rearrangeSticks(n int, k int) int {
const mod = 1e9 + 7
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, k+1)
}
f[0][0] = 1
for i := 1; i <= n; i++ {
for j := 1; j <= k; j++ {
f[i][j] = (f[i-1][j-1] + (i-1)*f[i-1][j]) % mod
}
}
return f[n][k]
}
# Accepted solution for LeetCode #1866: Number of Ways to Rearrange Sticks With K Sticks Visible
class Solution:
def rearrangeSticks(self, n: int, k: int) -> int:
mod = 10**9 + 7
f = [[0] * (k + 1) for _ in range(n + 1)]
f[0][0] = 1
for i in range(1, n + 1):
for j in range(1, k + 1):
f[i][j] = (f[i - 1][j - 1] + f[i - 1][j] * (i - 1)) % mod
return f[n][k]
// Accepted solution for LeetCode #1866: Number of Ways to Rearrange Sticks With K Sticks Visible
/**
* [1866] Number of Ways to Rearrange Sticks With K Sticks Visible
*
* There are n uniquely-sized sticks whose lengths are integers from 1 to n. You want to arrange the sticks such that exactly k sticks are visible from the left. A stick is visible from the left if there are no longer sticks to the left of it.
*
* For example, if the sticks are arranged [<u>1</u>,<u>3</u>,2,<u>5</u>,4], then the sticks with lengths 1, 3, and 5 are visible from the left.
*
* Given n and k, return the number of such arrangements. Since the answer may be large, return it modulo 10^9 + 7.
*
* Example 1:
*
* Input: n = 3, k = 2
* Output: 3
* Explanation: [<u>1</u>,<u>3</u>,2], [<u>2</u>,<u>3</u>,1], and [<u>2</u>,1,<u>3</u>] are the only arrangements such that exactly 2 sticks are visible.
* The visible sticks are underlined.
*
* Example 2:
*
* Input: n = 5, k = 5
* Output: 1
* Explanation: [<u>1</u>,<u>2</u>,<u>3</u>,<u>4</u>,<u>5</u>] is the only arrangement such that all 5 sticks are visible.
* The visible sticks are underlined.
*
* Example 3:
*
* Input: n = 20, k = 11
* Output: 647427950
* Explanation: There are 647427950 (mod 10^9 + 7) ways to rearrange the sticks such that exactly 11 sticks are visible.
*
*
* Constraints:
*
* 1 <= n <= 1000
* 1 <= k <= n
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible/
// discuss: https://leetcode.com/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn rearrange_sticks(n: i32, k: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1866_example_1() {
let n = 3;
let k = 2;
let result = 3;
assert_eq!(Solution::rearrange_sticks(n, k), result);
}
#[test]
#[ignore]
fn test_1866_example_2() {
let n = 5;
let k = 5;
let result = 1;
assert_eq!(Solution::rearrange_sticks(n, k), result);
}
#[test]
#[ignore]
fn test_1866_example_3() {
let n = 20;
let k = 11;
let result = 647427950;
assert_eq!(Solution::rearrange_sticks(n, k), result);
}
}
// Accepted solution for LeetCode #1866: Number of Ways to Rearrange Sticks With K Sticks Visible
function rearrangeSticks(n: number, k: number): number {
const mod = 10 ** 9 + 7;
const f: number[][] = Array.from({ length: n + 1 }, () =>
Array.from({ length: k + 1 }, () => 0),
);
f[0][0] = 1;
for (let i = 1; i <= n; ++i) {
for (let j = 1; j <= k; ++j) {
f[i][j] = (f[i - 1][j - 1] + (i - 1) * f[i - 1][j]) % mod;
}
}
return f[n][k];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.