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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en" class="dark">
|
| 3 |
+
|
| 4 |
+
<head>
|
| 5 |
+
|
| 6 |
+
<meta charset="UTF-8">
|
| 7 |
+
|
| 8 |
+
<meta name="viewport"
|
| 9 |
+
content="width=device-width, initial-scale=1.0">
|
| 10 |
+
|
| 11 |
+
<title>Subsets | DSA Portfolio</title>
|
| 12 |
+
|
| 13 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 14 |
+
|
| 15 |
+
<link rel="stylesheet" href="../assets/style.css">
|
| 16 |
+
|
| 17 |
+
<link rel="preconnect"
|
| 18 |
+
href="https://fonts.googleapis.com">
|
| 19 |
+
|
| 20 |
+
<link
|
| 21 |
+
href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;700;800&family=JetBrains+Mono:wght@400;500&display=swap"
|
| 22 |
+
rel="stylesheet">
|
| 23 |
+
|
| 24 |
+
</head>
|
| 25 |
+
|
| 26 |
+
<body class="bg-slate-950 text-white font-inter">
|
| 27 |
+
|
| 28 |
+
<!-- Header -->
|
| 29 |
+
<header class="border-b border-white/10">
|
| 30 |
+
|
| 31 |
+
<div class="max-w-7xl mx-auto px-6 py-5">
|
| 32 |
+
|
| 33 |
+
<a href="../index.html"
|
| 34 |
+
class="text-cyan-400">
|
| 35 |
+
← Back to Portfolio
|
| 36 |
+
</a>
|
| 37 |
+
|
| 38 |
+
</div>
|
| 39 |
+
|
| 40 |
+
</header>
|
| 41 |
+
|
| 42 |
+
<!-- Main -->
|
| 43 |
+
<main class="max-w-7xl mx-auto px-6 py-16 grid lg:grid-cols-3 gap-10">
|
| 44 |
+
|
| 45 |
+
<!-- Content -->
|
| 46 |
+
<section class="lg:col-span-2">
|
| 47 |
+
|
| 48 |
+
<p class="text-cyan-400 font-mono">
|
| 49 |
+
Recursion / Backtracking
|
| 50 |
+
</p>
|
| 51 |
+
|
| 52 |
+
<h1 class="text-5xl font-black mt-4">
|
| 53 |
+
Subsets
|
| 54 |
+
</h1>
|
| 55 |
+
|
| 56 |
+
<p class="text-slate-400 mt-6 text-lg">
|
| 57 |
+
Generate all possible subsets of a given array using recursion and backtracking.
|
| 58 |
+
</p>
|
| 59 |
+
|
| 60 |
+
<!-- Intuition -->
|
| 61 |
+
<div class="mt-14">
|
| 62 |
+
|
| 63 |
+
<h2 class="text-3xl font-bold">
|
| 64 |
+
Intuition
|
| 65 |
+
</h2>
|
| 66 |
+
|
| 67 |
+
<p class="text-slate-300 mt-5 leading-8">
|
| 68 |
+
For every element we have two choices:
|
| 69 |
+
either include it in the subset or exclude it.
|
| 70 |
+
This naturally forms a recursion tree.
|
| 71 |
+
</p>
|
| 72 |
+
|
| 73 |
+
</div>
|
| 74 |
+
|
| 75 |
+
<!-- Algorithm -->
|
| 76 |
+
<div class="mt-14">
|
| 77 |
+
|
| 78 |
+
<h2 class="text-3xl font-bold">
|
| 79 |
+
Algorithm
|
| 80 |
+
</h2>
|
| 81 |
+
|
| 82 |
+
<ol class="mt-5 space-y-3 text-slate-300 list-decimal list-inside">
|
| 83 |
+
|
| 84 |
+
<li>Start recursion from index 0</li>
|
| 85 |
+
|
| 86 |
+
<li>Include current element</li>
|
| 87 |
+
|
| 88 |
+
<li>Move to next index</li>
|
| 89 |
+
|
| 90 |
+
<li>Backtrack by removing element</li>
|
| 91 |
+
|
| 92 |
+
<li>Exclude current element</li>
|
| 93 |
+
|
| 94 |
+
</ol>
|
| 95 |
+
|
| 96 |
+
</div>
|
| 97 |
+
|
| 98 |
+
<!-- Dry Run -->
|
| 99 |
+
<div class="mt-14">
|
| 100 |
+
|
| 101 |
+
<h2 class="text-3xl font-bold">
|
| 102 |
+
Dry Run
|
| 103 |
+
</h2>
|
| 104 |
+
|
| 105 |
+
<div class="mt-5 p-6 rounded-2xl bg-white/5 border border-white/10 font-mono">
|
| 106 |
+
|
| 107 |
+
[] <br>
|
| 108 |
+
[1] <br>
|
| 109 |
+
[1,2] <br>
|
| 110 |
+
[2]
|
| 111 |
+
|
| 112 |
+
</div>
|
| 113 |
+
|
| 114 |
+
</div>
|
| 115 |
+
|
| 116 |
+
<!-- Code -->
|
| 117 |
+
<div class="mt-14">
|
| 118 |
+
|
| 119 |
+
<h2 class="text-3xl font-bold">
|
| 120 |
+
Python Solution
|
| 121 |
+
</h2>
|
| 122 |
+
|
| 123 |
+
<pre
|
| 124 |
+
class="mt-5 p-6 rounded-2xl bg-black overflow-x-auto border border-white/10 text-sm code-block">
|
| 125 |
+
|
| 126 |
+
<code>
|
| 127 |
+
class Solution:
|
| 128 |
+
|
| 129 |
+
def subsets(self, nums):
|
| 130 |
+
|
| 131 |
+
ans = []
|
| 132 |
+
|
| 133 |
+
def backtrack(i, curr):
|
| 134 |
+
|
| 135 |
+
if i == len(nums):
|
| 136 |
+
|
| 137 |
+
ans.append(curr[:])
|
| 138 |
+
return
|
| 139 |
+
|
| 140 |
+
curr.append(nums[i])
|
| 141 |
+
|
| 142 |
+
backtrack(i + 1, curr)
|
| 143 |
+
|
| 144 |
+
curr.pop()
|
| 145 |
+
|
| 146 |
+
backtrack(i + 1, curr)
|
| 147 |
+
|
| 148 |
+
backtrack(0, [])
|
| 149 |
+
|
| 150 |
+
return ans
|
| 151 |
+
</code>
|
| 152 |
+
|
| 153 |
+
</pre>
|
| 154 |
+
|
| 155 |
+
</div>
|
| 156 |
+
|
| 157 |
+
</section>
|
| 158 |
+
|
| 159 |
+
<!-- Sidebar -->
|
| 160 |
+
<aside>
|
| 161 |
+
|
| 162 |
+
<div class="sticky top-10 p-6 rounded-3xl bg-white/5 border border-white/10">
|
| 163 |
+
|
| 164 |
+
<h2 class="text-2xl font-bold">
|
| 165 |
+
Problem Info
|
| 166 |
+
</h2>
|
| 167 |
+
|
| 168 |
+
<div class="mt-8 space-y-5">
|
| 169 |
+
|
| 170 |
+
<div>
|
| 171 |
+
<p class="text-slate-400 text-sm">
|
| 172 |
+
Difficulty
|
| 173 |
+
</p>
|
| 174 |
+
|
| 175 |
+
<p class="text-yellow-400 font-semibold">
|
| 176 |
+
Medium
|
| 177 |
+
</p>
|
| 178 |
+
</div>
|
| 179 |
+
|
| 180 |
+
<div>
|
| 181 |
+
<p class="text-slate-400 text-sm">
|
| 182 |
+
Topic
|
| 183 |
+
</p>
|
| 184 |
+
|
| 185 |
+
<p>
|
| 186 |
+
Recursion
|
| 187 |
+
</p>
|
| 188 |
+
</div>
|
| 189 |
+
|
| 190 |
+
<div>
|
| 191 |
+
<p class="text-slate-400 text-sm">
|
| 192 |
+
Time Complexity
|
| 193 |
+
</p>
|
| 194 |
+
|
| 195 |
+
<p>
|
| 196 |
+
O(2^n)
|
| 197 |
+
</p>
|
| 198 |
+
</div>
|
| 199 |
+
|
| 200 |
+
<div>
|
| 201 |
+
<p class="text-slate-400 text-sm">
|
| 202 |
+
Space Complexity
|
| 203 |
+
</p>
|
| 204 |
+
|
| 205 |
+
<p>
|
| 206 |
+
O(n)
|
| 207 |
+
</p>
|
| 208 |
+
</div>
|
| 209 |
+
|
| 210 |
+
<div>
|
| 211 |
+
<p class="text-slate-400 text-sm">
|
| 212 |
+
Platform
|
| 213 |
+
</p>
|
| 214 |
+
|
| 215 |
+
<p>
|
| 216 |
+
LeetCode
|
| 217 |
+
</p>
|
| 218 |
+
</div>
|
| 219 |
+
|
| 220 |
+
<div>
|
| 221 |
+
<p class="text-slate-400 text-sm">
|
| 222 |
+
Date Solved
|
| 223 |
+
</p>
|
| 224 |
+
|
| 225 |
+
<p>
|
| 226 |
+
2026-05-08
|
| 227 |
+
</p>
|
| 228 |
+
</div>
|
| 229 |
+
|
| 230 |
+
</div>
|
| 231 |
+
|
| 232 |
+
</div>
|
| 233 |
+
|
| 234 |
+
</aside>
|
| 235 |
+
|
| 236 |
+
</main>
|
| 237 |
+
|
| 238 |
+
</body>
|
| 239 |
+
|
| 240 |
+
</html>
|