Skip to content

Instantly share code, notes, and snippets.

View Ch-sriram's full-sized avatar
🎯
Solving problems, one step at a time

Chandrabhatta Sriram Ch-sriram

🎯
Solving problems, one step at a time
View GitHub Profile
@Ch-sriram
Ch-sriram / connected-components-bfs.cpp
Created October 31, 2020 17:22
Count Number of Connected Components [TC: O(V+E); SC: O(V+E)]
#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);
}
@Ch-sriram
Ch-sriram / print-adj-list.cpp
Created October 31, 2020 17:03
Print Adjacency List [TC: O(V+E); SC: O(1)]
// 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;
@Ch-sriram
Ch-sriram / max-xor-subset.cpp
Last active October 31, 2020 04:35
Maximum XOR Subset [TC: O(32*(2N)); SC: O(1)]
// 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)
@Ch-sriram
Ch-sriram / max-array-xor.cpp
Last active October 31, 2020 04:16
Maximum Array XOR [TC: O(32*(2N)); SC: O(1)]
// 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)
@Ch-sriram
Ch-sriram / max-AND-value.cpp
Created October 29, 2020 22:39
Maximum AND Value [TC: O(32*N); SC: O(1)]
// 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
@Ch-sriram
Ch-sriram / num-sparse-or-not.cpp
Created October 29, 2020 20:45
Number is Sparse or Not [TC: O(logN); SC: O(1)]
// 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;
}
@Ch-sriram
Ch-sriram / swap-odd-even-bits.cpp
Created October 29, 2020 20:20
Swap All Odd & Even Bits [TC: O(logN); SC: O(1)]
// 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;
}
@Ch-sriram
Ch-sriram / bit-difference.cpp
Created October 29, 2020 19:51
Bit Difference [TC: O(logN); SC: O(1)]
// 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;
}
@Ch-sriram
Ch-sriram / missing-number-xor.cpp
Last active October 28, 2020 22:06
Missing Number in Array [TC: O(N); SC: O(1)]
// 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)
@Ch-sriram
Ch-sriram / two-numbers-odd-occurrences.cpp
Last active October 28, 2020 21:00
Two Numbers with Odd Occurrences [TC: O(N); SC: O(1)]
// 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();