Sunday, 28 Nov 2021
Category: graph-connectivity

import java.util.ArrayList;class GFG {                  boolean isSafe(int v, int graph[][],                   ArrayList path,                   int pos)    {                                if (graph[path.get(pos – 1)][v]            == 0)            return false;                          for (int i = 0; i < pos; i++)            if (path.get(i) == v)                return false;                          return true;    }              boolean hasCycle;              void hamCycle(int graph[][])    {                        hasCycle = false;                  ArrayList path            = new ArrayList();        path.add(0);                          boolean[] visited            = new boolean[graph.length];          for (int i = 0;             i < visited.length; i++)            visited[i] = false;          visited[0] = true;                          FindHamCycle(graph, 1, path,                     visited);          if (!hasCycle) {                                                  System.out.println(                "No Hamiltonian […]

Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect squareGiven a positive integer N, the task is to find the number of unique permutations of first N natural numbers having sum of the adjacent elements equal to a perfect square.Examples:Input: N = 17Output: 2Explanation:Following permutations have sum of […]

#include using namespace std;  int minTrioDegree(int N,                  vector& Edges){            int degree[N + 1] = { 0 };              int adj[N + 1][N + 1] = { 0 };              for (int i = 0; i < Edges.size(); i++) {                          int u = Edges[i][0];        int v = Edges[i][1];                          adj[u][v] = adj[u][v] = 1;                          degree[u]++;        degree[v]++;    }          int ans = INT_MAX;          for (int i = 1; i

#include using namespace std;  void find_union(    vector G1,    vector G2){        map added;          vector G;              for (auto p : G1) {          string a = get(p);                  int b = get(p);        int c = get(p);                          G.push_back(            make_tuple(a, b, c));        added[a] = { b, c };    }              for (auto p : G2) {          string a = get(p);        int b = get(p);        int c = get(p);          pair x = { b, c };        pair y = { c, b […]

#include using namespace std;    int maximumAND(int arr[], int n, int m) {               int tot = 1 > i) & 1) {                                  if (count == 0) {                                          andans = arr[i];                 }                 else {                                                               andans = andans & arr[i];                 }                                                      count++;             }         }                              if […]

#include using namespace std;    void addEdge(vector& adj,              int u, int v) {     adj[u].push_back(v);     adj[v].push_back(u); }    vector BFS(     int N, int source,     vector adjlist) {               int dist[N + 1];        vector vertex_set;                  memset(dist, -1, sizeof dist);                  vertex_set.assign(2, vector(0));             queue Q;             Q.push(source);     dist […]