diff --git a/C++palindrome_1.cpp b/C++palindrome_1.cpp new file mode 100644 index 000000000..5b7d391fc --- /dev/null +++ b/C++palindrome_1.cpp @@ -0,0 +1,41 @@ + +#include +using namespace std; + +//palindrome check for a number!!! +/*This program reverses an integer (entered by the user) using while loop. Then, if statement is +used to check whether the reversed number is equal to the original number or not.*/ + +//driver code +int main() +{ + int n, num, digit, rev = 0; + + cout << "Enter a positive number of your choice: "; //taking input from the user + cin >> num; + + n = num; + + do //do while loop to reverse the digits of the number + { + digit = num % 10; + rev = (rev * 10) + digit; + num = num / 10; + } while (num != 0); + + cout << " The reverse of the number is: " << rev << endl; + + if (n== rev) //checking if the reversed number is same as the original input number + cout << " The number is a palindrome!!!"; + else + cout << " The number is not a palindrome!!!"; + + return 0; +} +/*test cases- + 1.input-121 + output-The number is a palindrome!!! + 2.input-34567 + output-The number is not a palindrome!!! + 3.input-123454321 + output-The number is a palindrome!!! */ diff --git a/C++palindrome_2.cpp b/C++palindrome_2.cpp new file mode 100644 index 000000000..2cc7c00d0 --- /dev/null +++ b/C++palindrome_2.cpp @@ -0,0 +1,93 @@ +//c++ program to check whether a string is a palindrome or not + +/*Find the length of the string say l. Now, find the mid as mid = l / 2. +Push all the elements till mid into the stack i.e. str[0…mid-1]. +If the length of the string is odd then neglect the middle character. +Till the end of the string, keep popping elements from the stack and compare +it with the current character i.e. string[i]. +If there is mismatch then the string is not a palindrome. +If all the elements match then the string is a palindrome.*/ + +#include +#include +#include +#include +#include + +using namespace std; + +char* stack; +int top = -1; + +// push function +void push(char el) +{ + stack[++top] = el; +} + +// pop function +char pop() +{ + return stack[top--]; +} + +// Function that returns 1 +// if str is a palindrome +int isPalindrome(char str[]) +{ + int length = strlen(str); + + // Allocating the memory for the stack + stack = (char*)malloc(length * sizeof(char)); + + // Finding the mid + int i, mid = length / 2; + + for (i = 0; i < mid; i++) { + push(str[i]); + } + + // Checking if the length of the string + // is odd, if odd then neglect the + // middle character + if (length % 2 != 0) { + i++; + } + + // While not the end of the string + while (str[i] != '\0') { + char el = pop(); + + // If the characters differ then the + // given string is not a palindrome + if (el!= str[i]) + return 0; + i++; + } + + return 1; +} + +// Driver code +int main() +{ + char str[100]; + cout<<"Enter a string of your choice: "<>str; + if (isPalindrome(str)) { + cout<<"Yes"< 3 7 + / \ / \ \ / \ + 2 4 6 8 4 6 8 +2) Node to be deleted has only one child: Copy the child to the node and delete the child + + 5 5 + / \ delete(3) / \ + 3 7 ---------> 4 7 + \ / \ / \ + 4 6 8 6 8 +3) Node to be deleted has two children: Find inorder successor of the node. Copy contents of the inorder +successor to the node and delete the inorder successor. Note that inorder predecessor can also be used. + + + 5 6 + / \ delete(5) / \ + 4 7 ---------> 4 7 + / \ \ + 6 8 8 +The important thing to note is, inorder successor is needed only when right child is not empty. +In this particular case, inorder successor can be obtained by finding the minimum value in right child +of the node.*/ + +//code +#include +using namespace std; + +struct node { + int key; + struct node *l,*r; +}; + +//Function to create a new BST node + +struct node* new_node(int value) +{ + struct node* temp + = (struct node*)malloc(sizeof(struct node)); + temp->key = value; + temp->l = temp->r = NULL; + return temp; +} + +//Inorder traversal of BST +void inorder(struct node* root) +{ + if (root != NULL) { + inorder(root->l); + cout << root->key; + inorder(root->r); + } +} + +//Function to insert a new node with given key in BST// + +struct node* insert(struct node* node, int key) +{ + //If the tree is empty, return a new node // + if (node == NULL) + return new_node(key); + + // Otherwise, recur down the tree // + if (key < node->key) + node->l = insert(node->l, key); + else + node->r = insert(node->r, key); + + //return the (unchanged) node pointer // + return node; +} +//function to search for the minimum value in the BST +struct node* minValueNode(struct node* node) +{ + struct node* present = node; + + /* loop down to find the leftmost leaf */ + while (present && present->l!= NULL) + present = present->l; + + return present; +} + +/* Given a binary search tree and a key, this function +deletes the key and returns the new root */ +struct node* deleteNode(struct node* root, int key) +{ + // base case + if (root == NULL) + return root; + + // If the key to be deleted is + // smaller than the root's + // key, then it lies in left subtree + if (key < root->key) + root->l = deleteNode(root->l, key); + + // If the key to be deleted is + // greater than the root's + // key, then it lies in right subtree + else if (key > root->key) + root->r = deleteNode(root->r, key); + + // if key is same as root's key, then this is the node to be deleted + else { + // node with only one child or no child + if (root->l == NULL) { + struct node* temp = root->r; + free(root); + return temp; + } + else if (root->r == NULL) { + struct node* temp = root->l; + free(root); + return temp; + } + + // node with two children: Get the inorder successor + // (smallest in the right subtree) + struct node* temp = minValueNode(root->r); + + // Copy the inorder successor's content to this node + root->key = temp->key; + + // Delete the inorder successor + root->r = deleteNode(root->r, temp->key); + } + return root; +} + +// Driver Code +int main() +{ + struct node* root = NULL; + root = insert(root, 1); + root = insert(root, 5); + root = insert(root, 2); + root = insert(root, 7); + root = insert(root, 14); + root = insert(root, 9); + + cout << "Inorder traversal of the given tree \n"; + inorder(root); + + cout << "\nDelete 2\n"; + root = deleteNode(root,2); + cout << "Inorder traversal of the modified tree \n"; + inorder(root); + + cout << "\nDelete 5\n"; + root = deleteNode(root, 5); + cout << "Inorder traversal of the modified tree \n"; + inorder(root); + + cout << "\nDelete 14\n"; + root = deleteNode(root, 14); + cout << "Inorder traversal of the modified tree \n"; + inorder(root); + + return 0; +} + +/*test cases- +1.input-2 + output-157914 + +2.input-5 + output-17914 + +3.input-14 + output-179 */ diff --git a/longest bitonic subsequence.cpp b/longest bitonic subsequence.cpp new file mode 100644 index 000000000..857ed631c --- /dev/null +++ b/longest bitonic subsequence.cpp @@ -0,0 +1,67 @@ +/* +LONGEST BITONIC SUBSEQUENCE- +It is the longest subsequence in which array is sorted such that +it is first in increasing order to the peak and then in decreasing +order . + +*/ + +/* lbs() length of the Longest Bitonic Subsequence in + arr[] of size n. The function mainly creates two temporary arrays + lis[] and lds[] and returns the maximum lis[i] + lds[i] - 1. + + lis[i] -Longest Increasing subsequence ending with arr[i] + lds[i] -Longest decreasing subsequence starting with arr[i] +*/ +int lbs( int arr[], int n ) +{ + int i, j; + + /* Allocate memory for lis[] and initialize LIS values as 1 for + all indexes */ + int *lis = new int[n]; + for (i = 0; i < n; i++) + lis[i] = 1; + + /* Compute LIS values from left to right */ + for (i = 1; i < n; i++) + for (j = 0; j < i; j++) + if (arr[i] > arr[j] && lis[i] < lis[j] + 1) + lis[i] = lis[j] + 1; + + /* Allocate memory for lds and initialize LDS values for + all indexes */ + int *lds = new int [n]; + for (i = 0; i < n; i++) + lds[i] = 1; + + /* Compute LDS values from right to left */ + for (i = n-2; i >= 0; i--) + for (j = n-1; j > i; j--) + if (arr[i] > arr[j] && lds[i] < lds[j] + 1) + lds[i] = lds[j] + 1; + + + /* Return the maximum value of lis[i] + lds[i] - 1*/ + int max = lis[0] + lds[0] - 1; + for (i = 1; i < n; i++) + if (lis[i] + lds[i] - 1 > max) + max = lis[i] + lds[i] - 1; + return max; +} + +/* Driver program to test above function */ +int main() +{ + + int arr[]; + int n; + cout<<"enter the number of array element you want to enter"; + cin>>n; + for(int i=0;i>arr[i]; + } + cout<<"Length of LBS is "<