how to construct the graph for 1st question in coding round2
@Chotu-v8cАй бұрын
Did you get it?
@csec0565Ай бұрын
from collections import deque # Function to add an edge between two nodes in the adjacency list def add_edge(adj, u, v): adj[u].append(v) adj[v].append(u) # Function to generate the figure/grid structure def generate_grid(n): fig = [[] for _ in range(n+1)] # Create a list of lists to hold grid structure start = 1 # For the growing half for i in range(1, n//2+1): for j in range(1, i*2): fig[i].append(start) start += 1 # For the shrinking half for i in range(n//2+1, n + 1): for j in range(1, (n-i+1)*2): fig[i].append(start) start += 1 return fig # Function to add edges in each row of the grid def add_row_edges(fig, adj, n): for i in range(1, n+1): for j in range(len(fig[i]) - 1): add_edge(adj, fig[i][j], fig[i][j+1]) # Function to add vertical edges between specific rows in the grid def add_diagonal_edges(fig, adj, n): # For the growing half for i in range(1, n//2): for j in range(len(fig[i])): if not((i%2) ^ fig[i][j] % 2): add_edge(adj, fig[i][j], fig[i][j] + i * 2) # For the middle row for j in range(len(fig[n//2])): if not((n//2 % 2) ^ fig[n//2][j] % 2): add_edge(adj, fig[n//2][j], fig[n//2][j] + n - 1) # For the shrinking half for i in range(n, n//2, -1): # Corrected to reverse order for j in range(len(fig[i])): if not((i%2) ^ fig[i][j] % 2): add_edge(adj, fig[i][j], fig[i][j] - (n - i + 1) * 2) # BFS function to calculate distances from node k def bfs(adj, k, tot_n): q = deque([k]) dist = [-1] * (tot_n+1) dist[k] = 0 while q: node = q.popleft() for neighbor in adj[node]: if dist[neighbor] == -1: dist[neighbor] = dist[node] + 1 q.append(neighbor) return dist # Input handling and main execution n, k = map(int, input().split()) fig = generate_grid(n) tot_n = sum(len(row) for row in fig) # Total number of nodes adj = [[] for _ in range(tot_n+1)] # Add edges between nodes add_row_edges(fig, adj, n) add_diagonal_edges(fig, adj, n) # Perform BFS and print the distances distances = bfs(adj, k, tot_n) print(distances[1:])
@QuirkNinja-md8in27 күн бұрын
how many days it took to get notification after interview. i have completed my interview on 16th .
@U-DAY27 күн бұрын
@@QuirkNinja-md8in it would take more than a week
@AnyTimeMovie-forU27 күн бұрын
hii i got shortlisted for the 2nd round virtual interview .... can you tell how the interview will be and what questions were asked
@U-DAY27 күн бұрын
www.linkedin.com/in/sri-varshith-singampalli-8a38a9253? You can ping him