diff --git a/kruskal.CPP.txt b/kruskal.CPP.txt new file mode 100644 index 00000000..10e9cba6 --- /dev/null +++ b/kruskal.CPP.txt @@ -0,0 +1,189 @@ +// C++ program for Kruskal's algorithm to find Minimum Spanning Tree +// of a given connected, undirected and weighted graph +#include +#include +#include + +// a structure to represent a weighted edge in graph +struct Edge +{ + int src, dest, weight; +}; + +// a structure to represent a connected, undirected +// and weighted graph +struct Graph +{ + // V-> Number of vertices, E-> Number of edges + int V, E; + + // graph is represented as an array of edges. + // Since the graph is undirected, the edge + // from src to dest is also edge from dest + // to src. Both are counted as 1 edge here. + struct Edge* edge; +}; + +// Creates a graph with V vertices and E edges +struct Graph* createGraph(int V, int E) +{ + struct Graph* graph = new Graph; + graph->V = V; + graph->E = E; + + graph->edge = new Edge[E]; + + return graph; +} + +// A structure to represent a subset for union-find +struct subset +{ + int parent; + int rank; +}; + +// A utility function to find set of an element i +// (uses path compression technique) +int find(struct subset subsets[], int i) +{ + // find root and make root as parent of i + // (path compression) + if (subsets[i].parent != i) + subsets[i].parent = find(subsets, subsets[i].parent); + + return subsets[i].parent; +} + +// A function that does union of two sets of x and y +// (uses union by rank) +void Union(struct subset subsets[], int x, int y) +{ + int xroot = find(subsets, x); + int yroot = find(subsets, y); + + // Attach smaller rank tree under root of high + // rank tree (Union by Rank) + if (subsets[xroot].rank < subsets[yroot].rank) + subsets[xroot].parent = yroot; + else if (subsets[xroot].rank > subsets[yroot].rank) + subsets[yroot].parent = xroot; + + // If ranks are same, then make one as root and + // increment its rank by one + else + { + subsets[yroot].parent = xroot; + subsets[xroot].rank++; + } +} + +// Compare two edges according to their weights. +// Used in qsort() for sorting an array of edges +int myComp(const void* a, const void* b) +{ + struct Edge* a1 = (struct Edge*)a; + struct Edge* b1 = (struct Edge*)b; + return a1->weight > b1->weight; +} + +// The main function to construct MST using Kruskal's algorithm +void KruskalMST(struct Graph* graph) +{ + int V = graph->V; + struct Edge result[V]; // Tnis will store the resultant MST + int e = 0; // An index variable, used for result[] + int i = 0; // An index variable, used for sorted edges + + // Step 1: Sort all the edges in non-decreasing + // order of their weight. If we are not allowed to + // change the given graph, we can create a copy of + // array of edges + qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp); + + // Allocate memory for creating V ssubsets + struct subset *subsets = + (struct subset*) malloc( V * sizeof(struct subset) ); + + // Create V subsets with single elements + for (int v = 0; v < V; ++v) + { + subsets[v].parent = v; + subsets[v].rank = 0; + } + + // Number of edges to be taken is equal to V-1 + while (e < V - 1) + { + // Step 2: Pick the smallest edge. And increment + // the index for next iteration + struct Edge next_edge = graph->edge[i++]; + + int x = find(subsets, next_edge.src); + int y = find(subsets, next_edge.dest); + + // If including this edge does't cause cycle, + // include it in result and increment the index + // of result for next edge + if (x != y) + { + result[e++] = next_edge; + Union(subsets, x, y); + } + // Else discard the next_edge + } + + // print the contents of result[] to display the + // built MST + printf("Following are the edges in the constructed MST\n"); + for (i = 0; i < e; ++i) + printf("%d -- %d == %d\n", result[i].src, result[i].dest, + result[i].weight); + return; +} + +// Driver program to test above functions +int main() +{ + /* Let us create following weighted graph + 10 + 0--------1 + | \ | + 6| 5\ |15 + | \ | + 2--------3 + 4 */ + int V = 4; // Number of vertices in graph + int E = 5; // Number of edges in graph + struct Graph* graph = createGraph(V, E); + + + // add edge 0-1 + graph->edge[0].src = 0; + graph->edge[0].dest = 1; + graph->edge[0].weight = 10; + + // add edge 0-2 + graph->edge[1].src = 0; + graph->edge[1].dest = 2; + graph->edge[1].weight = 6; + + // add edge 0-3 + graph->edge[2].src = 0; + graph->edge[2].dest = 3; + graph->edge[2].weight = 5; + + // add edge 1-3 + graph->edge[3].src = 1; + graph->edge[3].dest = 3; + graph->edge[3].weight = 15; + + // add edge 2-3 + graph->edge[4].src = 2; + graph->edge[4].dest = 3; + graph->edge[4].weight = 4; + + KruskalMST(graph); + + return 0; +} \ No newline at end of file diff --git a/solution to common problems/CODECHEF_DIVSUBS b/solution to common problems/CODECHEF_DIVSUBS new file mode 100644 index 00000000..654c0cc8 --- /dev/null +++ b/solution to common problems/CODECHEF_DIVSUBS @@ -0,0 +1,70 @@ +//Divisible Subset +//https://www.codechef.com/status/DIVSUBS +//Divsub is a famous programming question. The idea is pigeonhole princliple. +//Example: 3 4 3 5 2 3 +//Naive approach is by exponential time finding pairs with 1 element only then 2 element only and so on. +//Any such problem can be illustrated as (1+x^3)(1+x^4)(1+x^3)(1+x^5)(1+x^2)(1+x^3) solving this will give terms having powers of all subsets we just need to apply % N = 0. +//It can be solved in O(NlogN) by Fast Fourier Transform or simply in O(N^2) +//https://www.youtube.com/watch?v=QQQpOa3aXew +//Itterate all array elements and apply % N and record it in the array of vector below. +//0 1 2 3 4 5 +// 0 +//Then 4 +//0 1 2 3 4 5 +// 1 +//Then in 4 to 3 so 4 + 3 % N +//0 1 2 3 4 5 +// 0,1 0 1 +//and so on. +//a[] = 3 4 3 5 2 3 +//b[] = 0 3 7 10 15 17 20 +//b[] = 0 3 1 4 3 5 2 +//temp[] = 2 6 1,4 3 5 +//This is pigeonhole senerio in every case any temp arr will have two element. end - start i.e. 4 - 1 = 3 so 3 elements that are 2nd 3rd and 4th (4 + 3 + 5) % 6 = 0 +#include +using namespace std; + +#define MOD 1000000000 +typedef long long ll; +typedef unsigned long long ull; + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + int t; + cin >> t; + while(t--) + { + int n; + cin >> n; + int a[n]; + for (int i = 0; i < n; i++) cin >> a[i]; + + ll b[n + 1]; + b[0] = 0; + vector temp[n]; + temp[b[0] % n].push_back(0); + for(int i=1; i<=n; i++) + { + b[i] = b[i - 1] + a[i - 1]; + temp[b[i] % n].push_back(i); + } + ll start, end; + for(int i=0; i=2 ) + { + start = temp[i][0]; + end = temp[i][1]; + } + } + cout << (end - start) << endl; + for(int i = start + 1; i <= end; i++) + cout << i << " "; + + cout << endl; + } + return 0; +} diff --git a/solution to common problems/SPOJ_FAVDICE.cpp b/solution to common problems/SPOJ_FAVDICE.cpp new file mode 100644 index 00000000..25a4f673 --- /dev/null +++ b/solution to common problems/SPOJ_FAVDICE.cpp @@ -0,0 +1,22 @@ +//Favourite Dice +//https://www.spoj.com/problems/FAVDICE/ +#include +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + int t; + cin >> t; + while(t--) + { + double n; + cin >> n; + double ans = 0; + for (int i = 1; i <= n; i++) ans += (double)1/i; + ans *= n; + cout << ans << endl; + } + return 0; +} diff --git a/solution to common problems/SPOJ_FIBOSUM.cpp b/solution to common problems/SPOJ_FIBOSUM.cpp new file mode 100644 index 00000000..36827f4d --- /dev/null +++ b/solution to common problems/SPOJ_FIBOSUM.cpp @@ -0,0 +1,76 @@ +//Fibonacci Sum +//https://www.spoj.com/problems/FIBOSUM/ +//This question wants the user to calculate Fibonacci with in a range. A naive approach will be to simply use the +//relation F(N) = F(N - 1) + F(N - 2) to find the nth fibonacci and the itterate over the range. A better approach will be to derrive +//a recurence relation of the fibonacci series. Refer this link for mathematics behind it https://www.youtube.com/watch?v=WT_TGxQrV1k&t=214s +//Even now itterating to find the sum for fibbonaci will give us TLE because of large constraints. +//So there's an observation to get TLE fixed. 1, 1, 2, 3, 5, 8, 13, 21, 34. The observation is Sum till Nth fibonacci is F(n+2) - 1 +//This observation is true throughout. Now calculating sum within range from n to m will be sum till m minus sum till n + +#include +using namespace std; + +typedef long long int lli; +#define mat(x, y, name) vector< vector > name (x, vector(y)); + +lli MOD = 1000000007; + +vector< vector > matMul(vector< vector > A, vector< vector > B) +{ + vector< vector > C(A.size(), vector(B[0].size())); + for (int i = 0; i < A.size(); i++) + { + for (int j = 0; j < B[0].size(); j++) + { + C[i][j] = 0; + for (int k = 0; k < B.size(); k++) + C[i][j] = (C[i][j] + ((A[i][k] * B[k][j]) % MOD)) % MOD; + } + } + return C; +} + +vector< vector > matPow(vector< vector > A, int p) +{ + if (p == 1) + return A; + if (p&1) + return matMul(A, matPow(A, p-1)); + else + { + vector< vector > C = matPow(A, p/2); + return matMul(C, C); + } +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + mat(2, 1, F); + mat(2, 2, T); + T[0][0] = 0; + T[0][1] = 1; + T[1][0] = 1; + T[1][1] = 1; + F[0][0] = 1; + F[1][0] = 1; + int t; + cin >> t; + while(t--) + { + lli n, m; + cin >> n >> m; + lli minSum = (n == 0) ? 0 : ((matMul(matPow(T, n), F)[0][0]) - 1) % MOD; + lli maxSum = (m == 0) ? 0 : ((matMul(matPow(T, m+1), F)[0][0]) - 1) % MOD; + lli ans = (maxSum - minSum) % MOD; + if (ans < 0) + { + ans += MOD; + ans = ans % MOD; + } + cout << ans << endl; + } + return 0; +} diff --git a/solution to common problems/between two sets.cpp b/solution to common problems/between two sets.cpp new file mode 100644 index 00000000..184b82bc --- /dev/null +++ b/solution to common problems/between two sets.cpp @@ -0,0 +1,63 @@ + +//https://www.hackerrank.com/challenges/between-two-sets/problem + +#include +#include +#include +#include +#include +using namespace std; + + +int main() { + int n,m,count=0,flag=0; + int arr1[10],arr2[10]; + cin>>n; + cin>>m; + for(int i=0;i>arr1[i]; + } + for(int i=0;i>arr2[i]; + } + int max=arr1[0]; + int min=arr2[0]; + for(int i=0;imax) + max=arr1[i]; + } + for(int i=0;i +#include +#include +#include +#include +using namespace std; + + +int main() { + int q; + int arr[100][3]; + cin>>q; + for(int i=0;i>arr[i][j]; + } + } + for(int i=0;iabs(arr[i][1]-arr[i][2])) + cout<<"Cat B\n"; + else if(abs(arr[i][0]-arr[i][2]) +#include +//https://www.hackerrank.com/challenges/electronics-shop/problem + +#include +#include +#include +using namespace std; + + +int main() { + long int s,n,m,arrn[100000],arrm[100000]; + long int ans=-1; + cin>>s; + cin>>n; + cin>>m; + long int i=0; + for(;i>arrn[i]; + } + long int j=0; + for(;j>arrm[j]; + } + long int k=0; + long int l=0; + for(;kans&&(arrn[k]+arrm[l])<=s) + ans=arrn[k]+arrm[l]; + } + } + cout< +#include +#include +#include +#include +using namespace std; + +int main() { + int arr[100],total,count=0; + cin>>total; + for(int i=0;i>arr[i]; + for(int j=0;j