├── BinarySearch ├── RotationCount.cpp ├── findAllPairs.cpp ├── findFirstOccurance.cpp └── findMissingElement.cpp ├── BinarySearchTree ├── binaryTree.cpp ├── convertLeverOrder.cpp └── findKth.cpp ├── BinaryTree ├── ConmpleteInorder.cpp ├── ConstructTree.cpp ├── PrintBinaryTreeWithLevelOrderTraversal.cpp ├── countNumberOfLeaf.cpp ├── createATreeFromArray.cpp ├── findRightNode.cpp ├── printAllPaths.cpp ├── printNodesAtOddLevel.cpp └── writeIterativeVersion.cpp ├── CircularLinkedList ├── CountNodes.cpp ├── Delete.cpp └── insertNodes.cpp ├── DoubleLinked ├── rearrange.cpp ├── rotate.cpp └── swapNodes.cpp ├── DynamicMemory └── cutTheSticks.cpp ├── Functions ├── PrimeFactors.cpp ├── SumOfRange.cpp ├── additionOfNumbers.cpp ├── binaryToDecimal.cpp ├── displayOfStrings.cpp ├── greatestCommonDivisor.cpp └── verifyPrime.cpp ├── Heap ├── FindKLargest.cpp ├── checkAGivenTree.cpp ├── connectTheSticks.cpp └── convertMinHeap.cpp ├── LinkedList ├── MoveTheSmallest.cpp ├── PrintList.cpp ├── ReversetheList.cpp ├── addTwoNumbers.cpp ├── checkListForPalindrome.cpp ├── copyFirstToSecond.cpp ├── deleteANode.cpp └── findTheLoop.cpp ├── OverviewOfC++ ├── EvenAndOdd.cpp ├── GivenAmount.cpp ├── LargestOfThreeNumbers.cpp ├── MultiplicationTable.cpp ├── Pyramid6.cpp ├── leapYear.cpp ├── pyramid7.cpp ├── pyramid8.cpp └── sumOfN.cpp ├── PriorityQueue └── ImplementPriorityQueueUsingLL.cpp ├── Queue ├── ImplementStackUsingTwoQueues.cpp ├── implementQueueUsingArray.cpp ├── implementQueueUsingLinkedList.cpp ├── implementQueueUsingTwoStacks.cpp └── reverseQueue.cpp ├── README.md ├── Sorting ├── BubbleSort.cpp ├── InsertionSort.cpp ├── MergeSort.cpp ├── QuickSort.cpp └── SelectionSort.cpp ├── Stack ├── EvaluateThePrefix.cpp ├── ImplementStackUsingArray.cpp ├── ImplementStackUsingLinkedList.cpp ├── NextGreaterElement.cpp ├── ReverseAString.cpp ├── designStackForPush.cpp ├── evaluatePostfixExpression.cpp ├── infixToPostfix.cpp └── minimumBrackets.cpp ├── arrays ├── MaximumFrequency.cpp ├── MoveTheElements.cpp ├── SecondMaximum.cpp ├── checkTwoArrays.cpp └── countNegative.cpp ├── circularQueue └── implemenetCirxularQueue.cpp └── objectAndClasses ├── Date.cpp ├── count.cpp └── timeSpan.cpp /BinarySearch/RotationCount.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | 4 | int rotationCount(int a[], int size){ 5 | int mn = *min_element(a,a+size); 6 | for (int i = 0; i < size; i++) 7 | { 8 | if(a[i] == mn){ 9 | return i; 10 | } 11 | } 12 | return 0; 13 | } 14 | 15 | int main(){ 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /BinarySearch/findAllPairs.cpp: -------------------------------------------------------------------------------- 1 | 2 | int getPairsCount(int arr[], int n, int sum){ 3 | unordered_map m; 4 | for (int i = 0; i < n; i++) 5 | m[arr[i]]++; 6 | int c = 0; 7 | for (int i = 0; i < n; i++) { 8 | c += m[sum - arr[i]]; 9 | if (sum - arr[i] == arr[i]) 10 | c--; 11 | } 12 | return c / 2; 13 | } 14 | -------------------------------------------------------------------------------- /BinarySearch/findFirstOccurance.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | int find(int arr[],int n,int k){ 4 | for (int i = 0; i < n; i++) 5 | { 6 | if(arr[i] == k){ 7 | return i; 8 | break; 9 | } 10 | } 11 | return -1; 12 | } 13 | int main(){ 14 | int t;cin>>t; 15 | while (t--) 16 | { 17 | int n,k; 18 | cin>>n>>k; 19 | int arr[n]; 20 | for (int i = 0; i < n; i++) 21 | { 22 | cin>>arr[i]; 23 | } 24 | cout< 2 | using namespace std; 3 | 4 | void sort(int arr[],int n){ 5 | for (int i = 0; i < n; ++i) 6 | { 7 | for (int j = i + 1; j < n; ++j) 8 | { 9 | if (arr[i] > arr[j]) 10 | { 11 | int temp = arr[i]; 12 | arr[i] = arr[j]; 13 | arr[j] = temp; 14 | } 15 | } 16 | } 17 | } 18 | 19 | int getMissingElement(int* a,int a_size,int* b ,int b_size){ 20 | sort(a,a_size); 21 | sort(b,b_size); 22 | for (int i = 0; i < b_size; i++) 23 | { 24 | if(a[i] != b[i]){ 25 | return a[i]; 26 | } 27 | } 28 | return a[a_size-1]; 29 | } 30 | int main(){ 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /BinarySearchTree/binaryTree.cpp: -------------------------------------------------------------------------------- 1 | int isBinarySearchTree(struct Node* t1){ 2 | static struct Node *prev = NULL; 3 | if(t1 != NULL){ 4 | if(!isBinarySearchTree(t1->left)){ 5 | return 0; 6 | } 7 | if(prev != NULL && t1->data <= prev->data){ 8 | return 0; 9 | } 10 | prev = t1; 11 | return isBinarySearchTree(t1->right); 12 | } 13 | else{ 14 | return 1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /BinarySearchTree/convertLeverOrder.cpp: -------------------------------------------------------------------------------- 1 | struct Node * insert(Node *root,int key){ 2 | if(root == NULL){ 3 | Node *temp = newNode(key); 4 | return temp; 5 | } 6 | if(key < root->data){ 7 | root->left = insert(root->left,key); 8 | } 9 | else if(key > root->data){ 10 | root->right = insert(root->right,key); 11 | } 12 | return root; 13 | } 14 | Node* buildSearchTree(int t[], int n){ 15 | if(n == 0){ 16 | return NULL; 17 | } 18 | Node* root = newNode(t[0]); 19 | for (int i = 1; i < n; i++) 20 | { 21 | insert(root,t[i]); 22 | } 23 | return(root); 24 | } 25 | -------------------------------------------------------------------------------- /BinarySearchTree/findKth.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | int x = 0; 3 | int arr[100]; 4 | void makeArray(Node *t1,int k){ 5 | if(t1 != NULL){ 6 | arr[x] = t1->data; 7 | x++; 8 | makeArray(t1->left,k); 9 | makeArray(t1->right,k); 10 | } 11 | } 12 | int kSmallest(Node* t1, int k){ 13 | makeArray(t1,k); 14 | sort(arr,arr+x); 15 | int y = arr[k-1]; 16 | return y; 17 | } 18 | -------------------------------------------------------------------------------- /BinaryTree/ConmpleteInorder.cpp: -------------------------------------------------------------------------------- 1 | void inorder(Node* root){ 2 | if(root != NULL){ 3 | inorder(root->left); 4 | printf("%d ",root->data); 5 | inorder(root->right); 6 | } 7 | } 8 | void preorder(Node* root){ 9 | if(root != NULL){ 10 | printf("%d ",root->data); 11 | preorder(root->left); 12 | preorder(root->right); 13 | } 14 | } 15 | void postorder(Node* root){ 16 | if(root != NULL){ 17 | postorder(root->left); 18 | postorder(root->right); 19 | printf("%d ",root->data); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /BinaryTree/ConstructTree.cpp: -------------------------------------------------------------------------------- 1 | int search(int arr[], int strt, int end, int value) 2 | { 3 | int i; 4 | for (i = strt; i <= end; i++) { 5 | if (arr[i] == value) 6 | break; 7 | } 8 | return i; 9 | } 10 | Node *buildUtil(int in[],int post[],int inStrt,int inEnd,int *pIndex){ 11 | if(inStrt > inEnd){ 12 | return NULL; 13 | } 14 | Node *node = newNode(post[*pIndex]); 15 | (*pIndex)--; 16 | if(inStrt == inEnd){ 17 | return node; 18 | } 19 | int inIndex = search(in , inStrt , inEnd , node->data); 20 | node->right = buildUtil(in, post, inIndex + 1, inEnd, pIndex); 21 | node->left = buildUtil(in, post, inStrt, inIndex - 1, pIndex); 22 | return node; 23 | } 24 | Node* buildTree(int in[], int post[], int N){ 25 | int pIndex = N - 1; 26 | return buildUtil(in , post , 0 , N-1 , &pIndex); 27 | } 28 | -------------------------------------------------------------------------------- /BinaryTree/PrintBinaryTreeWithLevelOrderTraversal.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | void printLevelWise(struct Node* root){ 3 | if (root == NULL){ 4 | return; 5 | } 6 | queue q; 7 | q.push(root); 8 | while (q.empty() == false) 9 | { 10 | int nodeCount = q.size(); 11 | while (nodeCount>0) 12 | { 13 | Node *node = q.front(); 14 | cout<data; 15 | if(nodeCount>1){ 16 | cout<<" "; 17 | } 18 | q.pop(); 19 | if(node->left != NULL){ 20 | q.push(node->left); 21 | } 22 | if(node->right != NULL){ 23 | q.push(node->right); 24 | } 25 | nodeCount--; 26 | } 27 | cout<<"\n"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /BinaryTree/countNumberOfLeaf.cpp: -------------------------------------------------------------------------------- 1 | int countLeafs(struct Node* root) 2 | { 3 | if (root == NULL) 4 | { 5 | return 0; 6 | } 7 | if (root->left == NULL && root->right == NULL) 8 | { 9 | return 1; 10 | } 11 | return countLeafs(root->left) + countLeafs(root->right); 12 | } 13 | int countNonLeafs(struct Node* root) 14 | { 15 | if(root == NULL || (root->left == NULL && root->right == NULL)){ 16 | return 0; 17 | } 18 | return 1 + countNonLeafs(root->left) + countNonLeafs(root->right); 19 | } 20 | -------------------------------------------------------------------------------- /BinaryTree/createATreeFromArray.cpp: -------------------------------------------------------------------------------- 1 | struct Node * createBT(int arr[],Node *root,int i,int n){ 2 | if(ileft = createBT(arr,root->left,2*i+1,n); 6 | root->right = createBT(arr,root->right,2*i+2,n); 7 | } 8 | return root; 9 | } 10 | struct Node* buildTree(int t[], int n){ 11 | int i = 0; 12 | Node *root = createBT(t,root,i,n); 13 | return root; 14 | } 15 | -------------------------------------------------------------------------------- /BinaryTree/findRightNode.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | Node * nextRight(Node *root ,int k){ 3 | if(root == NULL){ 4 | return 0; 5 | } 6 | queue qn; 7 | queue ql; 8 | int level = 0; 9 | qn.push(root); 10 | ql.push(level); 11 | while (qn.size()) 12 | { 13 | Node *node = qn.front(); 14 | level = ql.front(); 15 | qn.pop(); 16 | ql.pop(); 17 | if(node->data == k){ 18 | if(ql.size() == 0 || ql.front() != level){ 19 | return NULL; 20 | } 21 | return qn.front(); 22 | } 23 | if(node->left != NULL){ 24 | qn.push(node->left); 25 | ql.push(level+1); 26 | } 27 | if(node->right != NULL){ 28 | qn.push(node->right); 29 | ql.push(level+1); 30 | } 31 | } 32 | return NULL; 33 | } 34 | int findRightSibling(struct Node* root, int key){ 35 | Node *nr = nextRight(root,key); 36 | if(nr != NULL){ 37 | return nr->data; 38 | } 39 | return -1; 40 | } 41 | -------------------------------------------------------------------------------- /BinaryTree/printAllPaths.cpp: -------------------------------------------------------------------------------- 1 | void printArray(int ints[], int len) 2 | { 3 | int i; 4 | for (i = 0; i < len; i++) 5 | { 6 | cout << ints[i] << " "; 7 | } 8 | } 9 | int x = 0; 10 | void printPathsRecur(Node* node, int path[], int pathLen) 11 | { 12 | if (node == NULL) return; 13 | path[pathLen] = node->data; 14 | pathLen++; 15 | if (node->left == NULL && node->right == NULL) 16 | { 17 | x++; 18 | printArray(path, pathLen); 19 | cout<<(pathLen-1)<left, path, pathLen); 24 | printPathsRecur(node->right, path, pathLen); 25 | } 26 | } 27 | void printAllPaths(Node* root) { 28 | int path[1000]; 29 | printPathsRecur(root, path, 0); 30 | if(root != NULL){ 31 | cout<left); 7 | int right_height = height(root->right); 8 | return max(left_height,right_height)+1; 9 | } 10 | } 11 | void printGivenLevel(struct Node* root, int level) 12 | { 13 | if (root == NULL) 14 | return; 15 | if (level == 1) 16 | printf("%d ", root->data); 17 | else if (level > 1) 18 | { 19 | printGivenLevel(root->left, level-1); 20 | printGivenLevel(root->right, level-1); 21 | } 22 | } 23 | void printOdd(struct Node* root) 24 | { 25 | int h = height(root); 26 | for (int i = 1; i <= h; i+=2) 27 | { 28 | printGivenLevel(root,i); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /BinaryTree/writeIterativeVersion.cpp: -------------------------------------------------------------------------------- 1 | void printInorder(struct Node* root) 2 | { 3 | if(root != NULL){ 4 | printInorder(root->left); 5 | cout<data<<" "; 6 | printInorder(root->right); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /CircularLinkedList/CountNodes.cpp: -------------------------------------------------------------------------------- 1 | int countNodes(Node* head){ 2 | int length = 0; 3 | if(head == NULL){ 4 | return 0; 5 | } 6 | Node *p = head; 7 | while (p->next != head) 8 | { 9 | length++; 10 | p = p->next; 11 | } 12 | return length+1; 13 | } 14 | -------------------------------------------------------------------------------- /CircularLinkedList/Delete.cpp: -------------------------------------------------------------------------------- 1 | Node* deleteBeg(Node* head){ 2 | Node *p = head; 3 | while (p->next != head) 4 | { 5 | p = p->next; 6 | } 7 | p->next = head->next; 8 | int x = head->data; 9 | delete head; 10 | head = p->next; 11 | return head; 12 | } 13 | Node* deleteEnd(Node* head){ 14 | Node *p = head; 15 | while (p->next->next != head) 16 | { 17 | p = p->next; 18 | } 19 | Node *q = p->next; 20 | p->next = head; 21 | delete q; 22 | return head; 23 | } 24 | -------------------------------------------------------------------------------- /CircularLinkedList/insertNodes.cpp: -------------------------------------------------------------------------------- 1 | Node* insertBeg(Node* head, int data){ 2 | Node *ptr = new Node(); 3 | ptr->data = data; 4 | ptr->next = NULL; 5 | Node *temp ; 6 | if(ptr == NULL){ 7 | printf("\nOVERFLOW"); 8 | } 9 | else{ 10 | if(head == NULL){ 11 | head = ptr; 12 | ptr->next = head; 13 | } 14 | else{ 15 | temp = head; 16 | while (temp->next != head) 17 | { 18 | temp = temp->next; 19 | } 20 | ptr->next = head; 21 | temp->next = ptr; 22 | head = ptr; 23 | } 24 | } 25 | } 26 | Node* insertEnd(Node* head, int data){ 27 | Node *ptr = new Node(); 28 | ptr->data = data; 29 | ptr->next = NULL; 30 | Node *temp ; 31 | if(ptr == NULL){ 32 | printf("\nOVERFLOW"); 33 | } 34 | else{ 35 | if(head == NULL){ 36 | head = ptr; 37 | ptr->next = head; 38 | } 39 | else{ 40 | temp = head; 41 | while (temp->next != head) 42 | { 43 | temp = temp->next; 44 | } 45 | temp->next = ptr; 46 | ptr->next = head; 47 | return head; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /DoubleLinked/rearrange.cpp: -------------------------------------------------------------------------------- 1 | Node* rearrangeList(Node* head){ 2 | int i = 1; 3 | Node *even = NULL; 4 | Node *odd = NULL; 5 | Node *last = head; 6 | while (last != NULL) 7 | { 8 | if(i%2 == 0){ 9 | even = insertEnd(even , last->data); 10 | } 11 | if(i%2 != 0){ 12 | odd = insertEnd(odd , last->data); 13 | } 14 | last = last->next; 15 | i++; 16 | } 17 | Node *final = NULL; 18 | while (even != NULL) 19 | { 20 | final = insertEnd(final , even->data); 21 | even = even->next; 22 | } 23 | while (odd != NULL) 24 | { 25 | final = insertEnd(final , odd->data); 26 | odd = odd->next; 27 | } 28 | return final; 29 | } 30 | -------------------------------------------------------------------------------- /DoubleLinked/rotate.cpp: -------------------------------------------------------------------------------- 1 | void insertAtBeg(Node **head , int data){ 2 | Node *node = new Node(data); 3 | node->data = data; 4 | node->next = *head; 5 | node->prev = NULL; 6 | if(head != NULL){ 7 | (*head)->prev = node; 8 | } 9 | *head = node; 10 | } 11 | int deleteAtEnd(Node *head){ 12 | Node *last = head; 13 | while (last->next != NULL) 14 | { 15 | last = last->next; 16 | } 17 | int x = last->data; 18 | last->prev->next = NULL; 19 | delete(last); 20 | return x; 21 | } 22 | Node* rotateByK(Node* head, int k) 23 | { 24 | if(head == NULL){ 25 | return head; 26 | } 27 | for (int i = 0; i < k; i++) 28 | { 29 | int x = deleteAtEnd(head); 30 | insertAtBeg(&head,x); 31 | } 32 | return head; 33 | } 34 | -------------------------------------------------------------------------------- /DoubleLinked/swapNodes.cpp: -------------------------------------------------------------------------------- 1 | void swapNodes(Node** head, int x, int y){ 2 | if(x == y){ 3 | return; 4 | } 5 | Node *prevX = NULL , *currX = *head; 6 | while (currX && currX->data != x) 7 | { 8 | prevX = currX; 9 | currX = currX->next; 10 | } 11 | Node *prevY = NULL , *currY = *head; 12 | while (currY && currY->data != y) 13 | { 14 | prevY = currY; 15 | currY = currY->next; 16 | } 17 | if(currX == NULL || currY == NULL){ 18 | return; 19 | } 20 | if(prevX != NULL){ 21 | prevX->next = currY; 22 | } 23 | else{ 24 | *head = currY; 25 | } 26 | if(prevY != NULL){ 27 | prevY->next = currX; 28 | } 29 | else{ 30 | *head = currX; 31 | } 32 | Node *temp = currY->next; 33 | currY->next = currX->next; 34 | currX->next = temp; 35 | } 36 | -------------------------------------------------------------------------------- /DynamicMemory/cutTheSticks.cpp: -------------------------------------------------------------------------------- 1 | void sort(int arr[],int n){ 2 | for (int i = 0; i < n; ++i) 3 | { 4 | for (int j = i + 1; j < n; ++j) 5 | { 6 | if (arr[i] > arr[j]) 7 | { 8 | int temp = arr[i]; 9 | arr[i] = arr[j]; 10 | arr[j] = temp; 11 | } 12 | } 13 | } 14 | } 15 | 16 | int* cutSticks(int lengths_size, int *lengths, int *result_size) { 17 | int i; 18 | int *arr; 19 | int n=lengths_size; 20 | arr=(int*)malloc(sizeof(int)*lengths_size); 21 | int min,count=0,count1=0,k=0; 22 | while(count1!=n) 23 | { 24 | count=0; 25 | count1=0; 26 | for(i=0;i=0) 44 | { 45 | lengths[i]=lengths[i]-min; 46 | count++; 47 | } 48 | } 49 | for(i=0;i 2 | 3 | void primeFactors(int n){ 4 | // Write your code here 5 | while (n%2 == 0) 6 | { 7 | printf("%d\n", 2); 8 | n = n/2; 9 | } 10 | for (int i = 3; i <= sqrt(n); i = i+2) 11 | { 12 | while (n%i == 0) 13 | { 14 | printf("%d\n", i); 15 | n = n/i; 16 | } 17 | } 18 | if (n > 2) 19 | printf ("%d\n", n); 20 | } -------------------------------------------------------------------------------- /Functions/SumOfRange.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | 4 | int sumOfRange(int min, int max){ 5 | int sum(0); 6 | for (int i = min; i <= max; i++) 7 | { 8 | sum += i; 9 | } 10 | return sum; 11 | } 12 | 13 | 14 | int main(){ 15 | 16 | 17 | 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Functions/additionOfNumbers.cpp: -------------------------------------------------------------------------------- 1 | // write the overloaded sum() functions for 2, 3 and 4 arguments 2 | int sum(int a,int b){ 3 | return a+b; 4 | } 5 | int sum(int a,int b,int c){ 6 | return a+b+c; 7 | } 8 | int sum(int a,int b,int c,int d){ 9 | return a+b+c+d; 10 | } -------------------------------------------------------------------------------- /Functions/binaryToDecimal.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | 4 | int binaryToDecimal(string binary){ 5 | // Write your code here 6 | string num = binary; 7 | int dec_value = 0; 8 | int base = 1; 9 | int len = num.length(); 10 | for (int i = len - 1; i >= 0; i--) { 11 | if (num[i] == '1') 12 | dec_value += base; 13 | base = base * 2; 14 | } 15 | return dec_value; 16 | } 17 | -------------------------------------------------------------------------------- /Functions/displayOfStrings.cpp: -------------------------------------------------------------------------------- 1 | // Write overloaded display(string a) and display(string a, string b) functions 2 | # include 3 | using namespace std; 4 | 5 | void display(string a){ 6 | cout< first integer 5 | * j -> second integer 6 | * 7 | * @returns 8 | * an integer, denoting the gcd of i and j 9 | */ 10 | int gcd(int i, int j){ 11 | if(j == 0){ 12 | return i; 13 | } 14 | return gcd(j,i%j); 15 | } -------------------------------------------------------------------------------- /Functions/verifyPrime.cpp: -------------------------------------------------------------------------------- 1 | bool verifyPrime(int n){ 2 | // Write your code her 3 | if(n == 0 || n == 1){ 4 | return false; 5 | } 6 | for (int i = 2; i < n; i++) 7 | { 8 | if(n%i == 0){ 9 | return false; 10 | } 11 | } 12 | return true; 13 | } -------------------------------------------------------------------------------- /Heap/FindKLargest.cpp: -------------------------------------------------------------------------------- 1 | void printKLargest(int array[],int n,int k){ 2 | sort(array , array + n); 3 | for (int i = n-1; i > n-1-k; i--) 4 | { 5 | cout< (n-k )){ 7 | cout<<" "; 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Heap/checkAGivenTree.cpp: -------------------------------------------------------------------------------- 1 | int isMaxHeap(int array[], int n){ 2 | for (int i = 0; i <= (n-2)/2; i++) 3 | { 4 | if(array[2*i+1] > array[i]){ 5 | return false; 6 | } 7 | if(2*i+2 < n && array[2*i+2] > array[i]){ 8 | return false; 9 | } 10 | } 11 | return true; 12 | } 13 | -------------------------------------------------------------------------------- /Heap/connectTheSticks.cpp: -------------------------------------------------------------------------------- 1 | struct MinHeap { 2 | unsigned size; 3 | unsigned capacity; 4 | int* harr; 5 | }; 6 | struct MinHeap* createMinHeap(unsigned capacity) 7 | { 8 | struct MinHeap* minHeap = new MinHeap; 9 | minHeap->size = 0; 10 | minHeap->capacity = capacity; 11 | minHeap->harr = new int[capacity]; 12 | return minHeap; 13 | } 14 | void swapMinHeapNode(int* a, int* b) 15 | { 16 | int temp = *a; 17 | *a = *b; 18 | *b = temp; 19 | } 20 | void minHeapify(struct MinHeap* minHeap, int idx) 21 | { 22 | int smallest = idx; 23 | int left = 2 * idx + 1; 24 | int right = 2 * idx + 2; 25 | if (left < minHeap->size 26 | && minHeap->harr[left] < minHeap->harr[smallest]) 27 | smallest = left; 28 | if (right < minHeap->size 29 | && minHeap->harr[right] < minHeap->harr[smallest]) 30 | smallest = right; 31 | if (smallest != idx) { 32 | swapMinHeapNode(&minHeap->harr[smallest], &minHeap->harr[idx]); 33 | minHeapify(minHeap, smallest); 34 | } 35 | } 36 | int isSizeOne(struct MinHeap* minHeap) 37 | { 38 | return (minHeap->size == 1); 39 | } 40 | int extractMin(struct MinHeap* minHeap) 41 | { 42 | int temp = minHeap->harr[0]; 43 | minHeap->harr[0] = minHeap->harr[minHeap->size - 1]; 44 | --minHeap->size; 45 | minHeapify(minHeap, 0); 46 | return temp; 47 | } 48 | void insertMinHeap(struct MinHeap* minHeap, int val) 49 | { 50 | ++minHeap->size; 51 | int i = minHeap->size - 1; 52 | while (i && (val < minHeap->harr[(i - 1) / 2])) { 53 | minHeap->harr[i] = minHeap->harr[(i - 1) / 2]; 54 | i = (i - 1) / 2; 55 | } 56 | minHeap->harr[i] = val; 57 | } 58 | void buildMinHeap(struct MinHeap* minHeap) 59 | { 60 | int n = minHeap->size - 1; 61 | int i; 62 | for (i = (n - 1) / 2; i >= 0; --i) 63 | minHeapify(minHeap, i); 64 | } 65 | struct MinHeap* createAndBuildMinHeap( 66 | int len[], int size) 67 | { 68 | struct MinHeap* minHeap = createMinHeap(size); 69 | for (int i = 0; i < size; ++i) 70 | minHeap->harr[i] = len[i]; 71 | minHeap->size = size; 72 | buildMinHeap(minHeap); 73 | return minHeap; 74 | } 75 | int connectCost(int lengths[], int n){ 76 | int cost = 0; 77 | struct MinHeap* minHeap = createAndBuildMinHeap(lengths, n); 78 | while (!isSizeOne(minHeap)) { 79 | int min = extractMin(minHeap); 80 | int sec_min = extractMin(minHeap); 81 | cost += (min + sec_min); 82 | insertMinHeap(minHeap, min + sec_min); 83 | } 84 | return cost; 85 | } 86 | -------------------------------------------------------------------------------- /Heap/convertMinHeap.cpp: -------------------------------------------------------------------------------- 1 | void maxHeapify(int arr[] , int i , int n){ 2 | int l = (2*i) + 1; 3 | int r = (2*i) + 2; 4 | int largest = i; 5 | if(l arr[i]){ 6 | largest = l; 7 | } 8 | if(r arr[largest]){ 9 | largest = r; 10 | } 11 | if(largest != i){ 12 | swap(arr[i] , arr[largest]); 13 | maxHeapify(arr , largest , n); 14 | } 15 | } 16 | void modifyMintoMax(int a[], int n){ 17 | for (int i = (n-2)/2; i >= 0; --i) 18 | { 19 | maxHeapify(a , i , n); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LinkedList/MoveTheSmallest.cpp: -------------------------------------------------------------------------------- 1 | 2 | # include 3 | int minNode(Node *head){ 4 | Node *last = head; 5 | int min = INT_MAX; 6 | while (last != NULL) 7 | { 8 | if(last->data < min){ 9 | min = last->data; 10 | } 11 | last = last->next; 12 | } 13 | return min; 14 | } 15 | int maxNode(Node *head){ 16 | Node *last = head; 17 | int max = INT_MIN; 18 | while (last != NULL) 19 | { 20 | if(last->data > max){ 21 | max = last->data; 22 | } 23 | last = last->next; 24 | } 25 | return max; 26 | } 27 | void insertBeg(struct Node** head, int data){ 28 | struct Node* node = (struct Node*) malloc(sizeof(struct Node)); 29 | node->data = data; 30 | node->next = (*head); 31 | (*head) = node; 32 | } 33 | struct Node * shiftSmallLarge(struct Node *org) 34 | { 35 | int x = minNode(org); 36 | int y = maxNode(org); 37 | if(org == NULL){ 38 | return org; 39 | } 40 | Node *ptr = NULL; 41 | if(org->next == NULL){ 42 | insertBeg(&ptr , x); 43 | return ptr; 44 | } 45 | Node *last = org; 46 | while (last != NULL) 47 | { 48 | if(last->data == x){ 49 | last = last->next; 50 | } 51 | else if(last->data == y){ 52 | last = last->next; 53 | } 54 | else{ 55 | insertEnd(&ptr , last->data); 56 | last = last->next; 57 | } 58 | } 59 | insertBeg(&ptr , x); 60 | insertEnd(&ptr , y); 61 | return ptr; 62 | } 63 | -------------------------------------------------------------------------------- /LinkedList/PrintList.cpp: -------------------------------------------------------------------------------- 1 | void forwardPrint(struct Node* head) 2 | { 3 | if(head != NULL){ 4 | cout<data<<"-"; 5 | forwardPrint(head->next); 6 | } 7 | } 8 | void backwardPrint(struct Node* head) 9 | { 10 | if(head != NULL){ 11 | backwardPrint(head->next); 12 | cout<data<<"-"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LinkedList/ReversetheList.cpp: -------------------------------------------------------------------------------- 1 | Node *ptr = NULL; 2 | void giveReverse(Node *head){ 3 | ptr = NULL; 4 | if(head != NULL){ 5 | giveReverse(head->next); 6 | insertEnd(&ptr,head->data); 7 | } 8 | } 9 | struct Node* reverseList(struct Node* head) { 10 | giveReverse(head); 11 | return ptr; 12 | } 13 | -------------------------------------------------------------------------------- /LinkedList/addTwoNumbers.cpp: -------------------------------------------------------------------------------- 1 | Node* addListNumbers(Node* head1,Node* head2){ 2 | int x = 0; 3 | int i = 0; 4 | while (head1 != NULL) 5 | { 6 | int m = head1->data; 7 | x = m*pow(10,i) + x; 8 | i++; 9 | head1 = head1->next; 10 | } 11 | int y = 0; 12 | int j = 0; 13 | while (head2 != NULL) 14 | { 15 | int m = head2->data; 16 | y = m*pow(10,j) + y; 17 | j++; 18 | head2 = head2->next; 19 | } 20 | int sum = x+y; 21 | Node *ptr = NULL; 22 | while (sum > 0 ) 23 | { 24 | int x = sum%10; 25 | insertEnd(&ptr ,x); 26 | sum = sum/10; 27 | } 28 | return ptr; 29 | } 30 | -------------------------------------------------------------------------------- /LinkedList/checkListForPalindrome.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | int checkPalindrome(struct Node* head) 3 | { 4 | if(head == NULL){ 5 | return 0; 6 | } 7 | Node *ptr = head; 8 | stack s; 9 | while (ptr != NULL) 10 | { 11 | s.push(ptr->data); 12 | ptr = ptr->next; 13 | } 14 | while (head != NULL) 15 | { 16 | int i = s.top(); 17 | s.pop(); 18 | if(head->data != i){ 19 | return 0; 20 | } 21 | head = head->next; 22 | } 23 | return 1; 24 | } 25 | -------------------------------------------------------------------------------- /LinkedList/copyFirstToSecond.cpp: -------------------------------------------------------------------------------- 1 | struct Node *copyList(struct Node *org) 2 | { 3 | Node *ptr = NULL; 4 | while (org != NULL) 5 | { 6 | insertEnd(&ptr , org->data); 7 | org = org->next; 8 | } 9 | return ptr; 10 | } 11 | -------------------------------------------------------------------------------- /LinkedList/deleteANode.cpp: -------------------------------------------------------------------------------- 1 | void deleteNodeK(Node* node){ 2 | if(node == NULL){ 3 | return; 4 | } 5 | if (node->next == NULL) 6 | { 7 | //free(node); 8 | return; 9 | } 10 | Node* temp = node->next; 11 | node->data = temp->data; 12 | node->next = temp->next; 13 | free(temp); 14 | } 15 | -------------------------------------------------------------------------------- /LinkedList/findTheLoop.cpp: -------------------------------------------------------------------------------- 1 | int countNodes(struct Node *n) 2 | { 3 | int res = 1; 4 | struct Node *temp = n; 5 | while (temp->next != n) 6 | { 7 | res++; 8 | temp = temp->next; 9 | } 10 | return res; 11 | } 12 | int loopInList(Node* head) { 13 | struct Node *slow_p = head, *fast_p = head; 14 | while (slow_p && fast_p && 15 | fast_p->next) 16 | { 17 | slow_p = slow_p->next; 18 | fast_p = fast_p->next->next; 19 | if (slow_p == fast_p) 20 | return countNodes(slow_p); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /OverviewOfC++/EvenAndOdd.cpp: -------------------------------------------------------------------------------- 1 | 2 | # include 3 | using namespace std; 4 | int main(){ 5 | int n;cin>>n; 6 | if(n%2 == 0){ 7 | cout<<"Even"<<"\n"; 8 | } 9 | else{ 10 | cout<<"Odd"<<"\n"; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /OverviewOfC++/GivenAmount.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | int main(){ 4 | int arr[] = {2000, 500, 100, 50, 20, 10, 5, 2, 1}; 5 | int n;cin>>n; 6 | int x = sizeof(arr)/sizeof(arr[0]); 7 | for (int i = 0; i < x; i++) 8 | { 9 | int z = n/arr[i]; 10 | n = n - z*arr[i]; 11 | arr[i] = z; 12 | } 13 | for (int i = 0; i < x; i++) 14 | { 15 | cout< 2 | using namespace std; 3 | int main(){ 4 | int arr[3]; 5 | for (int i = 0; i < 3; i++) 6 | { 7 | cin>>arr[i]; 8 | } 9 | cout<<(*max_element(arr,arr+3))<<"\n"; 10 | return 0; 11 | } -------------------------------------------------------------------------------- /OverviewOfC++/MultiplicationTable.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,m; 6 | cin>>n>>m; 7 | for (int i = 1; i <= m; i++) 8 | { 9 | cout<<(n*i)<<"\n"; 10 | } 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /OverviewOfC++/Pyramid6.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | for (int i = 1; i <= n; i++) 7 | { 8 | int p = i; 9 | for (int j = 1; j <= i; j++) 10 | { 11 | cout< 2 | using namespace std; 3 | bool isLeap(int n){ 4 | if(n % 400 == 0){ 5 | return true; 6 | } 7 | if(n %100 == 0){ 8 | return false; 9 | } 10 | if(n %4 == 0){ 11 | return true; 12 | } 13 | return false; 14 | } 15 | int main(){ 16 | int n;cin>>n; 17 | if(isLeap(n)){ 18 | cout<<"Leap Year"<<"\n"; 19 | } 20 | else{ 21 | cout<<"Not a Leap Year"<<"\n"; 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /OverviewOfC++/pyramid7.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | int p =1; 7 | for (int i = 1; i <= n; i++) 8 | { 9 | for (int j = 1; j <= i; j++) 10 | { 11 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | for (int i = 0; i < 1; i++) 7 | { 8 | for (int j = 1; j < n-i+1; j++) 9 | { 10 | cout< 2 | using namespace std; 3 | int main(){ 4 | int n;cin>>n; 5 | cout<<(n*(n+1))/2; 6 | return 0; 7 | } -------------------------------------------------------------------------------- /PriorityQueue/ImplementPriorityQueueUsingLL.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | class PriorityQueue{ 3 | Node *front, *rear; 4 | public: 5 | PriorityQueue(){ 6 | front = NULL; 7 | rear = NULL; 8 | } 9 | void enQueue(int data,int priority){ 10 | Node *tmp , *q; 11 | tmp = new Node(data,priority); 12 | if(front == NULL || priority < front->priority){ 13 | tmp->next = front; 14 | front = tmp; 15 | } 16 | else{ 17 | q = front; 18 | while (q->next != NULL && q->next->priority <= priority) 19 | { 20 | q = q->next; 21 | } 22 | tmp->next = q->next; 23 | q->next = tmp; 24 | } 25 | } 26 | int deQueue(){ 27 | Node *tmp; 28 | if(front == NULL){ 29 | cout<<"Queue Underflow\n"; 30 | return -1; 31 | } 32 | else 33 | { 34 | tmp = front; 35 | int x = tmp->data; 36 | // cout<data<<" "; 37 | front = front->next; 38 | free(tmp); 39 | return x; 40 | } 41 | return -1; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /Queue/ImplementStackUsingTwoQueues.cpp: -------------------------------------------------------------------------------- 1 | class Stack 2 | { 3 | public: 4 | void push(QueueArray* qa1, QueueArray* qa2, int item) 5 | { 6 | while (!(qa1->front > qa1->rear)) 7 | { 8 | qa2->enqueue(qa1->dequeue()); 9 | } 10 | qa1->enqueue(item); 11 | while (!(qa2->front > qa2->rear)) 12 | { 13 | qa1->enqueue(qa2->dequeue()); 14 | } 15 | } 16 | int pop(QueueArray* qa1, QueueArray* qa2) 17 | { 18 | if((qa1->front > qa1->rear)){ 19 | return -1; 20 | } 21 | int x = qa1->dequeue(); 22 | return x; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Queue/implementQueueUsingArray.cpp: -------------------------------------------------------------------------------- 1 | void enqueue(int item) 2 | { 3 | if(rear == SIZE){ 4 | return; 5 | } 6 | if(front == -1 && rear == -1){ 7 | front++; 8 | rear++; 9 | } 10 | else{ 11 | rear++; 12 | } 13 | array[rear] = item; 14 | } 15 | // Method to remove an item from queue. 16 | int dequeue() 17 | { 18 | if(front>rear){ 19 | return -1; 20 | } 21 | int item = array[front]; 22 | front++; 23 | return item; 24 | } 25 | -------------------------------------------------------------------------------- /Queue/implementQueueUsingLinkedList.cpp: -------------------------------------------------------------------------------- 1 | class Queue { 2 | struct QNode { 3 | int data; 4 | QNode* next; 5 | QNode(int d){ 6 | data = d; 7 | next = NULL; 8 | } 9 | }; 10 | QNode *front, *rear; 11 | public: 12 | // Constructor 13 | Queue(){ 14 | } 15 | // Add an element to the queue 16 | void enQueue(int x){ 17 | QNode* newNode = new QNode(x); 18 | if(rear == NULL){ 19 | front = rear = newNode; 20 | return; 21 | } 22 | rear->next = newNode; 23 | rear = newNode; 24 | } 25 | // Delete front element from the queue 26 | int deQueue() { 27 | if(front == NULL){ 28 | return -1; 29 | } 30 | int temp = front->data; 31 | front = front->next; 32 | if (front == NULL){ 33 | rear = NULL; 34 | } 35 | return temp; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /Queue/implementQueueUsingTwoStacks.cpp: -------------------------------------------------------------------------------- 1 | /* push(int), pop(), isEmpty(), isFull() are available on Stack. */ 2 | class Queue 3 | { 4 | //Beaware to do it public 5 | public: 6 | void enqueue(CQStack *st1, CQStack *st2, int item) 7 | { 8 | st1->push(item); 9 | } 10 | int dequeue(CQStack *st1, CQStack *st2) 11 | { 12 | if(st2->isEmpty() && st1->isEmpty()){ 13 | return -1; 14 | } 15 | if(st2->isEmpty()){ 16 | while (!st1->isEmpty()) 17 | { 18 | st2->push(st1->pop()); 19 | } 20 | } 21 | int temp = st2->pop(); 22 | return temp; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Queue/reverseQueue.cpp: -------------------------------------------------------------------------------- 1 | void reverseQueue(Queue *queue){ 2 | // Write your code here 3 | int arr[1000]; 4 | int i = 0; 5 | while (!queue->isEmpty()) 6 | { 7 | int temp = queue->deQueue(); 8 | arr[i] = temp; 9 | i++; 10 | } 11 | for (int j = i-1; j >= 0; j--) 12 | { 13 | int x = arr[j]; 14 | queue->enQueue(x); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeQuotientAnswers 2 | Answers of DSA C++ 3 |

hiten1502

4 | -------------------------------------------------------------------------------- /Sorting/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | 4 | int numberOfSwaps(int *A,int n){ 5 | int isSorted = 1; 6 | int swap(0); 7 | for (int i = 0; i < n-1; i++) 8 | { 9 | for (int j = 0; j < n-i-1; j++) 10 | { 11 | if(A[j]>A[j+1]){ 12 | int temp = A[j]; 13 | A[j] = A[j+1]; 14 | A[j+1] = temp; 15 | isSorted = 0; 16 | swap++; 17 | } 18 | } 19 | if(isSorted){ 20 | return swap; 21 | } 22 | } 23 | return swap; 24 | } 25 | 26 | 27 | 28 | int main(){ 29 | int t; 30 | cin>>t; 31 | while(t--){ 32 | int n;cin>>n; 33 | int arr[n]; 34 | for (int i = 0; i < n; i++) 35 | { 36 | cin>>arr[i]; 37 | } 38 | cout< 2 | using namespace std; 3 | int InsertionSort(int *A,int n){ 4 | int cnt = 0; 5 | int key,j; 6 | for (int i = 1; i <= n-1; i++) 7 | { 8 | key = A[i]; 9 | j = i-1; 10 | int in = 0; 11 | while (j>=0 && A[j]>key) 12 | { 13 | A[j+1] = A[j]; 14 | j--; 15 | cnt++; 16 | in = 1; 17 | } 18 | if(in == 1){ 19 | cnt++; 20 | in = 0; 21 | } 22 | A[j+1] = key; 23 | } 24 | return cnt; 25 | } 26 | int main(){ 27 | int t; 28 | cin>>t; 29 | while (t--) 30 | { 31 | int n; 32 | cin>>n; 33 | int arr[n]; 34 | for (int i = 0; i < n; i++) 35 | { 36 | cin>>arr[i]; 37 | } 38 | cout< 2 | using namespace std; 3 | 4 | void merge(int array[], int l, int m, int r) { 5 | int i,j,k,B[r+1]; 6 | i = l; 7 | j = m + 1; 8 | k = l; 9 | while (i<=m && j<=r) 10 | { 11 | if(array[i] < array[j]){ 12 | B[k] = array[i]; 13 | i++; 14 | k++; 15 | } 16 | else{ 17 | B[k] = array[j]; 18 | j++; 19 | k++; 20 | } 21 | } 22 | while (i <= m) 23 | { 24 | B[k] = array[i]; 25 | k++; 26 | i++; 27 | } 28 | while (j <= r) 29 | { 30 | B[k] = array[j]; 31 | k++; 32 | j++; 33 | } 34 | for (int i = l; i <= r ; i++) 35 | { 36 | array[i] = B[i]; 37 | } 38 | } 39 | 40 | void mergeSort(int array[], int l, int r) { 41 | int mid; 42 | if(l 2 | using namespace std; 3 | 4 | void swap(int* a, int* b) 5 | { 6 | int t = *a; 7 | *a = *b; 8 | *b = t; 9 | } 10 | int partition (int array[], int low, int high) { 11 | int pivot = array[high]; 12 | int i = low - 1; 13 | int j; 14 | for ( j = low; j <= high-1; j++) 15 | { 16 | if(array[j] <= pivot){ 17 | i++; 18 | swap(&array[i],&array[j]); 19 | } 20 | } 21 | swap(&array[i + 1], &array[high]); 22 | return (i + 1); 23 | } 24 | 25 | void quickSort(int array[], int low, int high) { 26 | if (low < high) 27 | { 28 | int pivot_index; 29 | pivot_index = partition(array, low, high); 30 | quickSort(array, low, pivot_index - 1); 31 | quickSort(array, pivot_index + 1, high); 32 | } 33 | } 34 | 35 | int main(){ 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Sorting/SelectionSort.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | void swap(int *a,int *b){ 4 | int temp = *a; 5 | *a = *b; 6 | *b = temp; 7 | } 8 | int swaps(int arr[],int n){ 9 | int min_index; 10 | int cnt = 0; 11 | for (int i = 0; i < n-1; i++) 12 | { 13 | min_index = i; 14 | int c = 0; 15 | for (int j = i+1; j < n; j++) 16 | { 17 | if(arr[j]>t; 33 | while(t--){ 34 | int n;cin>>n; 35 | int arr[n]; 36 | for (int i = 0; i < n; i++) 37 | { 38 | cin>>arr[i]; 39 | } 40 | cout<=0 ; i--) 13 | { 14 | char c = exp[i]; 15 | if (isdigit(c)) 16 | push(c-'0'); 17 | else 18 | { 19 | op1 = pop(); 20 | op2 = pop(); 21 | switch(c) 22 | { 23 | case '+': push(op1 + op2); break; 24 | case '-': push(op1 - op2); break; 25 | case '*': push(op1 * op2); break; 26 | case '/': push(op1 / op2); break; 27 | case '^':push(pow(op1,op2)); break; 28 | } 29 | } 30 | } 31 | return pop(); 32 | } -------------------------------------------------------------------------------- /Stack/ImplementStackUsingArray.cpp: -------------------------------------------------------------------------------- 1 | int isFull() 2 | { 3 | return top == SIZE - 1; 4 | } 5 | // Function to check if stack is empty. 6 | int isEmpty() 7 | { 8 | return top == -1; 9 | } 10 | // Function to add an item to stack. 11 | int push(int item) 12 | { 13 | if (isFull()) 14 | { 15 | return -1; 16 | } 17 | else{ 18 | Stack[++top] = item; 19 | } 20 | } 21 | // Function to remove an item from stack. 22 | int pop() 23 | { 24 | if(isEmpty()){ 25 | return -1; 26 | } 27 | else{ 28 | int val = Stack[top--]; 29 | return val; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Stack/ImplementStackUsingLinkedList.cpp: -------------------------------------------------------------------------------- 1 | class LinkStack{ 2 | LinkList *first; // ref to first item on list 3 | int size; 4 | public: 5 | LinkStack(){ 6 | first = NULL; 7 | size = 0; 8 | } 9 | bool isEmpty(){ 10 | return first == NULL; 11 | } 12 | void push(int dd){ 13 | LinkList *ptr = new LinkList(dd); 14 | ptr->next = first; 15 | first = ptr; 16 | size ++; 17 | } 18 | int pop(){ 19 | if(isEmpty()){ 20 | return -1; 21 | } 22 | size--; 23 | int temp = first->data; 24 | first = first->next; 25 | return temp; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Stack/NextGreaterElement.cpp: -------------------------------------------------------------------------------- 1 | void printNextGreaterElement(int arr[],int n){ 2 | for (int i = 0; i < n; i++) 3 | { 4 | for (int j = i; j < n; j++) 5 | { 6 | if(arr[j]>arr[i]){ 7 | cout<push(s[i]); 7 | } 8 | for (int i = 0; i < s.length(); i++) 9 | { 10 | int temp = sp->pop(); 11 | ans += temp; 12 | } 13 | return ans; 14 | } 15 | -------------------------------------------------------------------------------- /Stack/designStackForPush.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | int Stack[SIZE], top = -1; 3 | int isFull(); 4 | int isEmpty(); 5 | Above variables are used for Stack, SIZE and top and all are global variables. Also above two functions are provided. */ 6 | // Function to add an item to stack. 7 | void push(int item) 8 | { 9 | if (isFull()) 10 | { 11 | return; 12 | } 13 | else{ 14 | Stack[++top] = item; 15 | } 16 | } 17 | // Function to remove an item from stack. 18 | int pop() 19 | { 20 | if(isEmpty()){ 21 | return -1; 22 | } 23 | else{ 24 | int val = Stack[top--]; 25 | return val; 26 | } 27 | } 28 | // Function to return the minimum item from stack. 29 | int getMin() 30 | { 31 | int a = top; 32 | int min = Stack[a]; 33 | while (a != -1) 34 | { 35 | int x = Stack[a]; 36 | if(xpush(exp[i] - '0'); 9 | } 10 | else{ 11 | int op2 = st->pop(); 12 | int op1 = st->pop(); 13 | switch (exp[i]) 14 | { 15 | case '+': 16 | st->push(op1 + op2); 17 | break; 18 | case '-': 19 | st->push(op1 - op2); 20 | break; 21 | case '*': 22 | st->push(op1 * op2); 23 | break; 24 | case '/': 25 | st->push(op1 / op2); 26 | break; 27 | case '^': 28 | st->push(pow(op1,op2)); 29 | } 30 | } 31 | } 32 | answer = st->pop(); 33 | return answer; 34 | } -------------------------------------------------------------------------------- /Stack/infixToPostfix.cpp: -------------------------------------------------------------------------------- 1 | struct stack{ 2 | int size; 3 | char data; 4 | int top; 5 | }; 6 | int isOperand(char ch){ 7 | return (ch>= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z' || (isdigit(ch))); 8 | } 9 | int checkPrecedence(char ch){ 10 | switch(ch){ 11 | case '+': 12 | case '-': 13 | return 1; 14 | case '*': 15 | case '/': 16 | return 2; 17 | case '^': 18 | return 3; 19 | } 20 | return -1; 21 | } 22 | int infixToPostfix(char exp[], char output[]) 23 | { 24 | int i = 0,k = 0; 25 | while (exp[i]) 26 | { 27 | if(isOperand(exp[i])){ 28 | output[k++] = exp[i]; 29 | } 30 | else if(exp[i] == '('){ 31 | push(exp[i]); 32 | } 33 | else if(exp[i] == ')'){ 34 | while (!isEmpty() && Stack[top] != '(') 35 | { 36 | output[k++] = Stack[top]; 37 | pop(); 38 | } 39 | if(!isEmpty() && Stack[top] != '('){ 40 | return -1; 41 | } 42 | else{ 43 | pop(); 44 | } 45 | } 46 | else{ 47 | while (!isEmpty() && checkPrecedence(exp[i]) <= checkPrecedence(Stack[top])) 48 | { 49 | output[k++] = Stack[top]; 50 | pop(); 51 | } 52 | push(exp[i]); 53 | } 54 | i++; 55 | } 56 | while (!isEmpty()) 57 | { 58 | output[k++] = Stack[top]; 59 | pop(); 60 | } 61 | output[k++] = '\0'; 62 | } -------------------------------------------------------------------------------- /Stack/minimumBrackets.cpp: -------------------------------------------------------------------------------- 1 | int minReversal(char* expr){ 2 | int len = strlen(expr); 3 | if (len%2) 4 | return -1; 5 | stack s; 6 | for (int i=0; i 2 | using namespace std; 3 | int main(){ 4 | int t; 5 | cin>>t; 6 | while(t--){ 7 | int n; 8 | cin>>n; 9 | int arr[n]; 10 | for (int i = 0; i < n; i++) 11 | { 12 | cin>>arr[i]; 13 | } 14 | int mx = *max_element(arr,arr+n); 15 | int x = mx+1; 16 | int b[x] = {0}; 17 | for (int i = 0; i < n; i++) 18 | { 19 | b[arr[i]]++; 20 | } 21 | int ans = *max_element(b,b+x); 22 | for (int i = 0; i < x; i++) 23 | { 24 | if(b[i] == ans){ 25 | cout< 2 | using namespace std; 3 | 4 | void moveElements(int arr[], int n){ 5 | int key,j; 6 | for (int i = 0; i < n; i++) 7 | { 8 | key = arr[i]; 9 | j = i-1; 10 | while (j>=0 && arr[j]<0 && key>=0) 11 | { 12 | arr[j+1] = arr[j]; 13 | j--; 14 | } 15 | arr[j+1] = key; 16 | } 17 | } 18 | 19 | int main(){ 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /arrays/SecondMaximum.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | int main(){ 4 | int arr[5]; 5 | for (int i = 0; i < 5; i++) 6 | { 7 | cin>>arr[i]; 8 | } 9 | sort(arr,arr+5); 10 | for (int i = 0; i < 5; i++) 11 | { 12 | if(arr[i] == arr[i+1]){ 13 | arr[i] = 0; 14 | } 15 | } 16 | sort(arr,arr+5); 17 | cout< 2 | using namespace std; 3 | 4 | 5 | int arraysEqualorNot(vector A, vector B) { 6 | sort(A.begin(),A.end()); 7 | sort(B.begin(),B.end()); 8 | for (int i = 0; i < A.size(); i++) 9 | { 10 | if(A[i] != B[i]){ 11 | return 0; 12 | } 13 | } 14 | return 1; 15 | 16 | } 17 | 18 | int main(){ 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /arrays/countNegative.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | int main(){ 4 | int arr[10]; 5 | for (int i = 0; i < 10; i++) 6 | { 7 | cin>>arr[i]; 8 | } 9 | int neg(0),pos(0),even(0),odd(0); 10 | for (int i = 0; i < 10; i++) 11 | { 12 | if(arr[i]>=0){ 13 | pos++; 14 | } 15 | if(arr[i]<0){ 16 | neg++; 17 | } 18 | if(arr[i]%2 == 0){ 19 | even++; 20 | } 21 | else{ 22 | odd++; 23 | } 24 | } 25 | cout<=daysInMonth()){ 30 | month++; 31 | day=1; 32 | if(month>12){ 33 | month=1; 34 | } 35 | } 36 | } 37 | string toString(){ 38 | string result=""; 39 | result+=to_string(month); 40 | result+="/"; 41 | result+=to_string(day); 42 | return result; 43 | } 44 | 45 | 46 | int absoluteDay(){ 47 | int days; 48 | days=day; 49 | switch(getMonth()){ 50 | case 2: 51 | days+=31; 52 | break; 53 | case 3: 54 | days+=31+28; 55 | break; 56 | case 4: 57 | days+=31+28+31; 58 | break; 59 | case 5: 60 | days+=31+28+31+30; 61 | break; 62 | case 6: 63 | days+=31+28+31+30+31; 64 | break; 65 | case 7: 66 | days+=31+28+31+30+31+30; 67 | break; 68 | case 8: 69 | days+=31+28+31+30+31+30+31; 70 | break; 71 | case 9: 72 | days+=31+28+31+30+31+30+31+31; 73 | break; 74 | case 10: 75 | days+=31+28+31+30+31+30+31+31+30; 76 | break; 77 | case 11: 78 | days+=31+28+31+30+31+30+31+31+31; 79 | break; 80 | case 12: 81 | days+=31+28+31+30+31+30+31+31+31+30+30; 82 | break; 83 | } 84 | 85 | return days; 86 | } 87 | 88 | }; 89 | -------------------------------------------------------------------------------- /objectAndClasses/count.cpp: -------------------------------------------------------------------------------- 1 | int cnt=0; // manipluate this variable in your code 2 | class Counter 3 | { 4 | public: 5 | Counter(){ 6 | cnt++; 7 | //cout<<"c1 id "<= 60){ 20 | minutes -= 60; 21 | hours++; 22 | } 23 | } 24 | public: void add(TimeSpan tp){ 25 | add(tp.hours, tp.minutes); 26 | } 27 | public: double getTotalHours(){ 28 | return hours + minutes / 60.0; 29 | } 30 | public: std::string toString(){ 31 | std::string str{std::to_string(hours)+" Hours, "+std::to_string(minutes)+" Minutes"}; 32 | return str; 33 | } 34 | }; 35 | --------------------------------------------------------------------------------