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]; 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; +} 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 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 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 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 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 + +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<