From bab945ae26975447d732c1c96790cd45d82aa133 Mon Sep 17 00:00:00 2001 From: shubham Date: Sat, 3 Jun 2023 21:22:59 +0530 Subject: [PATCH 1/3] Added Swap Alternate Numbers Program --- Cpp/Swap_Alternate_Numbers.cpp | 44 +++++++ longest_palindromic_subsequence.cpp | 178 ++++++++++++++-------------- 2 files changed, 133 insertions(+), 89 deletions(-) create mode 100644 Cpp/Swap_Alternate_Numbers.cpp diff --git a/Cpp/Swap_Alternate_Numbers.cpp b/Cpp/Swap_Alternate_Numbers.cpp new file mode 100644 index 0000000..7f0588b --- /dev/null +++ b/Cpp/Swap_Alternate_Numbers.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +void alternate(int arr[], int n) +{ + for (int i = 0; i < n; i += 2) + { + if (i + 1 < n) + { + // With Swap Function + // swap(arr[i], arr[i+1]); + + // Without Swap Function + int temp; + temp = arr[i + 1]; + arr[i + 1] = arr[i]; + arr[i] = temp; + } + } +} + +int printArray(int arr[], int n) +{ + for (int i = 0; i < n; i++) + { + cout << arr[i] << " "; + } + cout << endl; + return 0; +} + +int main() +{ + int arr[6] = {1, 2, 3, 4, 5, 6}; + int brr[5] = {11, 12, 13, 14, 15}; + + alternate(arr, 6); + alternate(brr, 5); + + printArray(arr, 6); + printArray(brr, 5); + + return 0; +} \ No newline at end of file diff --git a/longest_palindromic_subsequence.cpp b/longest_palindromic_subsequence.cpp index 73720be..f3f7f21 100644 --- a/longest_palindromic_subsequence.cpp +++ b/longest_palindromic_subsequence.cpp @@ -1,90 +1,90 @@ -/** - * @file - * @brief Program to find the Longest Palindormic - * Subsequence of a string - * - * @details - * [Palindrome](https://en.wikipedia.org/wiki/Palindrome) string sequence of - * characters which reads the same backward as forward - * [Subsequence](https://en.wikipedia.org/wiki/Subsequence) is a sequence that - * can be derived from another sequence by deleting some or no elements without - * changing the order of the remaining elements. - - * @author [Anjali Jha](https://github.com/anjali1903) - */ - -#include -#include -#include -#include - -/** - * Function that returns the longest palindromic - * subsequence of a string - */ -std::string lps(std::string a) { - std::string b = a; - reverse(b.begin(), b.end()); - int m = a.length(); - std::vector > res(m + 1); - - // Finding the length of the longest - // palindromic subsequence and storing - // in a 2D array in bottoms-up manner - for (int i = 0; i <= m; i++) { - for (int j = 0; j <= m; j++) { - if (i == 0 || j == 0) { - res[i][j] = 0; - } else if (a[i - 1] == b[j - 1]) { - res[i][j] = res[i - 1][j - 1] + 1; - } else { - res[i][j] = std::max(res[i - 1][j], res[i][j - 1]); - } - } - } - // Length of longest palindromic subsequence - int idx = res[m][m]; - // Creating string of index+1 length - std::string ans(idx + 1, '\0'); - int i = m, j = m; - - // starting from right-most bottom-most corner - // and storing them one by one in ans - while (i > 0 && j > 0) { - // if current characters in a and b are same - // then it is a part of the ans - if (a[i - 1] == b[j - 1]) { - ans[idx - 1] = a[i - 1]; - i--; - j--; - idx--; - } - // If they are not same, find the larger of the - // two and move in that direction - else if (res[i - 1][j] > res[i][j - 1]) { - i--; - } else { - j--; - } - } - - return ans; -} - -/** Test function */ -void test() { - // lps("radar") return "radar" - assert(lps("radar") == "radar"); - // lps("abbcbaa") return "abcba" - assert(lps("abbcbaa") == "abcba"); - // lps("bbbab") return "bbbb" - assert(lps("bbbab") == "bbbb"); -} - -/** - * Main Function - */ -int main() { - test(); // execute the tests - return 0; +/** + * @file + * @brief Program to find the Longest Palindormic + * Subsequence of a string + * + * @details + * [Palindrome](https://en.wikipedia.org/wiki/Palindrome) string sequence of + * characters which reads the same backward as forward + * [Subsequence](https://en.wikipedia.org/wiki/Subsequence) is a sequence that + * can be derived from another sequence by deleting some or no elements without + * changing the order of the remaining elements. + + * @author [Anjali Jha](https://github.com/anjali1903) + */ + +#include +#include +#include +#include + +/** + * Function that returns the longest palindromic + * subsequence of a string + */ +std::string lps(std::string a) { + std::string b = a; + reverse(b.begin(), b.end()); + int m = a.length(); + std::vector > res(m + 1); + + // Finding the length of the longest + // palindromic subsequence and storing + // in a 2D array in bottoms-up manner + for (int i = 0; i <= m; i++) { + for (int j = 0; j <= m; j++) { + if (i == 0 || j == 0) { + res[i][j] = 0; + } else if (a[i - 1] == b[j - 1]) { + res[i][j] = res[i - 1][j - 1] + 1; + } else { + res[i][j] = std::max(res[i - 1][j], res[i][j - 1]); + } + } + } + // Length of longest palindromic subsequence + int idx = res[m][m]; + // Creating string of index+1 length + std::string ans(idx + 1, '\0'); + int i = m, j = m; + + // starting from right-most bottom-most corner + // and storing them one by one in ans + while (i > 0 && j > 0) { + // if current characters in a and b are same + // then it is a part of the ans + if (a[i - 1] == b[j - 1]) { + ans[idx - 1] = a[i - 1]; + i--; + j--; + idx--; + } + // If they are not same, find the larger of the + // two and move in that direction + else if (res[i - 1][j] > res[i][j - 1]) { + i--; + } else { + j--; + } + } + + return ans; +} + +/** Test function */ +void test() { + // lps("radar") return "radar" + assert(lps("radar") == "radar"); + // lps("abbcbaa") return "abcba" + assert(lps("abbcbaa") == "abcba"); + // lps("bbbab") return "bbbb" + assert(lps("bbbab") == "bbbb"); +} + +/** + * Main Function + */ +int main() { + test(); // execute the tests + return 0; } \ No newline at end of file From 0186ed31309fdc19b5dd4f969b76b7cbeff599db Mon Sep 17 00:00:00 2001 From: shubham Date: Sat, 3 Jun 2023 21:28:04 +0530 Subject: [PATCH 2/3] Added Reverse an Array Program --- Cpp/Reverse_an_Array.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Cpp/Reverse_an_Array.cpp diff --git a/Cpp/Reverse_an_Array.cpp b/Cpp/Reverse_an_Array.cpp new file mode 100644 index 0000000..4c1e605 --- /dev/null +++ b/Cpp/Reverse_an_Array.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +void reverse(int arr[], int n) +{ + int start = 0; + int end = n - 1; + while (start <= end) + { + swap(arr[start], arr[end]); + start++; + end--; + } +} + +int printArray(int arr[], int n) +{ + for (int i = 0; i < n; i++) + { + cout << arr[i] << " "; + } + cout << endl; + return 0; +} + +int main() +{ + int arr[6] = {1, 2, 3, 4, 5, 6}; + int brr[5] = {10, 9, 8, 7, 6}; + + reverse(arr, 6); + reverse(brr, 5); + + printArray(arr, 6); + printArray(brr, 5); + + return 0; +} \ No newline at end of file From 3f91b8a3321dc37e12aee7648327d773739e63f8 Mon Sep 17 00:00:00 2001 From: shubham Date: Sat, 3 Jun 2023 21:30:34 +0530 Subject: [PATCH 3/3] Added Linear Search Problem --- Cpp/Linear_Search.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Cpp/Linear_Search.cpp diff --git a/Cpp/Linear_Search.cpp b/Cpp/Linear_Search.cpp new file mode 100644 index 0000000..3b71178 --- /dev/null +++ b/Cpp/Linear_Search.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +bool linearSearch(int arr[], int n, int key) +{ + for (int i = 0; i < n; i++) + { + if (arr[i] == key) + { + return 1; + } + } + return 0; +} + +int main() +{ + int arr[10] = {5, -1, 6, 23, 789, 98, -9, -98, 89, 10}; + cout << "Enter any key value: " << endl; + int key; + cin >> key; + + bool found = linearSearch(arr, 10, key); + + if (found) + { + cout << "Key is Present" << endl; + } + else + { + cout << "Key is absent" << endl; + } + + return 0; +} \ No newline at end of file