This file contains 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 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 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 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 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 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 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 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; | |
} |
This file contains 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/missing-number-in-array1416/1 | |
int MissingNumber(vector<int>& ar, int n) { | |
// Find XOR of all integers from 1 to n | |
int XORn = 0; | |
for(int i = 1; i <= n; ++i) | |
XORn ^= i; | |
// Find XOR of all integers in ar[] | |
int XORar = 0; | |
for(int i = 0; i < (int)ar.size(); ++i) |
This file contains 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/two-numbers-with-odd-occurrences/0 | |
// Good Explanation: https://www.geeksforgeeks.org/find-the-two-numbers-with-odd-occurences-in-an-unsorted-array/ | |
#include <vector> | |
#include <iostream> | |
using namespace std; | |
void printTwoOddOccurring(vector<int> &ar) { | |
const int n = ar.size(); |