Forwarded from allcoding1_official
def construct_graph(n):
graph = [[] for _ in range(n+1)]
for i in range(2, n+1):
for j in range(2*i, n+1, i):
graph[i].append(j)
graph[j].append(i)
return graph
def dfs(node, graph, visited):
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs(neighbor, graph, visited)
def count_groups(n):
graph = construct_graph(n)
visited = set()
groups = 0
for i in range(2, n+1):
if i not in visited:
groups += 1
dfs(i, graph, visited)
return groups
Social Network
Python
Telegram:- @allcoding1
graph = [[] for _ in range(n+1)]
for i in range(2, n+1):
for j in range(2*i, n+1, i):
graph[i].append(j)
graph[j].append(i)
return graph
def dfs(node, graph, visited):
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs(neighbor, graph, visited)
def count_groups(n):
graph = construct_graph(n)
visited = set()
groups = 0
for i in range(2, n+1):
if i not in visited:
groups += 1
dfs(i, graph, visited)
return groups
Social Network
Python
Telegram:- @allcoding1
Forwarded from allcoding1_official
int countNecklaces(int maxPearls, int startMagnificence, int endMagnificence) {
int count = 0;
for (int pearls = 1; pearls <= maxPearls; pearls++) {
for (int magnificence = startMagnificence; magnificence <= endMagnificence; magnificence++) {
if (pearls == 1) {
count++;
} else {
int prevCount = count;
for (int prevMagnificence = startMagnificence; prevMagnificence <= magnificence; prevMagnificence++) {
count += prevCount;
}
}
}
}
return count;
}
C++
Charles and Neckless
Telegram:- @allcoding1_official
int count = 0;
for (int pearls = 1; pearls <= maxPearls; pearls++) {
for (int magnificence = startMagnificence; magnificence <= endMagnificence; magnificence++) {
if (pearls == 1) {
count++;
} else {
int prevCount = count;
for (int prevMagnificence = startMagnificence; prevMagnificence <= magnificence; prevMagnificence++) {
count += prevCount;
}
}
}
}
return count;
}
C++
Charles and Neckless
Telegram:- @allcoding1_official
👍8