From 0006460425b2597d0b43d6ad6386d4d85844d789 Mon Sep 17 00:00:00 2001 From: Pratham Kumar <115283906+PrathamKumar125@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:30:31 +0530 Subject: [PATCH 1/8] Create Longest_Common_Subsequence.py --- .../Longest_Common_Subsequence.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 code/dynamic_programming/src/longest_common_subsequence_substring/Longest_Common_Subsequence.py diff --git a/code/dynamic_programming/src/longest_common_subsequence_substring/Longest_Common_Subsequence.py b/code/dynamic_programming/src/longest_common_subsequence_substring/Longest_Common_Subsequence.py new file mode 100644 index 0000000000..b4fca639c9 --- /dev/null +++ b/code/dynamic_programming/src/longest_common_subsequence_substring/Longest_Common_Subsequence.py @@ -0,0 +1,47 @@ +# The longest common subsequence in Python + + +# Function to find lcs_algo +def lcs_algo(S1, S2, m, n): + L = [[0 for x in range(n+1)] for x in range(m+1)] + + # Building the mtrix in bottom-up way + for i in range(m+1): + for j in range(n+1): + if i == 0 or j == 0: + L[i][j] = 0 + elif S1[i-1] == S2[j-1]: + L[i][j] = L[i-1][j-1] + 1 + else: + L[i][j] = max(L[i-1][j], L[i][j-1]) + + index = L[m][n] + + lcs_algo = [""] * (index+1) + lcs_algo[index] = "" + + i = m + j = n + while i > 0 and j > 0: + + if S1[i-1] == S2[j-1]: + lcs_algo[index-1] = S1[i-1] + i -= 1 + j -= 1 + index -= 1 + + elif L[i-1][j] > L[i][j-1]: + i -= 1 + else: + j -= 1 + + # Printing the sub sequences + print("S1 : " + S1 + "\nS2 : " + S2) + print("LCS: " + "".join(lcs_algo)) + + +S1 = "ACADB" +S2 = "CBDA" +m = len(S1) +n = len(S2) +lcs_algo(S1, S2, m, n) From d7e6cb32c092f9def2cb95873cf7544c1ced9c8e Mon Sep 17 00:00:00 2001 From: Pratham Kumar <115283906+PrathamKumar125@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:39:24 +0530 Subject: [PATCH 2/8] Create Longest_Common_Subsequence.java --- .../Longest_Common_Subsequence.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 code/dynamic_programming/src/longest_common_subsequence_substring/Longest_Common_Subsequence.java diff --git a/code/dynamic_programming/src/longest_common_subsequence_substring/Longest_Common_Subsequence.java b/code/dynamic_programming/src/longest_common_subsequence_substring/Longest_Common_Subsequence.java new file mode 100644 index 0000000000..611b259ae8 --- /dev/null +++ b/code/dynamic_programming/src/longest_common_subsequence_substring/Longest_Common_Subsequence.java @@ -0,0 +1,55 @@ +// The longest common subsequence in Java + +class LCS_ALGO { + static void lcs(String S1, String S2, int m, int n) { + int[][] LCS_table = new int[m + 1][n + 1]; + + // Building the mtrix in bottom-up way + for (int i = 0; i <= m; i++) { + for (int j = 0; j <= n; j++) { + if (i == 0 || j == 0) + LCS_table[i][j] = 0; + else if (S1.charAt(i - 1) == S2.charAt(j - 1)) + LCS_table[i][j] = LCS_table[i - 1][j - 1] + 1; + else + LCS_table[i][j] = Math.max(LCS_table[i - 1][j], LCS_table[i][j - 1]); + } + } + + int index = LCS_table[m][n]; + int temp = index; + + char[] lcs = new char[index + 1]; + lcs[index] = '\0'; + + int i = m, j = n; + while (i > 0 && j > 0) { + if (S1.charAt(i - 1) == S2.charAt(j - 1)) { + lcs[index - 1] = S1.charAt(i - 1); + + i--; + j--; + index--; + } + + else if (LCS_table[i - 1][j] > LCS_table[i][j - 1]) + i--; + else + j--; + } + + // Printing the sub sequences + System.out.print("S1 : " + S1 + "\nS2 : " + S2 + "\nLCS: "); + for (int k = 0; k <= temp; k++) + System.out.print(lcs[k]); + System.out.println(""); + } + + public static void main(String[] args) { + String S1 = "ACADB"; + String S2 = "CBDA"; + int m = S1.length(); + int n = S2.length(); + lcs(S1, S2, m, n); + } +} From 36363722fc3cf3454ba8066e35f1b536f9a4848e Mon Sep 17 00:00:00 2001 From: Pratham Kumar <115283906+PrathamKumar125@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:02:54 +0530 Subject: [PATCH 3/8] Create Dining_Philosophers_Problem.c --- .../Dining_Philosophers_Problem.c | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 code/operating_system/src/concurrency/dining_philosophers/Dining_Philosophers_Problem.c diff --git a/code/operating_system/src/concurrency/dining_philosophers/Dining_Philosophers_Problem.c b/code/operating_system/src/concurrency/dining_philosophers/Dining_Philosophers_Problem.c new file mode 100644 index 0000000000..d314bbd161 --- /dev/null +++ b/code/operating_system/src/concurrency/dining_philosophers/Dining_Philosophers_Problem.c @@ -0,0 +1,99 @@ +#include +#include +#include + +#define N 5 +#define THINKING 2 +#define HUNGRY 1 +#define EATING 0 +#define LEFT (phnum + 4)%N +#define RIGHT (phnum + 1)%N + +int state[N]; +int phil[N] = {0, 1, 2, 3, 4}; + +sem_t mutex; +sem_t S[N]; + +void test(int phnum) +{ + if (state[phnum] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) + { + //state that eating + state[phnum] = EATING; + sleep(2); + printf("Philosopher %d takes fork %d and %d\n", phnum + 1, LEFT + 1, phnum + 1); + printf("Philosopher %d is Eating\n", phnum + 1); + + //sem_post(&S[phnum]) has no effect during takefork + //used to wake up hungry philosophers during putfork + sem_post(&S[phnum]); + } +} + +//take up chopsticks +void take_fork(int phnum) +{ + //access critical section + sem_wait(&mutex); + //state that hungry + state[phnum] = HUNGRY; + printf("Philosopher %d is Hungry\n", phnum + 1); + + //eat if neighbours are not eating + test(phnum); + //leave critical section + sem_post(&mutex); + + //if unable to eat wait to be signalled + sem_wait(&S[phnum]); + sleep(1); +} + + +//put down chopsticks +void put_fork(int phnum) +{ + //access critical section + sem_wait(&mutex); + // state that thinking + state[phnum] = THINKING; + printf("Philosopher %d putting", phnum + 1); + printf(" fork %d and %d down\n", LEFT + 1, phnum + 1); + printf("Philosopher %d is thinking\n", phnum + 1); + test(LEFT); + test(RIGHT); + //leave critical section + sem_post(&mutex); +} + +void *philospher(void *num) +{ + while(1) + { + int *i = num; + sleep(1); + take_fork(*i); + sleep(0); + put_fork(*i); + } +} + +int main() +{ + int i; + pthread_t thread_id[N]; + // initialize the semaphores + sem_init(&mutex, 0, 1); + for(i = 0; i < N; i++) + sem_init(&S[i], 0, 0); + for(i = 0; i < N; i++) + { + // create philosopher processes + pthread_create(&thread_id[i], NULL, philospher, &phil[i]); + printf("Philosopher %d is thinking\n", i + 1); + } + for(i = 0; i < N; i++) + pthread_join(thread_id[i], NULL); + return 0; +} From 47eae36397450064c9e9aca1d69099a757f63165 Mon Sep 17 00:00:00 2001 From: Pratham Kumar <115283906+PrathamKumar125@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:18:28 +0530 Subject: [PATCH 4/8] Create reversing_singly_linked_list.cpp --- .../reversing_singly_linked_list.cpp | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 code/data_structures/src/Linked_List/reversing_singly_linked_list.cpp diff --git a/code/data_structures/src/Linked_List/reversing_singly_linked_list.cpp b/code/data_structures/src/Linked_List/reversing_singly_linked_list.cpp new file mode 100644 index 0000000000..6531abfeb8 --- /dev/null +++ b/code/data_structures/src/Linked_List/reversing_singly_linked_list.cpp @@ -0,0 +1,85 @@ +#include + +//Represent a node of the singly linked list +struct node{ + int data; + struct node *next; +}; + +//Represent the head and tail of the singly linked list +struct node *head, *tail = NULL; + +//addNode() will add a new node to the list +void addNode(int data) { + //Create a new node + struct node *newNode = (struct node*)malloc(sizeof(struct node)); + newNode->data = data; + newNode->next = NULL; + + //Checks if the list is empty + if(head == NULL) { + //If list is empty, both head and tail will point to new node + head = newNode; + tail = newNode; + } + else { + //newNode will be added after tail such that tail's next will point to newNode + tail->next = newNode; + //newNode will become new tail of the list + tail = newNode; + } +} + +//reverse() will the reverse the order of the list +void reverse(struct node *current) { + //Checks if list is empty + if(head == NULL) { + printf("List is empty\n"); + return; + } + else{ + //Checks if the next node is null, if yes then prints it. + if(current->next == NULL) { + printf("%d ", current->data); + return; + } + //Recursively calls the reverse function + reverse(current->next); + printf("%d ", current->data); + } +} + +//display() will display all the nodes present in the list +void display() { + //Node current will point to head + struct node *current = head; + + if(head == NULL) { + printf("List is empty\n"); + return; + } + while(current != NULL) { + //Prints each node by incrementing pointer + printf("%d ", current->data); + current = current->next; + } + printf("\n"); +} + +int main() +{ + //Add nodes to the list + addNode(1); + addNode(2); + addNode(3); + addNode(4); + + printf("Original List: \n"); + display(); + + printf("Reversed List: \n"); + //Print reversed list + reverse(head); + + return 0; +} From bc10ab26d7c029dea022f2354ca7d763a8788122 Mon Sep 17 00:00:00 2001 From: Pratham Kumar <115283906+PrathamKumar125@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:26:39 +0530 Subject: [PATCH 5/8] kruskal_minimum_spanning_tree.cpp --- .../kruskal_minimum_spanning_tree.cpp | 136 +++++++++++------- 1 file changed, 84 insertions(+), 52 deletions(-) diff --git a/code/graph_algorithms/src/kruskal_minimum_spanning_tree/kruskal_minimum_spanning_tree.cpp b/code/graph_algorithms/src/kruskal_minimum_spanning_tree/kruskal_minimum_spanning_tree.cpp index 16b7438f5f..0de6516e7e 100644 --- a/code/graph_algorithms/src/kruskal_minimum_spanning_tree/kruskal_minimum_spanning_tree.cpp +++ b/code/graph_algorithms/src/kruskal_minimum_spanning_tree/kruskal_minimum_spanning_tree.cpp @@ -1,61 +1,93 @@ +// Kruskal's algorithm in C++ + +#include #include #include -#include -// Part of Cosmos by OpenGenus Foundation +using namespace std; + +#define edge pair + +class Graph { + private: + vector > G; // graph + vector > T; // mst + int *parent; + int V; // number of vertices/nodes in graph + public: + Graph(int V); + void AddWeightedEdge(int u, int v, int w); + int find_set(int i); + void union_set(int u, int v); + void kruskal(); + void print(); +}; +Graph::Graph(int V) { + parent = new int[V]; + + //i 0 1 2 3 4 5 + //parent[i] 0 1 2 3 4 5 + for (int i = 0; i < V; i++) + parent[i] = i; -int n, dj[100], rank[100]; //disjoint set -int findset(int a) -{ - if (dj[a] != a) - return dj[a] = findset(dj[a]); - else - return a; + G.clear(); + T.clear(); } -bool sameset(int a, int b) -{ - return findset(a) == findset(b); +void Graph::AddWeightedEdge(int u, int v, int w) { + G.push_back(make_pair(w, edge(u, v))); } -void unionset(int a, int b) -{ - int x = findset(a), y = findset(b); - if (rank[x] > rank[y]) - dj[y] = x; - else - { - dj[x] = y; - if (rank[x] == rank[y]) - rank[y]++; - } +int Graph::find_set(int i) { + // If i is the parent of itself + if (i == parent[i]) + return i; + else + // Else if i is not the parent of itself + // Then i is not the representative of his set, + // so we recursively call Find on its parent + return find_set(parent[i]); } -int main() -{ - using namespace std; - int e, u, v, w; - vector< pair>> edge; //(weight, two vertices that the edge connects) - for (int i = 0; i < n; i++) - { - dj[i] = i; - ::rank[i] = 0; - } - cout << "Input Number of Edges" << endl; - cin >> e; - cout << "Input Edges (weight and then two vertices that the edge connects)" << endl; - for (int i = 0; i < e; i++) - { - cin >> u >> v >> w; //u,v,w are just temporary variables - edge.push_back({u, {v, w}}); - } - sort(edge.begin(), edge.end()); //sort by edge weight - int mst = 0; - for (int i = 0; i < e; i++) - { - int x = edge[i].second.first, y = edge[i].second.second; - if (!sameset(x, y)) - { - mst += edge[i].first; - unionset(x, y); - } +void Graph::union_set(int u, int v) { + parent[u] = parent[v]; +} +void Graph::kruskal() { + int i, uRep, vRep; + sort(G.begin(), G.end()); // increasing weight + for (i = 0; i < G.size(); i++) { + uRep = find_set(G[i].second.first); + vRep = find_set(G[i].second.second); + if (uRep != vRep) { + T.push_back(G[i]); // add to tree + union_set(uRep, vRep); } - cout << mst << endl; + } +} +void Graph::print() { + cout << "Edge :" + << " Weight" << endl; + for (int i = 0; i < T.size(); i++) { + cout << T[i].second.first << " - " << T[i].second.second << " : " + << T[i].first; + cout << endl; + } +} +int main() { + Graph g(6); + g.AddWeightedEdge(0, 1, 4); + g.AddWeightedEdge(0, 2, 4); + g.AddWeightedEdge(1, 2, 2); + g.AddWeightedEdge(1, 0, 4); + g.AddWeightedEdge(2, 0, 4); + g.AddWeightedEdge(2, 1, 2); + g.AddWeightedEdge(2, 3, 3); + g.AddWeightedEdge(2, 5, 2); + g.AddWeightedEdge(2, 4, 4); + g.AddWeightedEdge(3, 2, 3); + g.AddWeightedEdge(3, 4, 3); + g.AddWeightedEdge(4, 2, 4); + g.AddWeightedEdge(4, 3, 3); + g.AddWeightedEdge(5, 2, 2); + g.AddWeightedEdge(5, 4, 3); + g.kruskal(); + g.print(); + return 0; } From 1ca6be440d628370a1943c8f33d19f0e2229480a Mon Sep 17 00:00:00 2001 From: Pratham Kumar <115283906+PrathamKumar125@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:41:04 +0530 Subject: [PATCH 6/8] Create Co-Prime_Numbers.c --- .../src/coprime_numbers/Co-Prime_Numbers.c | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 code/mathematical_algorithms/src/coprime_numbers/Co-Prime_Numbers.c diff --git a/code/mathematical_algorithms/src/coprime_numbers/Co-Prime_Numbers.c b/code/mathematical_algorithms/src/coprime_numbers/Co-Prime_Numbers.c new file mode 100644 index 0000000000..b4c107906b --- /dev/null +++ b/code/mathematical_algorithms/src/coprime_numbers/Co-Prime_Numbers.c @@ -0,0 +1,31 @@ +#include + +// Function to calculate the GCD of two numbers +int gcd(int a, int b) { + if (b == 0) + return a; + return gcd(b, a % b); +} + +// Function to check if two numbers are coprime +int areCoprime(int num1, int num2) { + return (gcd(num1, num2) == 1); +} + +int main() { + int num1, num2; + + printf("Enter the first number: "); + scanf("%d", &num1); + + printf("Enter the second number: "); + scanf("%d", &num2); + + if (areCoprime(num1, num2)) { + printf("%d and %d are coprime.\n", num1, num2); + } else { + printf("%d and %d are not coprime.\n", num1, num2); + } + + return 0; +} From dd81c01edbd39eb2290bad3eb61de4f3fd5073df Mon Sep 17 00:00:00 2001 From: Pratham Kumar <115283906+PrathamKumar125@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:46:49 +0530 Subject: [PATCH 7/8] Create Partition_Array_for_Maximum_Sum.cpp --- .../Partition_Array_for_Maximum_Sum.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 code/online_challenges/src/leetcode/Partition Array for Maximum Sum/Partition_Array_for_Maximum_Sum.cpp diff --git a/code/online_challenges/src/leetcode/Partition Array for Maximum Sum/Partition_Array_for_Maximum_Sum.cpp b/code/online_challenges/src/leetcode/Partition Array for Maximum Sum/Partition_Array_for_Maximum_Sum.cpp new file mode 100644 index 0000000000..702a6ca687 --- /dev/null +++ b/code/online_challenges/src/leetcode/Partition Array for Maximum Sum/Partition_Array_for_Maximum_Sum.cpp @@ -0,0 +1,40 @@ + +#include +using namespace std; + +// Function to find the maximum sum after partitioning the array. +int maxSumAfterPartitioning(vector& num, int k) { + int n = num.size(); + + // Create a DP array to store the maximum sum. + vector dp(n + 1, 0); + + // Iterate through the array from right to left. + for (int ind = n - 1; ind >= 0; ind--) { + int len = 0; + int maxi = INT_MIN; + int maxAns = INT_MIN; + + // Loop through the next k elements (or remaining elements if k is smaller). + for (int j = ind; j < min(ind + k, n); j++) { + len++; + maxi = max(maxi, num[j]); + int sum = len * maxi + dp[j + 1]; + maxAns = max(maxAns, sum); + } + + // Store the computed maximum sum in the DP array. + dp[ind] = maxAns; + } + + // The maximum sum after partitioning the entire array is stored in dp[0]. + return dp[0]; +} + +int main() { + vector num = {1, 15, 7, 9, 2, 5, 10}; + int k = 3; + int maxSum = maxSumAfterPartitioning(num, k); + cout << "The maximum sum is: " << maxSum << "\n"; + return 0; +} From 7da76648b8d192d75e4a4150d5cc6c4b111878a1 Mon Sep 17 00:00:00 2001 From: Pratham Kumar <115283906+PrathamKumar125@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:54:05 +0530 Subject: [PATCH 8/8] Create Peak_index_in_mountain_array.cpp --- .../Peak_index_in_mountain_array.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 code/online_challenges/src/leetcode/Peak index in a mountain array/Peak_index_in_mountain_array.cpp diff --git a/code/online_challenges/src/leetcode/Peak index in a mountain array/Peak_index_in_mountain_array.cpp b/code/online_challenges/src/leetcode/Peak index in a mountain array/Peak_index_in_mountain_array.cpp new file mode 100644 index 0000000000..702a6ca687 --- /dev/null +++ b/code/online_challenges/src/leetcode/Peak index in a mountain array/Peak_index_in_mountain_array.cpp @@ -0,0 +1,40 @@ + +#include +using namespace std; + +// Function to find the maximum sum after partitioning the array. +int maxSumAfterPartitioning(vector& num, int k) { + int n = num.size(); + + // Create a DP array to store the maximum sum. + vector dp(n + 1, 0); + + // Iterate through the array from right to left. + for (int ind = n - 1; ind >= 0; ind--) { + int len = 0; + int maxi = INT_MIN; + int maxAns = INT_MIN; + + // Loop through the next k elements (or remaining elements if k is smaller). + for (int j = ind; j < min(ind + k, n); j++) { + len++; + maxi = max(maxi, num[j]); + int sum = len * maxi + dp[j + 1]; + maxAns = max(maxAns, sum); + } + + // Store the computed maximum sum in the DP array. + dp[ind] = maxAns; + } + + // The maximum sum after partitioning the entire array is stored in dp[0]. + return dp[0]; +} + +int main() { + vector num = {1, 15, 7, 9, 2, 5, 10}; + int k = 3; + int maxSum = maxSumAfterPartitioning(num, k); + cout << "The maximum sum is: " << maxSum << "\n"; + return 0; +}