From 89398c30a184b526e42ecf1d679a8837aec64c28 Mon Sep 17 00:00:00 2001 From: vedic-kalra <85125045+vedic-kalra@users.noreply.github.com> Date: Thu, 14 Oct 2021 01:57:12 +0530 Subject: [PATCH 1/8] Longest Common Subsequence CPP Implementation here i am providing two solutions to the following problems using dynamic programming one is Top-Down DP and another is Bottom-Up DP this paticular questions corresponds to the folloing question on leetcode leetcode-1143 https://leetcode.com/problems/longest-common-subsequence/ here i have explained the question in a gradual optimal manner --- longest_common_subsequence_using_dp.cpp | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 longest_common_subsequence_using_dp.cpp diff --git a/longest_common_subsequence_using_dp.cpp b/longest_common_subsequence_using_dp.cpp new file mode 100644 index 0000000..cf862e8 --- /dev/null +++ b/longest_common_subsequence_using_dp.cpp @@ -0,0 +1,73 @@ +//here i am providing two solutions to the following problems using dynamic programming +//one is Top-Down DP and another is Bottom-Up DP +// this paticular questions corresponds to the folloing question on leetcode +//leetcode-1143 https://leetcode.com/problems/longest-common-subsequence/ +//here i have explained the question in a gradual optimal manner +//------------------------------------------------------------------------------------------------------------------ +//brute force approach +class Solution { + public int longestCommonSubsequence(String text1, String text2) { + return longestCommonSubsequence(text1, text2, 0, 0); + } + + private int longestCommonSubsequence(String text1, String text2, int i, int j) { + if (i == text1.length() || j == text2.length()) + return 0; + if (text1.charAt(i) == text2.charAt(j)) + return 1 + longestCommonSubsequence(text1, text2, i + 1, j + 1); + else + return Math.max( + longestCommonSubsequence(text1, text2, i + 1, j), + longestCommonSubsequence(text1, text2, i, j + 1) + ); + } +} +//------------------------------------------------------------------------------------------------------------------ +/* Top-down DP +We might use memoization to overcome overlapping subproblems. +Since there are two changing values, i.e. i and j in the recursive +function longestCommonSubsequence, we might apply a two-dimensional array.*/ +class Solution { + private Integer[][] dp; + public int longestCommonSubsequence(String text1, String text2) { + dp = new Integer[text1.length()][text2.length()]; + return longestCommonSubsequence(text1, text2, 0, 0); + } + + private int longestCommonSubsequence(String text1, String text2, int i, int j) { + if (i == text1.length() || j == text2.length()) + return 0; + + if (dp[i][j] != null) + return dp[i][j]; + + if (text1.charAt(i) == text2.charAt(j)) + return dp[i][j] = 1 + longestCommonSubsequence(text1, text2, i + 1, j + 1); + else + return dp[i][j] = Math.max( + longestCommonSubsequence(text1, text2, i + 1, j), + longestCommonSubsequence(text1, text2, i, j + 1) + ); + } +} +//------------------------------------------------------------------------------------------------------------------ +/*Bottom-up DP +For every i in text1, j in text2, we will choose one of the following two options: + +if two characters match, length of the common subsequence would be 1 plus the +length of the common subsequence till the i-1 andj-1 indexes +if two characters doesn't match, we will take the longer by either skipping i or j indexes*/ +class Solution { + public int longestCommonSubsequence(String text1, String text2) { + int[][] dp = new int[text1.length() + 1][text2.length() + 1]; + for (int i = 1; i <= text1.length(); i++) { + for (int j = 1; j <= text2.length(); j++) { + if (text1.charAt(i - 1) == text2.charAt(j - 1)) + dp[i][j] = 1 + dp[i - 1][j - 1]; + else + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + return dp[text1.length()][text2.length()]; + } +} \ No newline at end of file From 2a2fa78d3b147a0c3a287d25996b14e48d037c36 Mon Sep 17 00:00:00 2001 From: Kshitiz Raj Date: Thu, 14 Oct 2021 12:11:17 +0530 Subject: [PATCH 2/8] Add Zig Zag Pattern Program --- ZigZagPattern.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 ZigZagPattern.cpp diff --git a/ZigZagPattern.cpp b/ZigZagPattern.cpp new file mode 100644 index 0000000..e345f7f --- /dev/null +++ b/ZigZagPattern.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + +int main() +{ + + int n; + cin >> n; + + for (int i = 1; i <= 3; i++) + { + if (i == 1) + { + int c = 0; + for (int j = 1; j <= n; j++) + { + if (j == 1 || j == 2) + { + cout << " "; + } + else + { + if (c % 4 == 0) + { + cout << "* "; + } + else + { + cout << " "; + } + c++; + } + } + } + else if (i == 2) + { + for (int j = 1; j <= n; j++) + { + if (j % 2 == 0) + { + cout << "* "; + } + else + { + cout << " "; + } + } + } + else + { + for (int j = 1; j <= n; j++) + { + if (j % 4 == 1) + { + cout << "* "; + } + else + { + cout << " "; + } + } + } + cout << endl; + } + + return 0; +} \ No newline at end of file From dbdcf3dbc9fd469cda393f404f39c2ac2e9bcda1 Mon Sep 17 00:00:00 2001 From: Aryan Date: Thu, 14 Oct 2021 17:11:07 +0530 Subject: [PATCH 3/8] Added All The File Of Longest Common Subsequence --- LongestCommonSubsequence.cpp | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 LongestCommonSubsequence.cpp diff --git a/LongestCommonSubsequence.cpp b/LongestCommonSubsequence.cpp new file mode 100644 index 0000000..6b6a574 --- /dev/null +++ b/LongestCommonSubsequence.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +using namespace std; + +//finding max value +int max(int a, int b) +{ + return (a > b) ? a : b; +} + +//finding LCS +int LongestCommonSubsequence(char *str1, char *str2, int x, int y) +{ + if (x == 0 || y == 0) + { + return 0; + } + if (str1[x - 1] == str2[y - 1]) + { + return (1 + LongestCommonSubsequence(str1, str2, x - 1, y - 1)); + } + else + { + return max(LongestCommonSubsequence(str1, str2, x, y - 1), LongestCommonSubsequence(str1, str2, x - 1, y)); + } +} + +//main function +int main() +{ + char str1[] = "cantonment"; + char str2[] = "longatone"; + int x = strlen(str1); + int y = strlen(str2); + cout << "\nLength of Longest Common Subsequence From Both The Strings Is " << LongestCommonSubsequence(str1, str2, x, y) << endl; + return 0; +} \ No newline at end of file From 4d00773ee10c01defb4f4eb23dfe43535c89a548 Mon Sep 17 00:00:00 2001 From: SAGAR SATAPATHY Date: Fri, 15 Oct 2021 03:40:09 +0530 Subject: [PATCH 4/8] added stack linklist added stack linklist for hacktoberfest in cpp --- stack_linkedlist.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 stack_linkedlist.cpp diff --git a/stack_linkedlist.cpp b/stack_linkedlist.cpp new file mode 100644 index 0000000..5022a8b --- /dev/null +++ b/stack_linkedlist.cpp @@ -0,0 +1,82 @@ +/* C++ program to implement basic stack +operations */ +#include + +using namespace std; + +#define MAX 1000 + +class Stack { + int top; + +public: + int a[MAX]; // Maximum size of Stack + + Stack() { top = -1; } + bool push(int x); + int pop(); + int peek(); + bool isEmpty(); +}; + +bool Stack::push(int x) +{ + if (top >= (MAX - 1)) { + cout << "Stack Overflow"; + return false; + } + else { + a[++top] = x; + cout << x << " pushed into stack\n"; + return true; + } +} + +int Stack::pop() +{ + if (top < 0) { + cout << "Stack Underflow"; + return 0; + } + else { + int x = a[top--]; + return x; + } +} +int Stack::peek() +{ + if (top < 0) { + cout << "Stack is Empty"; + return 0; + } + else { + int x = a[top]; + return x; + } +} + +bool Stack::isEmpty() +{ + return (top < 0); +} + +// Driver program to test above functions +int main() +{ + class Stack s; + s.push(10); + s.push(20); + s.push(30); + cout << s.pop() << " Popped from stack\n"; + //print all elements in stack : + cout<<"Elements present in stack : "; + while(!s.isEmpty()) + { + // print top element in stack + cout< Date: Mon, 18 Oct 2021 18:05:52 +0530 Subject: [PATCH 5/8] Insertion_Sort.cpp Added "the" in front of number in line 41 --- Insertion_Sort.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Insertion_Sort.cpp b/Insertion_Sort.cpp index bf8c6ed..769a5f7 100644 --- a/Insertion_Sort.cpp +++ b/Insertion_Sort.cpp @@ -38,7 +38,7 @@ void insertionSort(int arr[], int n) { int main() { int n; - cout<<"Enter number of elements : "; + cout<<"Enter the number of elements : "; cin>>n; int arr[n]; From 9cecedcc084b01c81126d5d3dd6767cc5c8bdfc6 Mon Sep 17 00:00:00 2001 From: PratikPrakhar <84954224+PratikPrakhar@users.noreply.github.com> Date: Tue, 19 Oct 2021 19:06:29 +0530 Subject: [PATCH 6/8] Added program of Stack using linked list --- Stack using linked list.c | 106 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 Stack using linked list.c diff --git a/Stack using linked list.c b/Stack using linked list.c new file mode 100644 index 0000000..c3f9864 --- /dev/null +++ b/Stack using linked list.c @@ -0,0 +1,106 @@ +#include +#include + +struct Node +{ + int data; + struct Node *next; +}; + +struct Node *head = NULL; + +void push(int value) +{ + struct Node *n = (struct Node *)malloc(sizeof(struct Node *)); + if (head == NULL) + { + head = n; + n->data = value; + n->next = NULL; + return; + } + + n->next = head; + n->data = value; + head = n; +} + +void pop() +{ + struct Node *temp = head; + if (head == NULL) + { + printf("\nStack is empty.\n"); + return; + } + + head = head->next; + temp->next = NULL; +} + +void peek() +{ + if (head == NULL) + { + printf("\nStack is empty.\n"); + return; + } + + printf("The top element is: %d\n", head->data); +} + +void display() +{ + struct Node *temp = head; + if (head == NULL) + { + printf("\nStack is empty.\n"); + return; + } + printf("\nElements of stack are:\n"); + while (temp != NULL) + { + printf("%d\n", temp->data); + temp = temp->next; + } +} + +int main() +{ + while (1) + { + printf("\n=======Menu=======\n"); + printf("1. Push\n"); + printf("2. Pop\n"); + printf("3. Peek\n"); + printf("4. Display\n"); + printf("5. Exit\n\n"); + int c, n; + scanf("%d", &c); + switch (c) + { + case 1: + printf("Enter the element to be inserted: "); + scanf("%d", &n); + push(n); + break; + case 2: + pop(); + break; + case 3: + peek(); + break; + case 4: + display(); + break; + case 5: + exit(0); + break; + + default: + printf("Please select a valid option."); + } + } + + return 0; +} \ No newline at end of file From 5d70b380e3b37522b4fbe31fffcf76307afa1c86 Mon Sep 17 00:00:00 2001 From: VikasArsul <92970538+VikasArsul@users.noreply.github.com> Date: Fri, 22 Oct 2021 11:28:25 +0530 Subject: [PATCH 7/8] Solution for Rat-In-Maze problem in C++ --- rat_in_maze.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 rat_in_maze.cpp diff --git a/rat_in_maze.cpp b/rat_in_maze.cpp new file mode 100644 index 0000000..808a791 --- /dev/null +++ b/rat_in_maze.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + +bool isSafe(int** arr, int x, int y, int n){ + + if( x>n; + + int** arr = new int*[n]; + for(int i=0; i>arr[i][j]; + } + } + + int** solArr = new int*[n]; + for(int i=0; i Date: Sat, 23 Oct 2021 09:48:38 +0530 Subject: [PATCH 8/8] Create Kadanes_Algo --- Kadanes_Algo | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Kadanes_Algo diff --git a/Kadanes_Algo b/Kadanes_Algo new file mode 100644 index 0000000..d788079 --- /dev/null +++ b/Kadanes_Algo @@ -0,0 +1,20 @@ +#include +using namespace std; +int main() +{ + int n; + cin >> n; + int ms = 0, cs = 0; + int arr[n]; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + cs += arr[i]; + if (cs < 0) + cs = 0; + else if (cs >= ms) + ms = cs; + } + cout << ms << endl; + return 0; +}