This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <vector> | |
| #include <iostream> | |
| #include <algorithm> | |
| using namespace std; | |
| void DFSRec(vector<int> *G, int src, vector<bool> &visited) { | |
| visited[src] = true; | |
| for(int u: G[src]) | |
| if(!visited[u]) | |
| DFSRec(G, u, visited); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Problem Link: https://practice.geeksforgeeks.org/problems/bfs-traversal-of-graph/1/ | |
| /* Function to do BFS of graph | |
| * g[]: adj list of the graph | |
| * N : number of vertices | |
| */ | |
| vector<int> bfs(vector<int> g[], int N) { | |
| vector<int> vertices; | |
| vector<bool> visited(N+1, false); | |
| queue<int> Q; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <queue> | |
| #include <vector> | |
| #include <iostream> | |
| #include <algorithm> | |
| using namespace std; | |
| void addEdge(vector<int> *adj, int u, int v) { | |
| adj[u].push_back(v); | |
| adj[v].push_back(u); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Problem Link: https://practice.geeksforgeeks.org/problems/print-adjacency-list-1587115620/1/ | |
| // The Graph structure is as folows | |
| // Function to print graph | |
| // adj: array of vectors to represent graph | |
| // V: number of vertices | |
| void printGraph(vector<int> adj[], int V) { | |
| for(int u = 0; u < V; ++u) { | |
| if((int)adj[u].size() == 0) cout << u; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Problem Link: https://practice.geeksforgeeks.org/problems/maximum-subset-xor/1/ | |
| // Good Explanation: https://www.geeksforgeeks.org/find-maximum-subset-xor-given-set/ | |
| int maxXor(int arr[], int n) { | |
| int maxEle = INT_MIN; | |
| for(int i = 0; i < n; ++i) | |
| maxEle = max(maxEle, arr[i]); | |
| int MSB = 0; | |
| for(int i = 31; i >= 0; --i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Problem Link: https://practice.geeksforgeeks.org/problems/maximum-subset-xor/1/ | |
| // Good Explanation: https://www.geeksforgeeks.org/find-maximum-subset-xor-given-set/ | |
| int maxSubarrayXOR(int arr[], int n) { | |
| int maxEle = INT_MIN; | |
| for(int i = 0; i < n; ++i) | |
| maxEle = max(maxEle, arr[i]); | |
| int MSB = 0; | |
| for(int i = 31; i >= 0; --i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Problem Link: https://practice.geeksforgeeks.org/problems/maximum-and-value2637/1 | |
| int checkBit(int pattern, int arr[], int n) { | |
| int count = 0; | |
| for(int i = 0; i < n; ++i) | |
| if((pattern & arr[i]) == pattern) | |
| ++count; | |
| return count; | |
| } | |
| // Function for finding maximum and value pair |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Problem Link: https://practice.geeksforgeeks.org/problems/number-is-sparse-or-not-1587115620/1/ | |
| bool isSparse(int n) { | |
| for(int i = 0; i < 32; ++i) | |
| if(((n >> i) & 3) == 3) | |
| return false; | |
| return true; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Problem Link: https://practice.geeksforgeeks.org/problems/swap-all-odd-and-even-bits-1587115621/1/ | |
| unsigned int swapBits(unsigned int n) { | |
| for(uint32_t i = 0; i < 32; i += 2) { | |
| uint32_t requiredBits = ((n >> i) & 3) ^ 3; | |
| if(requiredBits == 1 || requiredBits == 2) | |
| n ^= (3 << i); | |
| } | |
| return n; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Problem Link: https://practice.geeksforgeeks.org/problems/bit-difference-1587115620/1/ | |
| int countSetBits(int n) { | |
| int count = 0; | |
| while(n) { | |
| ++count; | |
| n &= (n-1); | |
| } | |
| return count; | |
| } |