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.
You are given two positive integers n and k.
Return an integer denoting the nth smallest positive integer that has exactly k ones in its binary representation. It is guaranteed that the answer is strictly less than 250.
Example 1:
Input: n = 4, k = 2
Output: 9
Explanation:
The 4 smallest positive integers that have exactly k = 2 ones in their binary representations are:
3 = 1125 = 10126 = 11029 = 10012Example 2:
Input: n = 3, k = 1
Output: 4
Explanation:
The 3 smallest positive integers that have exactly k = 1 one in their binary representations are:
1 = 122 = 1024 = 1002Constraints:
1 <= n <= 2501 <= k <= 50250.Problem summary: You are given two positive integers n and k. Return an integer denoting the nth smallest positive integer that has exactly k ones in its binary representation. It is guaranteed that the answer is strictly less than 250.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Bit Manipulation
4 2
3 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3821: Find Nth Smallest Integer With K One Bits
class Solution {
private static final int MX = 50;
private static final long[][] c = new long[MX][MX + 1];
static {
for (int i = 0; i < MX; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++) {
c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
}
}
public long nthSmallest(long n, int k) {
long ans = 0;
for (int i = 49; i >= 0; i--) {
if (n > c[i][k]) {
n -= c[i][k];
ans |= 1L << i;
k--;
if (k == 0) {
break;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3821: Find Nth Smallest Integer With K One Bits
const MX = 50
var c [MX][MX + 1]int64
func init() {
for i := 0; i < MX; i++ {
c[i][0] = 1
for j := 1; j <= i; j++ {
c[i][j] = c[i-1][j-1] + c[i-1][j]
}
}
}
func nthSmallest(n int64, k int) int64 {
var ans int64 = 0
for i := 49; i >= 0; i-- {
if n > c[i][k] {
n -= c[i][k]
ans |= 1 << i
k--
if k == 0 {
break
}
}
}
return ans
}
# Accepted solution for LeetCode #3821: Find Nth Smallest Integer With K One Bits
mx = 50
c = [[0] * (mx + 1) for _ in range(mx)]
for i in range(mx):
c[i][0] = 1
for j in range(1, i + 1):
c[i][j] = c[i - 1][j - 1] + c[i - 1][j]
class Solution:
def nthSmallest(self, n: int, k: int) -> int:
ans = 0
for i in range(49, -1, -1):
if n > c[i][k]:
n -= c[i][k]
ans |= 1 << i
k -= 1
if k == 0:
break
return ans
// Accepted solution for LeetCode #3821: Find Nth Smallest Integer With K One Bits
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3821: Find Nth Smallest Integer With K One Bits
// class Solution {
// private static final int MX = 50;
// private static final long[][] c = new long[MX][MX + 1];
//
// static {
// for (int i = 0; i < MX; i++) {
// c[i][0] = 1;
// for (int j = 1; j <= i; j++) {
// c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
// }
// }
// }
//
// public long nthSmallest(long n, int k) {
// long ans = 0;
// for (int i = 49; i >= 0; i--) {
// if (n > c[i][k]) {
// n -= c[i][k];
// ans |= 1L << i;
// k--;
// if (k == 0) {
// break;
// }
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3821: Find Nth Smallest Integer With K One Bits
const MX = 50;
const c: bigint[][] = Array.from({ length: MX }, () => Array(MX + 1).fill(0n));
for (let i = 0; i < MX; i++) {
c[i][0] = 1n;
for (let j = 1; j <= i; j++) {
c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
}
function nthSmallest(n: number, k: number): number {
let nn = BigInt(n);
let ans = 0n;
for (let i = 49; i >= 0; i--) {
if (nn > c[i][k]) {
nn -= c[i][k];
ans |= 1n << BigInt(i);
if (--k === 0) {
break;
}
}
}
return Number(ans);
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
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.