Generate All Possible Subsets | Return Power Set | Leetcode 78. | Bit Manipulation | Two for loops

preview_player
Показать описание
Given an integer array nums of unique elements, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.



Example 1:

Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:

Input: nums = [0]
Output: [[],[0]]

Check out our other playlists:

Dynamic Programming:

Trees:

Heaps and Maps:

Arrays and Maths:

Bit Manipulation:

Greedy Algorithms:

Sorting and Searching:

Strings:

Linked Lists:

Stack and Queues:

Two Pointers:

Graphs, BFS, DFS:

Backtracking:

Non- DSA playlists:

Probability:

SQL-Basic Join functions:

SQL-Basic Aggregate functions:
Рекомендации по теме
Комментарии
Автор

Time Complexity : O(N*2^(N))
int n = nums.size(); // n=3 -> 2^3 = 8

for(int i=0;i<(1<<n);i++)
{
vector<int>v;
for(int j=0;j<n;j++)
{
if((1<<j)&i)
v.push_back(nums[j]);
}
ans.push_back(v);

}
return ans;

probabilitycodingisfunis
Автор

I saw 2-3 videos before and I didnt understand their solution but as you started to explain the code I coded myself.
Thanks maam for your solution.

Codro
Автор

I appreciate your speed personally, thank you for making an informative video with clear examples as you went step by step.

jonlombardi
Автор

Ma'am wouldn't it be better if we initialise the vector as
vector<int>v = { };
since there won't be any garbage value in it

aviral
Автор

I solved it by my own just came here to see ur solution and I again learnt something new thanks alisha I only prefer ur videos.

satyamgupta
Автор

Thanks u have given very clear explaination

shrawan
Автор

hi alisha it would be more easy to understand if you dry run your code on wb and then explain

khushil
Автор

very well explained, 👏 today i learnt a new approach of this question.

tarunpatel
Автор

Thanks. Really nice and detailed explanation

sanketdatta
Автор

Great explanation but theres a small correction at 06:50 for i = 3 (011) you are going to take [2, 3]

maheshjamdade
Автор

Finally found a better explanation and solution, thanks @code with Alisha

devangrathod
Автор

Very helpful di. Thanks a lot. Just a small doubt about how (1<<j )&i is working. Please tell 🙏🏻

sunshineandrainbow
Автор

very clear explanation. thank you didi.

sanskarkumar
Автор

I spend 1 hr behind this problem made the spaghetti of it
And code here to see this
Well this is new approach to me
Thanks teacher

kaustubhsonar
Автор

ma'am how "i" number is converted to binary number

rahulchoudhary
Автор

Great, finally found subset using power set

rhishishranjan
Автор

thanks for the video it very clear explanation🙂

KomalKanwarRathore-oyyv
Автор

mam can you explain how decimal been converted to bits in c++;

RAMKUMAR-ebkq
Автор

the grid above is a 3*5 grid. This grid has four infected patients at position (0, 0), (0, 3), (1, 3) and (2, 3). Patient at (0, 0) will most likely infect recovering patient at (1, 0) and (0, 1), infected patient (0, 3) will most likely infect recovering patient (0.4), infected patient (1, 3) will infect both recovering patient at (1, 2) and (1, 4). And lastly, infected patient (2, 3) will infect recovering patient (2, 4). Total number of recovering patients infect is 6. For the first round of infection, one unit of time will be recorded. For the second round of infection, recovering patient (2, 0) will be infected by patient (1, 0). Patient (1, 0) was infected in the first round. Only one patient is infected in the second round. Total amount of time taken for the whole area to be infected is 2.


What is the algorthm can we use?

Umn
Автор

very sloppy presentation to be honest. You take it slow, write down things properly. I get the gist but still.

ayushtiwari