Category : breadth-first-search

#include <bits/stdc++.h> using namespace std; int left_arr[1024], right_arr[1024]; void swapping(int root, int k) { queue<int> q; q.push(root); int lvl = 0; while (!q.empty()) { lvl++; int size = q.size(); while (size–) { int p = q.front(); q.pop(); if (lvl % k == 0) swap(left_arr[p], right_arr[p]); if (left_arr[p] != -1) q.push(left_arr[p]); if (right_arr[p] != -1) q.push(right_arr[p]); ..

Read more

To find shortest path in undirected weighted graph I was comparing BFS and dijkstra’s algo to understand why we need priority queue. I wrote some code modifying BFS to find the shortest path to all nodes in a given graph. Problem link :- https://practice.geeksforgeeks.org/problems/implementing-dijkstra-set-1-adjacency-matrix/1# The below code got accepted on GeeksForGeeks that I wrote instead ..

Read more

This a question that I got in a coding exam and couldn’t solve it at that time. The question states that, You are currently at cell (1, 1) of an N X M grid. There is a rule that decides how you can move in the grid to reach the position (N, M). The rule ..

Read more