Skip to content

Commit d4d2ea3

Browse files
🔥 Day 30
1 parent 4c36386 commit d4d2ea3

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
## Week 5 🚧
4848

4949
1. [Pancake Sort](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/553/week-5-august-29th-august-31st/3441/) ➡️ [CPP Solution](Week5/pancakeSort.cpp)
50+
2. [Largest Component Size by Common Factor](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/553/week-5-august-29th-august-31st/3442/) ➡️ [CPP Solution](Week5/largestComponentSize.cpp)
5051

5152
## Other Challenges 💪
5253

Week5/largestComponentSize.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class dsu{
2+
public:
3+
vector<int> arr;
4+
dsu(int n):arr(n){
5+
for(int i=0;i<n;i++){
6+
arr[i]=i;
7+
}
8+
}
9+
10+
void merge(int a, int b){
11+
arr[find(arr[a])]=arr[find(arr[b])];
12+
}
13+
14+
int find(int x){
15+
if(arr[x]!=x){
16+
arr[x]=find(arr[x]);
17+
}
18+
return arr[x];
19+
}
20+
};
21+
22+
class Solution {
23+
24+
public:
25+
int largestComponentSize(vector<int>& A) {
26+
int n=*max_element(A.begin(),A.end());
27+
dsu DSU(n+1);
28+
for(auto x:A){
29+
int temp=sqrt(x);
30+
for(int k=2;k<=temp;k++){
31+
if(x%k==0){
32+
DSU.merge(x,k);
33+
DSU.merge(x,x/k);
34+
}
35+
}
36+
}
37+
unordered_map<int,int> m;
38+
int res=1;
39+
for(auto v:A){
40+
m[DSU.find(v)]+=1;
41+
res=max(res,m[DSU.find(v)]);
42+
}
43+
return res;
44+
}
45+
};

0 commit comments

Comments
 (0)