Leetcode Problem 1900-1999
O(N) Time and O(N) Space solution
- For i=0 to n , simply do what they have asked to.
- Push_back(nums[nums[i]])
O(N) Time and O(1) Space optimization
- This is done to keep both old and new value together.
- going by the example of [5,0,1,2,3,4], n = 6(size of array)
nums[i] = nums[i]+(n*(nums[nums[i]]%n));
- after this nums[0] will be 5 + 6*(4%6) = 5 + 24 = 29;
- now for next index calculation we might need the original value of num[0] which can be obtain by num[0]%6 = 29%6 = 5;
- if we want to get just the new value of num[0], we can get it by num[0]/6 = 29/6 = 4
O(N) Time and O(N) Space solution
- We will use inbuilt function
vector.insert()
to concatenate the arrays.
DFS
UnionFind
- If path exist between 2 nodes then they are in same component.
- We can use UnionFind to know if source and destination are in same component.