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.
Move from brute-force thinking to an efficient approach using math strategy.
Given n points on a 1-D plane, where the ith point (from 0 to n-1) is at x = i, find the number of ways we can draw exactly k non-overlapping line segments such that each segment covers two or more points. The endpoints of each segment must have integral coordinates. The k line segments do not have to cover all n points, and they are allowed to share endpoints.
Return the number of ways we can draw k non-overlapping line segments. Since this number can be huge, return it modulo 109 + 7.
Example 1:
Input: n = 4, k = 2
Output: 5
Explanation: The two line segments are shown in red and blue.
The image above shows the 5 different ways {(0,2),(2,3)}, {(0,1),(1,3)}, {(0,1),(2,3)}, {(1,2),(2,3)}, {(0,1),(1,2)}.
Example 2:
Input: n = 3, k = 1
Output: 3
Explanation: The 3 ways are {(0,1)}, {(0,2)}, {(1,2)}.
Example 3:
Input: n = 30, k = 7 Output: 796297179 Explanation: The total number of possible ways to draw 7 line segments is 3796297200. Taking this number modulo 109 + 7 gives us 796297179.
Constraints:
2 <= n <= 10001 <= k <= n-1Problem summary: Given n points on a 1-D plane, where the ith point (from 0 to n-1) is at x = i, find the number of ways we can draw exactly k non-overlapping line segments such that each segment covers two or more points. The endpoints of each segment must have integral coordinates. The k line segments do not have to cover all n points, and they are allowed to share endpoints. Return the number of ways we can draw k non-overlapping line segments. Since this number can be huge, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
4 2
3 1
30 7
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1621: Number of Sets of K Non-Overlapping Line Segments
class Solution {
private static final int MOD = (int) 1e9 + 7;
public int numberOfSets(int n, int k) {
int[][] f = new int[n + 1][k + 1];
int[][] g = new int[n + 1][k + 1];
f[1][0] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 0; j <= k; ++j) {
f[i][j] = (f[i - 1][j] + g[i - 1][j]) % MOD;
g[i][j] = g[i - 1][j];
if (j > 0) {
g[i][j] += f[i - 1][j - 1];
g[i][j] %= MOD;
g[i][j] += g[i - 1][j - 1];
g[i][j] %= MOD;
}
}
}
return (f[n][k] + g[n][k]) % MOD;
}
}
// Accepted solution for LeetCode #1621: Number of Sets of K Non-Overlapping Line Segments
func numberOfSets(n int, k int) int {
f := make([][]int, n+1)
g := make([][]int, n+1)
for i := range f {
f[i] = make([]int, k+1)
g[i] = make([]int, k+1)
}
f[1][0] = 1
var mod int = 1e9 + 7
for i := 2; i <= n; i++ {
for j := 0; j <= k; j++ {
f[i][j] = (f[i-1][j] + g[i-1][j]) % mod
g[i][j] = g[i-1][j]
if j > 0 {
g[i][j] += f[i-1][j-1]
g[i][j] %= mod
g[i][j] += g[i-1][j-1]
g[i][j] %= mod
}
}
}
return (f[n][k] + g[n][k]) % mod
}
# Accepted solution for LeetCode #1621: Number of Sets of K Non-Overlapping Line Segments
class Solution:
def numberOfSets(self, n: int, k: int) -> int:
mod = 10**9 + 7
f = [[0] * (k + 1) for _ in range(n + 1)]
g = [[0] * (k + 1) for _ in range(n + 1)]
f[1][0] = 1
for i in range(2, n + 1):
for j in range(k + 1):
f[i][j] = (f[i - 1][j] + g[i - 1][j]) % mod
g[i][j] = g[i - 1][j]
if j:
g[i][j] += f[i - 1][j - 1]
g[i][j] %= mod
g[i][j] += g[i - 1][j - 1]
g[i][j] %= mod
return (f[-1][-1] + g[-1][-1]) % mod
// Accepted solution for LeetCode #1621: Number of Sets of K Non-Overlapping Line Segments
struct Solution;
use std::collections::HashMap;
const MOD: i64 = 1_000_000_007;
impl Solution {
fn number_of_sets(n: i32, k: i32) -> i32 {
let mut memo: HashMap<(i32, i32, bool), i64> = HashMap::new();
Self::dp(n, k, false, &mut memo) as i32
}
fn dp(n: i32, k: i32, started: bool, memo: &mut HashMap<(i32, i32, bool), i64>) -> i64 {
if k == 0 {
return 1;
}
if n == 0 {
return 0;
}
if let Some(&res) = memo.get(&(n, k, started)) {
return res;
}
let mut res = Self::dp(n - 1, k, started, memo);
if started {
res += Self::dp(n, k - 1, false, memo);
} else {
res += Self::dp(n - 1, k, true, memo);
}
res %= MOD;
memo.insert((n, k, started), res);
res
}
}
#[test]
fn test() {
let n = 4;
let k = 2;
let res = 5;
assert_eq!(Solution::number_of_sets(n, k), res);
let n = 3;
let k = 1;
let res = 3;
assert_eq!(Solution::number_of_sets(n, k), res);
let n = 30;
let k = 7;
let res = 796297179;
assert_eq!(Solution::number_of_sets(n, k), res);
}
// Accepted solution for LeetCode #1621: Number of Sets of K Non-Overlapping Line Segments
function numberOfSets(n: number, k: number): number {
const f = Array.from({ length: n + 1 }, _ => new Array(k + 1).fill(0));
const g = Array.from({ length: n + 1 }, _ => new Array(k + 1).fill(0));
f[1][0] = 1;
const mod = 10 ** 9 + 7;
for (let i = 2; i <= n; ++i) {
for (let j = 0; j <= k; ++j) {
f[i][j] = (f[i - 1][j] + g[i - 1][j]) % mod;
g[i][j] = g[i - 1][j];
if (j) {
g[i][j] += f[i - 1][j - 1];
g[i][j] %= mod;
g[i][j] += g[i - 1][j - 1];
g[i][j] %= mod;
}
}
}
return (f[n][k] + g[n][k]) % mod;
}
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.