You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Group elements by (num % k) to find independent groups
11
+
2. Within each group, sort and build chains where elements differ by k
12
+
3. For each chain, use House Robber DP to count valid subsets
13
+
4. Multiply results across all independent chains
14
+
15
+
Time: O(n log n), Space: O(n)
16
+
"""
17
+
# Group numbers by their remainder when divided by k
18
+
groups=defaultdict(list)
19
+
fornuminnums:
20
+
groups[num%k].append(num)
21
+
22
+
res=1
23
+
24
+
# Process each group independently
25
+
forgroupingroups.values():
26
+
group.sort()
27
+
28
+
# Build chains within this group
29
+
i=0
30
+
whilei<len(group):
31
+
chain= [group[i]]
32
+
j=i+1
33
+
34
+
# Build chain where each element is exactly k more than previous
35
+
whilej<len(group) andgroup[j] ==chain[-1] +k:
36
+
chain.append(group[j])
37
+
j+=1
38
+
39
+
# House Robber DP for this chain
40
+
m=len(chain)
41
+
ifm==1:
42
+
chain_res=2# {} or {chain[0]}
43
+
else:
44
+
take=1# Take first element
45
+
skip=1# Skip first element
46
+
47
+
foridxinrange(1, m):
48
+
new_take=skip# Can only take current if we skipped previous
49
+
new_skip=take+skip# Can skip current regardless
50
+
take, skip=new_take, new_skip
51
+
52
+
chain_res=take+skip
53
+
54
+
res*=chain_res
55
+
i=j
56
+
57
+
returnres
58
+
59
+
60
+
61
+
62
+
63
+
'''
64
+
Detailed Algorithm Explanation
65
+
Part 1: Why Group by num % k?
66
+
Two numbers can have a difference of exactly k only if they have the same remainder when divided by k.
67
+
68
+
Mathematical proof:
69
+
70
+
If a - b = k, then a = b + k
71
+
Therefore: a % k = (b + k) % k = b % k
72
+
Example: nums = [2, 3, 5, 8], k = 5
73
+
74
+
num | num % 5 | group
75
+
----|---------|-------
76
+
2 | 2 | Group A
77
+
3 | 3 | Group B
78
+
5 | 0 | Group C
79
+
8 | 3 | Group B
80
+
81
+
82
+
Why this matters: Elements from different groups can never differ by k, so they're independent. We can combine any subset from Group A with any subset from Group B.
83
+
84
+
Part 2: Building Chains
85
+
Within each group, we sort and find chains where consecutive elements differ by exactly k.
86
+
87
+
Example with Group B: [3, 8]
88
+
89
+
Sorted: [3, 8]
90
+
Check: 8 - 3 = 5 ✓
91
+
Chain: 3 → 8
92
+
93
+
Another example: nums = [1, 6, 11, 21], k = 5 (all have remainder 1)
94
+
95
+
Sorted: [1, 6, 11, 21]
96
+
Check: 6-1=5 ✓, 11-6=5 ✓, 21-11=10 ✗
97
+
Chains: [1 → 6 → 11], [21]
98
+
99
+
100
+
Part 3: House Robber DP - The Core Logic
101
+
For a chain like [3 → 8], we can't pick both 3 and 8 (they differ by k). This is the House Robber problem: count all subsets where we don't pick adjacent elements.
102
+
103
+
DP State Variables
104
+
take = number of valid subsets that INCLUDE the current element
105
+
skip = number of valid subsets that EXCLUDE the current element
106
+
107
+
DP Transitions
108
+
new_take = skip # To take current, we MUST have skipped previous
109
+
new_skip = take + skip # To skip current, we can take or skip previous
0 commit comments