├── Find the missing number.cpp ├── README.md ├── Shuffle an array.cpp ├── Sum in array.cpp ├── arrays ├── 3-stacks-using-array.cpp ├── array-product-puzzle.cpp ├── binary-search-rotated.cpp ├── equilibrium-index.cpp ├── find-2-smallest.cpp ├── find-duplicate-elements.cpp ├── inversion-count.cpp ├── k-largest-elements.cpp ├── leaders-in-array.cpp ├── majority-element-sorted-array.cpp ├── max-diff-2-elements.cpp ├── max-square-subarray.cpp ├── max-sum-non-adjacent.cpp ├── max-sum-subarray.cpp ├── median-2-sorted-array.cpp ├── merge-array-m+n.cpp ├── odd-occurence.cpp ├── pair-in-array-map.cpp ├── reverse-array.cpp ├── rotate-by-reverse.cpp ├── rotate-matrix-by-90.cpp ├── search-in-matrix.cpp ├── segregate-0s-and-1s.cpp ├── stack-using-arrays.cpp ├── sum-close-to-zero.cpp ├── union-intersection-sorted-array.cpp └── unsorted-subarray.cpp ├── circular-list-sorted-insert.cpp ├── clone-list-arbit-pointer.cpp ├── delete-linkedlist.cpp ├── delete-node-double-list.cpp ├── delete-node-linkedlist.cpp ├── double-list-XOR.cpp ├── dp ├── 0-1-knapsack.cpp ├── coin-change.cpp ├── egg-dropping-puzzle.cpp ├── lis-sum.cpp ├── longest-palindromic-sub.cpp ├── longest-unique-substr.cpp ├── matrix-chain-mul.cpp ├── max-contiguous-sum.cpp ├── min-cost-path.cpp ├── optimal-bst.cpp └── rod-cutting.cpp ├── factorial-large-int.cpp ├── factorial-not-tail-recursion.cpp ├── fibonacci.cpp ├── frequency-sort.cpp ├── getNth-from-last.cpp ├── getNth.cpp ├── great-tree-list-problem.cpp ├── left-child-inorder.cpp ├── linkedlist-middle.cpp ├── list-add-two-nos.cpp ├── list-alternate-split.cpp ├── list-common-node.cpp ├── list-del-n-after-m.cpp ├── list-delete-greater-right.cpp ├── list-detect-remove-loop.cpp ├── list-double-reverse.cpp ├── list-flatten.cpp ├── list-k-reverse.cpp ├── list-merge-alternate.cpp ├── list-merge-sort.cpp ├── list-pair-swap-pointers.cpp ├── list-remove-duplicates.cpp ├── list-reverse-alt-k.cpp ├── list-rotate-counter-clockwise.cpp ├── list-sep-odd-even.cpp ├── list-sort-0-1-2.cpp ├── list-sorted-intersection.cpp ├── list-sorted-merge.cpp ├── list-triplet-from-3.cpp ├── list-union-intersection-1.cpp ├── list-union-intersection-2.cpp ├── list-unsorted-remove-dup.cpp ├── lists-identical.cpp ├── majority-element.cpp ├── max-subarray.cpp ├── pair-swap-by-pointer.cpp ├── pair-swap-by-value.cpp ├── palindrome-singly-list.cpp ├── shortest-path-DAG.cpp ├── singleton-class.cpp ├── sorted-list-to-bst-inplace.cpp ├── sorted-list-to-bst.cpp ├── split-circular-list.cpp ├── square-root.cpp ├── stack-from-double-list.cpp ├── swap-kth-beg-end.cpp ├── trailing-zeros-factorial.cpp ├── tree-post-order-single-stack.cpp └── trees ├── binary-to-bst.cpp ├── bst-or-not.cpp ├── build-tree-pre-in.cpp ├── child-sum-prop.cpp ├── convert-child-sum-prop.cpp ├── double-tree.cpp ├── height-balanced.cpp ├── lca-bst.cpp ├── leaf-count.cpp ├── level-order-spiral.cpp ├── max-width-btree.cpp ├── mirror-tree.cpp ├── root-to-leaf-path-sum.cpp └── root-to-leaf-path.cpp /Find the missing number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Find the missing number 3 | You are given a list of n-1 integers and these integers are in the range of 1 to n. 4 | There are no duplicates in list. One of the integers is missing in the list. 5 | Write an efficient code to find the missing integer. 6 | 7 | http://www.geeksforgeeks.org/find-the-missing-number/ 8 | */ 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int missingNumber(int* arr, int size) { 15 | int sum; 16 | int n = size+1; 17 | // n = sizeof(arr); // this means the size of the pointer to array 18 | sum = n*(n+1)/2; 19 | for(int i=0; i>n; 29 | int arr[n-1]; 30 | for (int i=0; i>arr[i]; 32 | missing = missingNumber(arr,n-1); 33 | cout<<"The missing number is: "< 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | void swap(int*, int, int); 11 | 12 | void shuffleArray(int* arr, int size) { 13 | srand(time(NULL)); 14 | for (int i = size-1; i > 0; i--) 15 | swap(arr, i, rand()%i); 16 | return; 17 | } 18 | 19 | void swap(int* arr, int a, int b) { 20 | int temp = arr[a]; 21 | arr[a] = arr[b]; 22 | arr[b] = temp; 23 | return; 24 | } 25 | 26 | void printArr(int* arr, int size) { 27 | for (int i = 0; i < size; i++) 28 | cout<>size; 37 | int arr[size]; 38 | for (int i = 0; i < size; i++) 39 | cin>>arr[i]; 40 | cout<<"original: "< 10 | #include 11 | using namespace std; 12 | 13 | void sumInArray(int* arr, int size, int x) { 14 | int first = 0, last = size-1; 15 | while(last > first) { 16 | if(x == arr[first] + arr[last]) { 17 | cout< arr[first] + arr[last]) 21 | ++first; 22 | else --last; 23 | } 24 | cout< arr[i+1]) { 34 | flag = true; 35 | int temp = arr[i+1]; 36 | arr[i+1] = arr[i]; 37 | arr[i] = temp; 38 | } 39 | } 40 | } 41 | return; 42 | } 43 | 44 | int main() { 45 | freopen("input.txt","r",stdin); 46 | 47 | int size, x; 48 | cin>>size; 49 | int arr[size]; 50 | for(int i=0; i>arr[i]; 52 | cin>>x; 53 | bubbleSort(arr, size); 54 | sumInArray(arr, size, x); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /arrays/3-stacks-using-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Stack { 6 | public: 7 | int stackCount; 8 | int stackSize; 9 | int *buffer; 10 | int *stackPointer; 11 | Stack(int stackCount, int stackSize) { 12 | this->stackCount = stackCount; 13 | this->stackSize = stackSize; 14 | this->buffer = new int[stackCount * stackSize + 1]; 15 | this->stackPointer = new int[stackCount]; 16 | } 17 | // functions 18 | void push(int stackNum, int value); 19 | int pop(int stackNum); 20 | int peek(int stackNum); 21 | bool isEmpty(int stackNum); 22 | }; 23 | 24 | void Stack::push(int stackNum, int value) { 25 | if (stackPointer[stackNum] >= stackSize) { 26 | cout<push(0, 99); 59 | s1->push(0, 100); 60 | s1->push(0, 101); 61 | s1->push(0, 102); 62 | s1->push(0, 103); 63 | s1->push(0, 104); 64 | 65 | cout<<"peek: "<peek(0)<pop(0); 67 | cout<<"peek after pop: "<peek(0)<stackPointer[0]<isEmpty(0) ? cout<<"true" : cout<<"false"; 71 | 72 | s1->pop(0); 73 | s1->pop(0); 74 | s1->pop(0); 75 | s1->pop(0); 76 | s1->pop(0); 77 | 78 | cout<isEmpty(0) ? cout<<"true" : cout<<"false"; 80 | 81 | cout< 4 | using namespace std; 5 | 6 | void printArray(int *arr, int n); 7 | 8 | void productArray1(int *arr, int n) { 9 | int p[n]; 10 | int prod; 11 | for (int i = 0; i < n; i++) { 12 | prod = 1; 13 | for (int j = 0; j < n; j++) { 14 | if (j == i) 15 | continue; 16 | prod = prod * arr[j]; 17 | } 18 | p[i] = prod; 19 | } 20 | cout<<"product array O(n^2): "; 21 | printArray(p, n); 22 | } 23 | 24 | void productArray2(int *arr, int n) { 25 | int p[n]; 26 | for (int i = 0; i < n; i++) 27 | p[i] = 1; 28 | 29 | int temp = 1; 30 | for (int i = 0; i < n; i++) { 31 | p[i] = p[i] * temp; 32 | temp = temp * arr[i]; 33 | } 34 | temp = 1; 35 | for (int i = n-1; i >= 0; i--) { 36 | p[i] = p[i] * temp; 37 | temp = temp * arr[i]; 38 | } 39 | cout<<"product array O(n): "; 40 | printArray(p, n); 41 | } 42 | 43 | void printArray(int *arr, int n) { 44 | if (n < 1 || arr == NULL) 45 | return; 46 | for (int i = 0; i < n; i++) 47 | cout< 4 | using namespace std; 5 | 6 | void binarySearchRotated(int *arr, int beg, int end, int x); 7 | void printArray(int *arr, int size); 8 | 9 | void binarySearchRotated(int *arr, int beg, int end, int x) { 10 | if (beg > end) { 11 | cout<<"element not found!"; 12 | return; 13 | } 14 | 15 | int mid = (beg + end) / 2; 16 | if (x == arr[mid]) { 17 | cout<<"element found at pos: "< arr[mid] && x <= arr[end]) 23 | binarySearchRotated(arr, mid+1, end, x); 24 | else binarySearchRotated(arr, beg, mid-1, x); 25 | } 26 | // left half of rotated array is sorted 27 | else { 28 | if (x >= arr[beg] && x < arr[mid]) 29 | binarySearchRotated(arr, beg, mid-1, x); 30 | else binarySearchRotated(arr, mid+1, end, x); 31 | } 32 | } 33 | 34 | void printArray(int *arr, int size) { 35 | if (size < 1 || arr == NULL) 36 | return; 37 | for (int i = 0; i < size; i++) 38 | cout< 4 | using namespace std; 5 | 6 | void findEquilibrium1(int *arr, int n); // complexity O(n^2) 7 | void findEquilibrium2(int *arr, int n); // complexity O(n) 8 | void printArray(int *arr, int n); 9 | 10 | void findEquilibrium1(int *arr, int n) { 11 | int left_sum; 12 | int right_sum; 13 | for (int i = 0; i < n; i++) { 14 | left_sum = 0; 15 | right_sum = 0; 16 | for (int j = 0; j < i; j++) 17 | left_sum += arr[j]; 18 | for (int j = i+1; j < n; j++) 19 | right_sum += arr[j]; 20 | if (left_sum == right_sum) 21 | cout< 4 | using namespace std; 5 | 6 | void printArray(int *arr, int n); 7 | 8 | void findTwoSmallest(int *arr, int n) { 9 | if (n < 2) 10 | return; 11 | 12 | int small, second_small; 13 | small = (arr[0] < arr[1]) ? arr[0] : arr[1]; 14 | second_small = (arr[0] < arr[1]) ? arr[1] : arr[0]; 15 | 16 | for (int i = 2; i < n; i++) { 17 | if (arr[i] < small) { 18 | second_small = small; 19 | small = arr[i]; 20 | } 21 | else if (arr[i] > small && arr[i] < second_small) 22 | second_small = arr[i]; 23 | } 24 | cout<<"smallest: "< 4 | using namespace std; 5 | 6 | void findRepeatElements(int *arr, int n); 7 | void printArray(int *arr, int n); 8 | 9 | void findRepeatElements(int *arr, int n) { 10 | for (int i = 0; i < n; i++) 11 | arr[arr[i] % n] += n; 12 | for (int i = 0; i < n ; i++) 13 | if (arr[i]/n > 1) 14 | cout< 4 | using namespace std; 5 | 6 | int inversionCount1(int *arr, int n); // O(n^2) 7 | int inversionCount2(int *arr, int n); // O(nlogn) 8 | int mergeSort(int *arr, int beg, int end); 9 | int sortedMerge(int *arr, int beg, int mid, int end); 10 | void printArray(int *arr, int size); 11 | 12 | int inversionCount1(int *arr, int n) { 13 | int inv_count = 0; 14 | for (int i = 0; i < n-1; i++) 15 | for (int j = i+1; j < n; j++) 16 | if (arr[i] > arr[j]) 17 | inv_count++; 18 | return inv_count; 19 | } 20 | 21 | int inversionCount2(int *arr, int n) { 22 | int beg = 0; 23 | int end = n-1; 24 | int inv_count; 25 | inv_count = mergeSort(arr, beg, end); 26 | 27 | return inv_count; 28 | } 29 | 30 | int mergeSort(int *arr, int beg, int end) { 31 | if (beg >= end) 32 | return 0; 33 | 34 | int left_count = 0, right_count = 0, cross_count = 0; 35 | int mid = (beg+end)/2; 36 | left_count = mergeSort(arr, beg, mid); 37 | right_count = mergeSort(arr, mid+1, end); 38 | cross_count = sortedMerge(arr, beg, mid, end); 39 | 40 | return (left_count + right_count + cross_count); 41 | } 42 | 43 | int sortedMerge(int *arr, int beg, int mid, int end) { 44 | int inv_count = 0; 45 | int i = beg, j = mid+1, k = beg; 46 | int temp[end+1]; 47 | while (i <= mid && j <= end) { 48 | if (arr[i] < arr[j]) 49 | temp[k++] = arr[i++]; 50 | else { 51 | temp[k++] = arr[j++]; 52 | inv_count = inv_count + (mid - i + 1); 53 | } 54 | } 55 | while (i <= mid) 56 | temp[k++] = arr[i++]; 57 | while (j <= end) 58 | temp[k++] = arr[j++]; 59 | for (i = beg; i <= end; i++) 60 | arr[i] = temp[i]; 61 | 62 | return inv_count; 63 | } 64 | 65 | void printArray(int *arr, int size) { 66 | if (size < 1 || arr == NULL) 67 | return; 68 | for (int i = 0; i < size; i++) 69 | cout< 4 | using namespace std; 5 | 6 | void printLargestK(int *arr, int n, int k); 7 | void buildMaxHeap(int *arr, int n); 8 | void heapifyMax(int *arr, int n, int i); 9 | int heapExtractMax(int *arr, int n); 10 | void swap(int* arr, int a, int b); 11 | void printArray(int *arr, int n); 12 | 13 | void printLargestK(int *arr, int n, int k) { 14 | buildMaxHeap(arr, n); 15 | cout<<"K largest are: "; 16 | for (int i = 0; i < k; i++) 17 | cout<= 0; i--) 22 | heapifyMax(arr, n, i); 23 | } 24 | 25 | void heapifyMax(int *arr, int n, int i) { 26 | int left = 2*i+1; 27 | int right = 2*i+2; 28 | int largest = i; 29 | if (left < n && arr[left] > arr[largest]) 30 | largest = left; 31 | if (right < n && arr[right] > arr[largest]) 32 | largest = right; 33 | if (largest != i) { 34 | swap(arr, i, largest); 35 | heapifyMax(arr, n, largest); 36 | } 37 | } 38 | 39 | int heapExtractMax(int *arr, int n) { 40 | int temp = arr[0]; 41 | arr[0] = arr[n-1]; 42 | n = n-1; 43 | heapifyMax(arr, n, 0); 44 | return temp; 45 | } 46 | 47 | void swap(int* arr, int a, int b) { 48 | int temp = arr[a]; 49 | arr[a] = arr[b]; 50 | arr[b] = temp; 51 | return; 52 | } 53 | 54 | void printArray(int *arr, int n) { 55 | if (n < 1 || arr == NULL) 56 | return; 57 | for (int i = 0; i < n; i++) 58 | cout< 4 | using namespace std; 5 | 6 | void printLeader1(int *arr, int n); // complexity O(n^2) 7 | void printLeader2(int *arr, int n); // complexity O(nlogn) 8 | void printArray(int *arr, int n); 9 | 10 | void printLeader1(int *arr, int n) { 11 | int i, j; 12 | for (i = 0; i < n; i++) { 13 | for (j = i+1; j < n; j++) { 14 | if (arr[i] <= arr[j]) 15 | break; 16 | } 17 | if (j == n) 18 | cout<= 0; i--) { 26 | if (arr[i] > max_from_right) { 27 | cout< "; 48 | printLeader1(arr, n); 49 | 50 | cout< "; 51 | printLeader2(arr, n); 52 | 53 | cout< 4 | using namespace std; 5 | 6 | bool isMajority(int *arr, int n, int x); // complexity O(logn) 7 | int binarySearch(int *arr, int n, int x); // returns first occurence of x 8 | void printArray(int *arr, int n); 9 | 10 | bool isMajority(int *arr, int n, int x) { 11 | // if x is not middle element, it can never be majority element 12 | int mid = n/2; 13 | if (x != arr[mid]) 14 | return false; 15 | 16 | // find first index of x, and then check for x at (i + n/2) 17 | int i = binarySearch(arr, n , x); 18 | if ((i + n/2) < n && x == arr[i + n/2]) 19 | return true; 20 | return false; 21 | } 22 | 23 | int binarySearch(int *arr, int n, int x) { 24 | int beg = 0; 25 | int end = n-1; 26 | while (beg <= end) { 27 | int mid = (beg+end)/2; 28 | if ( (mid == 0 || x > arr[mid-1]) && x == arr[mid] ) 29 | return mid; 30 | else if (x > arr[mid]) 31 | beg = mid+1; 32 | else end = mid-1; 33 | } 34 | return -1; 35 | } 36 | 37 | void printArray(int *arr, int n) { 38 | if (n < 1 || arr == NULL) 39 | return; 40 | for (int i = 0; i < n; i++) 41 | cout< 4 | using namespace std; 5 | 6 | int maxDiff1(int *arr, int n); // complexity O(n^2) 7 | int maxDiff2(int *arr, int n); // complexity O(n) 8 | void printArray(int *arr, int n); 9 | 10 | int maxDiff1(int *arr, int n) { 11 | int max_diff = 0; 12 | for (int i = 0; i < n-1; i++) 13 | for (int j = i+1; j < n; j++) 14 | if (arr[j] - arr[i] > max_diff) 15 | max_diff = arr[j] - arr[i]; 16 | return max_diff; 17 | } 18 | 19 | int maxDiff2(int *arr, int n) { 20 | int min_ele = arr[0]; 21 | int max_diff = arr[1] - arr[0]; 22 | for (int i = 0; i < n; i++) { 23 | if (arr[i] - min_ele > max_diff) 24 | max_diff = arr[i] - min_ele; 25 | if (arr[i] < min_ele) 26 | min_ele = arr[i]; 27 | } 28 | return max_diff; 29 | } 30 | 31 | void printArray(int *arr, int n) { 32 | if (n < 1 || arr == NULL) 33 | return; 34 | for (int i = 0; i < n; i++) 35 | cout< O(n^2): "; 46 | cout< O(n): "; 48 | cout< 2 | #define R 6 3 | #define C 5 4 | using namespace std; 5 | 6 | void findMaxSubSquare(int M[R][C]); 7 | int findMin(int a, int b, int c); 8 | 9 | void findMaxSubSquare(int M[R][C]) { 10 | int S[R][C] ; 11 | int i, j; 12 | // copy first row and col as it is 13 | for (i = 0; i < R; i++) 14 | S[i][0] = M[i][0]; 15 | for (i = 0; i < C; i++) 16 | S[0][i] = M[0][i]; 17 | for (i = 1; i < R; i++) { 18 | for (j = 1; j < C; j++) { 19 | if (M[i][j] == 1) 20 | S[i][j] = findMin(S[i-1][j], S[i][j-1], S[i-1][j-1]) + 1; 21 | else 22 | S[i][j] = 0; 23 | } 24 | } 25 | // find max value in S[R][C] 26 | int max = 0, max_i = 0, max_j = 0; 27 | for (i = 0; i < R; i++) 28 | for (j = 0; j < C; j++) 29 | if (S[i][j] > max) { 30 | max = S[i][j]; 31 | max_i = i; 32 | max_j = j; 33 | } 34 | // print max square matrix using max value and max_i & max_j indices 35 | for (i = (max_i - max + 1); i <= max_i; i++) { 36 | for (j = (max_j - max + 1); j <= max_j; j++) 37 | cout< b) ? b : a; 45 | return (temp > c) ? c : temp; 46 | } 47 | 48 | int main() { 49 | int M[R][C] = { {0, 1, 1, 0, 1}, 50 | {1, 1, 0, 1, 0}, 51 | {0, 1, 1, 1, 0}, 52 | {1, 1, 1, 1, 0}, 53 | {1, 1, 1, 1, 1}, 54 | {0, 0, 0, 0, 0} 55 | }; 56 | 57 | findMaxSubSquare(M); 58 | 59 | cout< 4 | using namespace std; 5 | 6 | void printArray(int *arr, int size); 7 | 8 | int findMaxSum(int *arr, int n) { 9 | int incl = arr[0]; 10 | int excl = 0; 11 | for (int i = 1; i < n; i++) { 12 | int temp = max(incl, excl); 13 | incl = excl + arr[i]; 14 | excl = temp; 15 | } 16 | return max(incl, excl); 17 | } 18 | 19 | void printArray(int *arr, int size) { 20 | if (size < 1 || arr == NULL) 21 | return; 22 | for (int i = 0; i < size; i++) 23 | cout< 5 | using namespace std; 6 | 7 | int findMaxSumSubarray(int *arr, int size); 8 | void printArray(int *arr, int size); 9 | 10 | int findMaxSumSubarray(int *arr, int size) { 11 | int max_so_far = 0; 12 | int max_here = 0; 13 | int start_index = 0, end_index = 0; 14 | for (int i = 0; i < size; i++) { 15 | max_here = max_here + arr[i]; 16 | if (max_here < 0) { 17 | max_here = 0; 18 | // if max_here resets, index starts from next element 19 | start_index = i+1; 20 | } 21 | if (max_here > max_so_far) { 22 | max_so_far = max_here; 23 | // end index is when max_so_far is set last time 24 | end_index = i; 25 | } 26 | } 27 | // if max sum consists of single element 28 | if (start_index > end_index) 29 | start_index = end_index; 30 | cout< 4 | using namespace std; 5 | 6 | void findMedian(int *arr1, int n1, int *arr2, int n2); 7 | int sortedMergeTillHalf(int *result, int *arr1, int n1, int *arr2, int n2); 8 | void printArray(int *arr, int size); 9 | 10 | void findMedian(int *arr1, int n1, int *arr2, int n2) { 11 | int result[1000] = {0}; 12 | int median = sortedMergeTillHalf(result, arr1, n1, arr2, n2); 13 | if ((n1+n2) % 2 != 0) 14 | cout< 4 | #include 5 | #define NA INT_MAX 6 | using namespace std; 7 | 8 | void binarySearchRotated(int *arr, int beg, int end, int x); 9 | void moveToEnd(int *arr, int size); 10 | void printArray(int *arr, int size); 11 | 12 | void mergemPlusN(int *mPlusN, int size_mplusn, int *N, int size_n) { 13 | moveToEnd(mPlusN, size_mplusn); 14 | int i = size_n, j = 0, k = 0; 15 | while (i < size_mplusn && j < size_n) { 16 | if (mPlusN[i] < N[j]) 17 | mPlusN[k++] = mPlusN[i++]; 18 | else 19 | mPlusN[k++] = N[j++]; 20 | } 21 | while (i < size_mplusn) 22 | mPlusN[k++] = mPlusN[i++]; 23 | while (j < size_n) 24 | mPlusN[k++] = N[j++]; 25 | } 26 | 27 | void moveToEnd(int *arr, int size) { 28 | int j = size-1; 29 | for (int i = size-2; i >= 0; i--) { 30 | if (arr[i] != NA) { 31 | arr[j] = arr[i]; 32 | arr[i] = NA; 33 | j--; 34 | } 35 | } 36 | } 37 | 38 | void printArray(int *arr, int size) { 39 | if (size < 1 || arr == NULL) 40 | return; 41 | for (int i = 0; i < size; i++) { 42 | if (arr[i] == NA) 43 | cout<<"NA "; 44 | else cout< 4 | using namespace std; 5 | 6 | void findOdd(int *arr, int size); 7 | void printArray(int *arr, int size); 8 | 9 | void findOdd(int *arr, int size) { 10 | int odd = 0; 11 | for (int i = 0; i < size; i++) 12 | odd = odd ^ arr[i]; 13 | cout< 5 | using namespace std; 6 | 7 | void findPairInArray(int *arr, int size, int sum); 8 | void printArray(int *arr, int size); 9 | 10 | void findPairInArray(int *arr, int size, int sum) { 11 | bool bin_map[1000] = {0}; 12 | int x; 13 | bool flag = false; 14 | for (int i = 0; i < size; i++) { 15 | x = sum - arr[i]; 16 | if (x >= 0 && bin_map[x] == 1) { 17 | cout< 4 | using namespace std; 5 | 6 | void reverseArrayIterative(int *arr, int size); 7 | void swapElement(int *arr, int a, int b); 8 | void printArray(int *arr, int size); 9 | 10 | void reverseArrayIterative(int *arr, int size) { 11 | int beg = 0, end = size-1; 12 | while (beg < end) { 13 | swapElement(arr, beg, end); 14 | beg++; 15 | end--; 16 | } 17 | } 18 | 19 | void reverseArrayRecursive(int *arr, int beg, int end) { 20 | if (beg > end) 21 | return; 22 | swapElement(arr, beg, end); 23 | reverseArrayRecursive(arr, ++beg, --end); 24 | } 25 | 26 | void swapElement(int *arr, int a, int b) { 27 | int temp = arr[a]; 28 | arr[a] = arr[b]; 29 | arr[b] = temp; 30 | } 31 | 32 | void printArray(int *arr, int size) { 33 | if (size < 1 || arr == NULL) 34 | return; 35 | for (int i = 0; i < size; i++) 36 | cout< 4 | using namespace std; 5 | 6 | void rotateLeftByReverse(int *arr, int n, int d); 7 | void rotateRightByReverse(int *arr, int n, int d); 8 | void reverseArrayRecursive(int *arr, int beg, int end); 9 | void swapElement(int *arr, int a, int b); 10 | void printArray(int *arr, int size); 11 | 12 | void rotateLeftByReverse(int *arr, int n, int d) { 13 | if (d > n) 14 | d = d-n; 15 | reverseArrayRecursive(arr, 0, d-1); 16 | reverseArrayRecursive(arr, d, n-1); 17 | reverseArrayRecursive(arr, 0, n-1); 18 | } 19 | 20 | void rotateRightByReverse(int *arr, int n, int d) { 21 | if (d > n) 22 | d = d-n; 23 | reverseArrayRecursive(arr, n-d, n-1); 24 | reverseArrayRecursive(arr, 0, n-d-1); 25 | reverseArrayRecursive(arr, 0, n-1); 26 | } 27 | 28 | void reverseArrayRecursive(int *arr, int beg, int end) { 29 | if (beg > end) 30 | return; 31 | swapElement(arr, beg, end); 32 | reverseArrayRecursive(arr, ++beg, --end); 33 | } 34 | 35 | void swapElement(int *arr, int a, int b) { 36 | int temp = arr[a]; 37 | arr[a] = arr[b]; 38 | arr[b] = temp; 39 | } 40 | 41 | void printArray(int *arr, int size) { 42 | if (size < 1 || arr == NULL) 43 | return; 44 | for (int i = 0; i < size; i++) 45 | cout< 4 | using namespace std; 5 | 6 | void rotate90(int *pSrc, int *pDest, int rows, int cols); 7 | void printMatrix(int *m, int rows, int cols); 8 | 9 | void rotate90(int *pSrc, int *pDest, int rows, int cols) { 10 | int i, j, k, l; 11 | for (i = 0, l = rows-1; i < rows; i++, l--) 12 | for (j = 0, k = 0; j < cols; j++, k++) 13 | *(pDest + k*rows + l) = *(pSrc + i*cols + j); 14 | } 15 | 16 | void printMatrix(int *m, int rows, int cols) { 17 | if (m == NULL) 18 | return; 19 | for (int i = 0; i < rows; i++) { 20 | for (int j = 0; j < cols; j++) 21 | cout<<*(m + i*cols + j)<<" "; 22 | cout< 4 | using namespace std; 5 | 6 | void printMatrix(int *m, int rows, int cols); 7 | 8 | bool findEleInMatrix(int *mat, int rows, int cols, int x) { 9 | int i = 0, j = cols-1; 10 | while (i < rows && j >= 0) { 11 | if (x == *(mat + i*cols + j)) { 12 | cout< *(mat + i*cols + j)) 16 | i++; 17 | else j--; 18 | } 19 | return false; 20 | } 21 | 22 | void printMatrix(int *mat, int rows, int cols) { 23 | if (mat == NULL) 24 | return; 25 | for (int i = 0; i < rows; i++) { 26 | for (int j = 0; j < cols; j++) 27 | cout<<*(mat + i*cols + j)<<" "; 28 | cout< 4 | using namespace std; 5 | 6 | void segregate0and1(int *arr, int n); 7 | void printArray(int *arr, int n); 8 | 9 | void segregate0and1(int *arr, int n) { 10 | int left = 0; 11 | int right = n-1; 12 | while (left < right) { 13 | while (left < right && arr[left] == 0) 14 | left++; 15 | while (left < right && arr[right] == 1) 16 | right--; 17 | // if upper loops break on arr[left] == 0 or arr[right] == 1, swap left and right 18 | if (left < right) { 19 | arr[left] = 0; 20 | arr[right] = 1; 21 | } 22 | left++; 23 | right--; 24 | } 25 | } 26 | 27 | void printArray(int *arr, int n) { 28 | if (n < 1 || arr == NULL) 29 | return; 30 | for (int i = 0; i < n; i++) 31 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int value; 8 | int min_beneath; 9 | node *next; 10 | node(int value, int min_beneath) { 11 | this->value = value; 12 | this->min_beneath = min_beneath; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | node* push(node *top, int value) { 18 | if (top == NULL) 19 | return new node(value, value); 20 | int min = (top->min_beneath < value) ? top->min_beneath : value; 21 | node *ptr = new node(value, min); 22 | ptr->next = top; 23 | top = ptr; 24 | return top; 25 | } 26 | 27 | node* pop(node *top) { 28 | if (top == NULL) { 29 | cout<<"Stack underflow.."<next; 33 | return top; 34 | } 35 | 36 | int peek(node *top) { 37 | if (top == NULL) { 38 | cout<<"Stack empty.."<value; 42 | } 43 | 44 | int minValue(node *top) { 45 | if (top == NULL) { 46 | cout<<"Stack empty.."<min_beneath; 50 | } 51 | 52 | bool isEmpty(node *top) { 53 | if (top == NULL) 54 | return true; 55 | return false; 56 | } 57 | 58 | int main() 59 | { 60 | // freopen("input.txt","r",stdin); 61 | node *top = push(top, 5); 62 | top = push(top, 6); 63 | top = push(top, 1); 64 | top = push(top, 8); 65 | 66 | int temp = peek(top); 67 | cout<<"peek: "<value<value< 4 | using namespace std; 5 | 6 | void sumCloseToZero1(int *arr, int n); // complexity O(n^2) 7 | void sumCloseToZero2(int *arr, int n); // complexity O(nlogn) 8 | void printArray(int *arr, int n); 9 | 10 | void sumCloseToZero1(int *arr, int n) { 11 | if (n < 2) 12 | return; 13 | int l_index = 0, r_index = 0, min_sum, sum; 14 | min_sum = arr[0] + arr[1]; 15 | for (int i = 0; i < n-1; i++) 16 | for (int j = i+1; j < n; j++) { 17 | sum = arr[i] + arr[j]; 18 | if (abs(sum) < abs(min_sum)) { 19 | min_sum = sum; 20 | l_index = i; 21 | r_index = j; 22 | } 23 | } 24 | cout<<"min sum: "< "; 66 | sumCloseToZero1(arr, n); 67 | 68 | cout< "; 69 | sumCloseToZero2(arr, n); 70 | 71 | cout< 4 | using namespace std; 5 | 6 | void unionArray(int *arr1, int n1, int *arr2, int n2); 7 | void intersectionArray(int *arr1, int n1, int *arr2, int n2); 8 | void printArray(int *arr, int n); 9 | 10 | void unionArray(int *arr1, int n1, int *arr2, int n2) { 11 | int i = 0, j = 0; 12 | while (i < n1 && j < n2) { 13 | if (arr1[i] < arr2[j]) 14 | cout< 4 | using namespace std; 5 | 6 | void findUnsortedSubarray(int *arr, int n); 7 | void printArray(int *arr, int n); 8 | 9 | void findUnsortedSubarray(int *arr, int n) { 10 | int s, e, max, min; 11 | for (s = 0; s < n-1; s++) 12 | if (arr[s] > arr[s+1]) 13 | break; 14 | for (e = n-1; e > 0; e--) 15 | if (arr[e-1] > arr[e]) 16 | break; 17 | max = s, min = s; 18 | for (int i = s; i <= e; i++) { 19 | if (arr[i] > arr[max]) 20 | max = i; 21 | if (arr[i] < arr[min]) 22 | min = i; 23 | } 24 | for (int i = 0; i < s; i++) 25 | if (arr[i] > arr[min]) 26 | s = i; 27 | for (int i = n-1; i > e; i--) 28 | if (arr[i] < arr[max]) 29 | e = i; 30 | cout<<"unsorted subarray.."< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | node* sortedInsertnode(node *head, int ele); 17 | void makeCircular(node *head); 18 | node* addnode(node *head, int data); 19 | void printCircularList(node *head); 20 | 21 | node* sortedInsertnode(node *head, int ele) { 22 | node *newnode = new node(ele); 23 | // case 1: list empty 24 | if (head == NULL) { 25 | head = newnode; 26 | head->next = head; 27 | } 28 | // case 2: insertion before head 29 | else if (ele <= head->data) { 30 | node *ptr = head; 31 | while (ptr->next != head) 32 | ptr = ptr->next; 33 | ptr->next = newnode; 34 | newnode->next = head; 35 | head = newnode; 36 | } 37 | // case 3: insertion after head 38 | else { 39 | node *ptr = head, *start = head, *temp; 40 | while (ptr->next != start && ptr->data < ele) { 41 | temp = ptr; 42 | ptr = ptr->next; 43 | } 44 | if (ptr->next == start && ptr->data < ele) { 45 | newnode->next = ptr->next; 46 | ptr->next = newnode; 47 | } 48 | else { 49 | newnode->next = temp->next; 50 | temp->next = newnode; 51 | } 52 | } 53 | return head; 54 | } 55 | 56 | void makeCircular(node *head) { 57 | if (head == NULL) 58 | return; 59 | node *ptr = head; 60 | while (ptr->next != NULL) 61 | ptr = ptr->next; 62 | ptr->next = head; 63 | } 64 | 65 | node* addnode(node *head, int data) { 66 | if (head == NULL) 67 | return new node(data); 68 | node *ptr = head; 69 | while (ptr->next != NULL) 70 | ptr = ptr->next; 71 | ptr->next = new node(data); 72 | return head; 73 | } 74 | 75 | void printCircularList(node *head) { 76 | if (head == NULL) 77 | return; 78 | node *start = head; 79 | while (head->next != start) { 80 | cout<data<<" "; 81 | head = head->next; 82 | } 83 | cout<data; 84 | } 85 | 86 | int main() { 87 | node *head = NULL; 88 | head = addnode(head, 2); 89 | head = addnode(head, 5); 90 | head = addnode(head, 8); 91 | head = addnode(head, 10); 92 | makeCircular(head); 93 | 94 | cout<<"original: "; 95 | printCircularList(head); 96 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *arbit, *next; 10 | Node(int data) { 11 | this->data = data; 12 | arbit = next = NULL; 13 | } 14 | }; 15 | 16 | Node* copyList(Node *orig); 17 | Node* createList(int n); 18 | void printList(Node *head); 19 | void printArbitList(Node *head); 20 | 21 | Node* copyList(Node *orig) { 22 | // create the copy of node 1 and insert it b/w node 1 & node 2 in original list and 23 | // repeat this for all the nodes in the original list 24 | Node *prev, *curr, *next; 25 | prev = orig; 26 | next = orig->next; 27 | int i = 1; 28 | while (next != NULL) { 29 | curr = new Node(i++); 30 | curr->next = next; 31 | prev->next = curr; 32 | prev = next; 33 | next = next->next; 34 | } 35 | prev->next = new Node(i); 36 | 37 | // copy the arbit linking of original list 38 | Node *temp_orig = orig; 39 | while (temp_orig != NULL) { 40 | temp_orig->next->arbit = temp_orig->arbit->next; 41 | temp_orig = temp_orig->next->next; 42 | } 43 | 44 | // separate the original and copy list 45 | Node* copy = orig->next; 46 | Node *temp_copy = copy; 47 | temp_orig = orig; 48 | while (temp_copy->next != NULL) { 49 | temp_orig->next = temp_orig->next->next; 50 | temp_orig = temp_orig->next; 51 | 52 | temp_copy->next = temp_copy->next->next; 53 | temp_copy = temp_copy->next; 54 | } 55 | temp_orig->next = NULL; 56 | 57 | // return the head pointer to copied list 58 | return copy; 59 | } 60 | 61 | Node* createList(int n) { 62 | Node *head = new Node(1); 63 | Node *ptr, *ptr1; 64 | ptr = ptr1 = head; 65 | for (int i = 2; i <= n; i++) { 66 | ptr1 = new Node(i); 67 | ptr->next = ptr1; 68 | ptr = ptr1; 69 | } 70 | return head; 71 | } 72 | 73 | void printList(Node *head) { 74 | if (head == NULL) 75 | return; 76 | while (head != NULL) { 77 | cout<data<<" "; 78 | head = head->next; 79 | } 80 | } 81 | 82 | void printArbitList(Node *head) { 83 | if (head == NULL) 84 | return; 85 | while (head != NULL) { 86 | cout<data<<"->"<arbit->data<<" . "; 87 | head = head->next; 88 | } 89 | } 90 | 91 | int main() 92 | { 93 | Node *head = createList(5); 94 | // create arbit linking of original list 95 | Node *ptr = head; 96 | ptr->arbit = ptr->next->next; // 1 -> 3 97 | ptr->next->arbit = ptr; // 2 -> 1 98 | ptr->next->next->arbit = ptr->next->next->next->next; // 3 -> 5 99 | ptr->next->next->next->arbit = ptr->next; // 4 -> 2 100 | ptr->next->next->next->next->arbit = ptr; // 5 -> 1 101 | 102 | cout<<"original: "; 103 | printList(head); 104 | cout< 6 | #include 7 | using namespace std; 8 | 9 | class Node { 10 | public: 11 | int data; 12 | Node* next; 13 | Node(int data) { 14 | this->data = data; 15 | next = NULL; 16 | } 17 | }; 18 | 19 | class LinkedList { 20 | public: 21 | Node* start; 22 | int size; 23 | LinkedList(Node* start, int size) { 24 | this->start = start; 25 | this->size = size; 26 | } 27 | }; 28 | 29 | Node* createLinkedList(int size); 30 | void deleteList(LinkedList* L); 31 | void printLL(LinkedList* L); 32 | 33 | Node* createLinkedList(int size) { 34 | if (size < 1) 35 | return NULL; 36 | Node* start = new Node(1); 37 | Node* ptr = start; 38 | Node* ptr1; 39 | for (int i=2; i<=size; i++) { 40 | ptr1 = new Node(i); 41 | ptr->next = ptr1; 42 | ptr = ptr->next; 43 | } 44 | return start; 45 | } 46 | 47 | void deleteList(LinkedList* L) { 48 | if (!L || !L->start) { 49 | cout<start; 53 | Node* curr = start->next; 54 | Node* prev = start; 55 | while (curr) { 56 | delete(prev); 57 | prev = curr; 58 | curr = curr->next; 59 | } 60 | L->start = NULL; 61 | L->size = 0; // setting new size of linked list to zero 62 | return; 63 | } 64 | 65 | void printLL(LinkedList* L) { 66 | if (!L || !L->start) { 67 | cout<start; 71 | while (start) { 72 | cout<data<<" "; 73 | start = start->next; 74 | } 75 | return; 76 | } 77 | 78 | int main() { 79 | freopen("input.txt","r",stdin); 80 | 81 | int size, index; 82 | cin>>size>>index; 83 | LinkedList* L = new LinkedList(createLinkedList(size), size); 84 | cout<<"List: "; 85 | printLL(L); 86 | deleteList(L); 87 | printLL(L); 88 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *left, *right; 10 | Node(int data) { 11 | this->data = data; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | 17 | void deleteNode(Node *head, int data); 18 | Node* addNode(Node *head, int data); 19 | void printLeftToRight(Node *head); 20 | void printLeftToRight(Node *head); 21 | 22 | void deleteNode(Node *head, int data) { 23 | if (head == NULL) 24 | return; 25 | Node *ptr = head; 26 | while (ptr != NULL && ptr->data != data) 27 | ptr = ptr->right; 28 | // element not in list 29 | if (ptr == NULL) { 30 | cout<right == NULL) { 35 | ptr->left->right = NULL; 36 | return; 37 | } 38 | // element is anything except last 39 | ptr->data = ptr->right->data; 40 | ptr->right = ptr->right->right; 41 | if (ptr->right != NULL) 42 | ptr->right->left = ptr; 43 | } 44 | 45 | Node* addNode(Node *head, int data) { 46 | if (head == NULL) 47 | return new Node(data); 48 | Node *ptr = head; 49 | while (ptr->right != NULL) 50 | ptr = ptr->right; 51 | ptr->right = new Node(data); 52 | ptr->right->left = ptr; 53 | return head; 54 | } 55 | 56 | void printLeftToRight(Node *head) { 57 | if (head == NULL) 58 | return; 59 | while (head != NULL) { 60 | cout<data<<" "; 61 | head = head->right; 62 | } 63 | } 64 | 65 | void printRightToLeft(Node *head) { 66 | if (head == NULL) 67 | return; 68 | while (head != NULL) { 69 | cout<data<<" "; 70 | head = head->left; 71 | } 72 | } 73 | 74 | int main() { 75 | Node *head = NULL; 76 | head = addNode(head, 1); 77 | head = addNode(head, 2); 78 | head = addNode(head, 3); 79 | head = addNode(head, 4); 80 | head = addNode(head, 5); 81 | 82 | cout<<"original: "; 83 | printLeftToRight(head); 84 | cout< 6 | #include 7 | using namespace std; 8 | 9 | class Node { 10 | public: 11 | int data; 12 | Node* next; 13 | Node(int data) { 14 | this->data = data; 15 | next = NULL; 16 | } 17 | }; 18 | 19 | class LinkedList { 20 | public: 21 | Node* start; 22 | int size; 23 | LinkedList(Node* start, int size) { 24 | this->start = start; 25 | this->size = size; 26 | } 27 | }; 28 | 29 | Node* createLinkedList(int size); 30 | void deleteNode(Node* ptr); 31 | void printLL(LinkedList* L); 32 | 33 | Node* createLinkedList(int size) { 34 | if (size < 1) 35 | return NULL; 36 | Node* start = new Node(1); 37 | Node* ptr = start; 38 | Node* ptr1; 39 | for (int i=2; i<=size; i++) { 40 | ptr1 = new Node(i); 41 | ptr->next = ptr1; 42 | ptr = ptr->next; 43 | } 44 | return start; 45 | } 46 | 47 | void deleteNode(Node* ptr) { 48 | if (!ptr) 49 | return; 50 | Node* ptr1 = ptr->next; 51 | ptr->data = ptr1->data; 52 | ptr->next = ptr1->next; 53 | delete(ptr1); 54 | return; 55 | } 56 | 57 | void printLL(LinkedList* L) { 58 | Node* start = L->start; 59 | if (!start) { 60 | cout<<"List empty.."<data<<" "; 65 | start = start->next; 66 | } 67 | return; 68 | } 69 | 70 | int main() { 71 | freopen("input.txt","r",stdin); 72 | 73 | int size; 74 | cin>>size; 75 | LinkedList* L = new LinkedList(createLinkedList(size), size); 76 | cout<<"List: "; 77 | printLL(L); 78 | deleteNode(L->start); 79 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *npx; 10 | node(int data) { 11 | this->data = data; 12 | this->npx = NULL; 13 | } 14 | }; 15 | 16 | node* addnode(node *head, int data); // adds node to double list 17 | node* XOR(node* a, node* b); // returns XOR of two pointers of node type 18 | void printList(node *head); // traverses double list L->R starting with head 19 | 20 | node* addnode(node *head, int data) { 21 | // if list is empty 22 | if (head == NULL) 23 | return new node(data); 24 | // if list is not empty, traverse till end and add node 25 | node *prev = NULL; 26 | node *curr = head; 27 | node* temp; 28 | while (curr->npx != XOR(prev, NULL)) { 29 | temp = curr; 30 | curr = XOR(curr->npx, prev); 31 | prev = temp; 32 | } 33 | // curr now points to last node 34 | temp = new node(data); 35 | curr->npx = XOR(prev, temp); 36 | temp->npx = XOR(curr, NULL); 37 | 38 | return head; 39 | } 40 | 41 | node* XOR(node* a, node* b) { 42 | return (node*) ((unsigned long)a ^ (unsigned long)b); 43 | } 44 | 45 | void printList(node *head) { 46 | if (head == NULL) 47 | return; 48 | node *prev = NULL; 49 | node* curr = head; 50 | node* temp; 51 | while (curr != NULL) { 52 | temp = curr; 53 | cout<data<<" "; 54 | curr = XOR(curr->npx, prev); 55 | prev = temp; 56 | } 57 | } 58 | 59 | int main() { 60 | node *head = NULL; 61 | head = addnode(head, 1); 62 | head = addnode(head, 2); 63 | head = addnode(head, 3); 64 | head = addnode(head, 4); 65 | head = addnode(head, 5); 66 | 67 | // traverse L->R 68 | cout<<"traverse (L->R): "; 69 | printList(head); 70 | 71 | // traverse R->L 72 | node *prev = NULL; 73 | node *curr = head; 74 | node* temp; 75 | while (curr->npx != XOR(prev, NULL)) { 76 | temp = curr; 77 | curr = XOR(curr->npx, prev); 78 | prev = temp; 79 | } 80 | cout<L): "; 81 | printList(curr); 82 | 83 | cout< 4 | using namespace std; 5 | 6 | void knapSack(int W, int *wt, int *val, int n); 7 | 8 | void knapSack(int W, int *wt, int *val, int n) { 9 | int K[n+1][W+1]; 10 | for (int i = 0; i <= n; i++) { 11 | for (int w = 0; w <= W; w++) { 12 | if (i == 0 || w == 0) 13 | K[i][w] = 0; 14 | else if (wt[i-1] <= w) 15 | K[i][w] = max(K[i-1][w], (K[i-1][w-wt[i-1]] + val[i-1])); 16 | else 17 | K[i][w] = K[i-1][w]; 18 | } 19 | } 20 | cout<<"max val in knapsack: "< 4 | using namespace std; 5 | 6 | int countSolutions(int *S, int m, int n) { 7 | int sol[n+1][m]; 8 | // if value is zero, there is one solution, don't include any coin 9 | for (int i = 0; i < m; i++) 10 | sol[0][i] = 1; 11 | for (int i = 1; i < n+1; i++) { 12 | for (int j = 0; j < m; j++) { 13 | // x is no of solutions including S[j] coin 14 | int x = (i-S[j] >= 0) ? sol[i-S[j]][j] : 0; 15 | // y is no of solutions excluding S[j] coin 16 | int y = (j > 0) ? sol[i][j-1] : 0; 17 | // total no of solutions is x + y for value = i and no of coins = j 18 | sol[i][j] = x + y; 19 | } 20 | } 21 | return sol[n][m-1]; 22 | } 23 | 24 | int main () { 25 | int arr[] = {2, 5, 3, 6}; 26 | int m = sizeof(arr)/sizeof(int); 27 | int n = 10; 28 | 29 | cout<<"max no of coin changes: "< 4 | #include 5 | using namespace std; 6 | 7 | int max(int a, int b); 8 | 9 | int eggDropNaive(int n, int k) { 10 | // if no of floors is 0 or 1 return k 11 | if (k == 0 || k == 1) 12 | return k; 13 | // if no of eggs is 1 and k floors, return k 14 | if (n == 1) 15 | return k; 16 | int min = INT_MAX; 17 | for (int x = 1; x < k; x++) { 18 | int trials = max(eggDropNaive(n-1, x-1), eggDropNaive(n, k-x)); 19 | if (trials < min) 20 | min = trials; 21 | } 22 | return min+1; 23 | } 24 | 25 | int eggDropDP(int n, int k) { 26 | // egg_floor[i][j] stores min no of trials for i eggs and j floors 27 | int egg_floor[n+1][k+1]; 28 | // if floor = 1 or 0, there are 1 and 0 trials resp 29 | for (int i = 1; i <= n; i++) { 30 | egg_floor[i][0] = 0; 31 | egg_floor[i][1] = 1; 32 | } 33 | // if eggs = 1, trials = no of floors 34 | for (int i = 1; i <= k; i++) 35 | egg_floor[1][i] = i; 36 | // in following loops i is for eggs and j for floors 37 | for (int i = 2; i <= n; i++) { 38 | for (int j = 2; j <= k; j++) { 39 | egg_floor[i][j] = INT_MAX; 40 | for (int x = 1; x <= j; x++) { 41 | int trials = 1 + max(egg_floor[i-1][x-1], egg_floor[i][j-x]); 42 | // if new trial value is less then min value in egg_floor, put new value in egg_floor 43 | if (trials < egg_floor[i][j]) 44 | egg_floor[i][j] = trials; 45 | } 46 | } 47 | } 48 | return egg_floor[n][k]; 49 | } 50 | 51 | int max(int a, int b) { 52 | return (a > b) ? a : b; 53 | } 54 | 55 | int main () { 56 | int n = 2; 57 | int k = 10; 58 | cout<<"min no of trials in worst case (naive): "< 4 | using namespace std; 5 | 6 | void lisSum(int *arr, int n); 7 | int maxElement(int *arr, int n); 8 | void printArray(int *arr, int size); 9 | 10 | void lisSum(int *arr, int n) { 11 | if (n == 0) 12 | return; 13 | // lis_sum[i] stores longest increasing subsequence sum till ith position 14 | int lis_sum[n]; 15 | for (int i = 0; i < n; i++) 16 | lis_sum[i] = arr[i]; 17 | for (int i = 1; i < n; i++) { 18 | for (int j = 0; j < i; j++) { 19 | if (arr[i] > arr[j] && lis_sum[i] < lis_sum[j] + arr[i]) 20 | lis_sum[i] = lis_sum[j] + arr[i]; 21 | } 22 | } 23 | // max element in lis_sum array is the maximum sum 24 | cout< max) 31 | max = arr[i]; 32 | return max; 33 | } 34 | 35 | void printArray(int *arr, int size) { 36 | if (size < 1 || arr == NULL) 37 | return; 38 | for (int i = 0; i < size; i++) 39 | cout< 4 | using namespace std; 5 | 6 | int lpsNaive(char* str, int i, int j); 7 | int lpsDP(char* str, int n); 8 | int max(int a, int b); 9 | 10 | int lpsNaive(char* str, int i, int j) { 11 | // single char 12 | if (i == j) 13 | return 1; 14 | // if 2 characters 15 | if (j == i+1) { 16 | // 2 char and same 17 | if (str[i] == str[j]) return 2; 18 | // 2 char and not same 19 | else return 0; 20 | } 21 | // more than 2 chars and first = last 22 | if (str[i] == str[j]) 23 | return 2 + lpsNaive(str, i+1, j-1); 24 | else 25 | return max(lpsNaive(str, i, j-1), lpsNaive(str, i+1, j)); 26 | } 27 | 28 | int lpsDP(char* str, int n) { 29 | // len[i][j] stores length of lps from i to j 30 | int len[n][n]; 31 | // lps of length = 1 is 1 32 | for (int i = 0; i < n; i++) 33 | len[i][i] = 1; 34 | // for length >= 2 35 | for (int L = 2; L <= n; L++) { 36 | for (int i = 0; i < n-L+1; i++) { 37 | int j = i+L-1; 38 | if (L == 2) { 39 | // 2 char and same 40 | if (str[i] == str[j]) 41 | len[i][j] = 2; 42 | // 2 char and not same 43 | else 44 | len[i][j] = 0; 45 | } 46 | if (str[i] == str[j]) 47 | len[i][j] = 2 + len[i+1][j-1]; 48 | else 49 | len[i][j] = max(len[i][j-1], len[i+1][j]); 50 | } 51 | } 52 | return len[0][n-1]; 53 | } 54 | 55 | int max(int a, int b) { 56 | return (a > b) ? a : b; 57 | } 58 | 59 | int main () { 60 | char str[] = "BBABCBCAB"; 61 | int n = sizeof(str)/sizeof(str[0]) - 1; 62 | 63 | cout<<"lps (naive): "< 4 | #define NO_OF_CHARS 256 5 | using namespace std; 6 | 7 | int longestUniqueSubstr(char* str, int n) { 8 | int visited[NO_OF_CHARS]; // array to to keep last occurence (index) of a char 9 | int curr_len; // length of curr NRCS 10 | int max_len; // length of max NRCS so far 11 | int last_index; // stores last occurence of current character 12 | 13 | // initialization 14 | curr_len = 1; 15 | max_len = 1; 16 | for (int i = 0; i < NO_OF_CHARS; i++) 17 | visited[i] = -1; 18 | visited[str[0]] = 0; 19 | 20 | for (int i = 1; i < n; i++) { 21 | last_index = visited[str[i]]; 22 | if (last_index == -1 || i - curr_len > last_index) 23 | curr_len++; 24 | else { 25 | if (curr_len > max_len) 26 | max_len = curr_len; 27 | curr_len = i - last_index; 28 | } 29 | visited[str[i]] = i; 30 | } 31 | if (curr_len > max_len) 32 | max_len = curr_len; 33 | return max_len; 34 | } 35 | 36 | int main () { 37 | char str[] = "ABDEFGABEF"; 38 | int n = sizeof(str)/sizeof(str[0])-1; 39 | cout<<"The input string is "< 4 | #include 5 | using namespace std; 6 | 7 | int matrixChainCount(int *p, int i, int j) { 8 | if (i == j) 9 | return 0; 10 | int min = INT_MAX; 11 | for (int k = i; k < j; k++) { 12 | int count = matrixChainCount(p, i , k) + 13 | matrixChainCount(p, k+1, j) + (p[i-1]*p[k]*p[j]); 14 | if (count < min) 15 | min = count; 16 | } 17 | return min; 18 | } 19 | 20 | int matrixChainCountDP(int *p, int n) { 21 | // cost[i][j] stores min cost of multiplying matrix Ai to Aj 22 | int cost[n][n]; 23 | // cost of multiplying a single matrix (i.e length = 1) is zero 24 | for (int i = 1; i < n; i++) 25 | cost[i][i] = 0; 26 | // for length >= 2 27 | for (int L = 2; L < n; L++) { 28 | for (int i = 1; i < n-L+1; i++) { 29 | int j = i+L-1; 30 | cost[i][j] = INT_MAX; 31 | for (int k = i; k < j; k++) { 32 | int q = cost[i][k] + cost[k+1][j] + p[i-1]*p[k]*p[j]; 33 | if (q < cost[i][j]) 34 | cost[i][j] = q; 35 | } 36 | } 37 | } 38 | // return min cost of multiplying matrix A(1) to A(n-1) 39 | return cost[1][n-1]; 40 | } 41 | 42 | int main () { 43 | int arr[] = {40, 20, 30, 10, 30}; 44 | int n = sizeof(arr)/sizeof(int); 45 | 46 | cout<<"min no of multiplications (naive): "< 4 | using namespace std; 5 | 6 | void maxContiguousSum1(int *arr, int n); // doesnt work for all -ve numbers 7 | void maxContiguousSum2(int *arr, int n); // works for all -ve numbers too 8 | void printArray(int *arr, int n); 9 | 10 | void maxContiguousSum1(int *arr, int n) { 11 | int start = 0, end; 12 | int max_here = 0; 13 | int max_so_far = 0; 14 | for (int i = 0; i < n; i++) { 15 | max_here = max_here + arr[i]; 16 | if (max_here < 0) { 17 | max_here = 0; 18 | start = i+1; 19 | } 20 | else if (max_here > max_so_far) { 21 | max_so_far = max_here; 22 | end = i; 23 | } 24 | } 25 | cout<<"max sum: "< end) 41 | start = end; 42 | cout<<"max sum: "< 4 | #include 5 | #define R 3 6 | #define C 3 7 | using namespace std; 8 | 9 | int minCostPathNaive(int cost[R][C], int m, int n); 10 | int minCostPathDP(int cost[R][C], int m, int n); 11 | int min(int a, int b, int c); 12 | void printMatrix(int *m, int rows, int cols); 13 | 14 | int minCostPathNaive(int cost[R][C], int m, int n) { 15 | if (m < 0 || n < 0) 16 | return INT_MAX; 17 | else if (m == 0 && n == 0) 18 | return cost[m][n]; 19 | else 20 | return cost[m][n] + min( 21 | minCostPathNaive(cost, m-1, n), 22 | minCostPathNaive(cost, m-1, n-1), 23 | minCostPathNaive(cost, m, n-1)); 24 | } 25 | 26 | int minCostPathDP(int cost[R][C], int m, int n) { 27 | // temp[i][j] stores min cost from (0,0) to (i,j) 28 | int temp[m][n]; 29 | temp[0][0] = cost[0][0]; 30 | for (int i = 1; i <= m; i++) 31 | temp[i][0] = temp[i-1][0] + cost[i][0]; 32 | for (int j = 1; j <= n; j++) 33 | temp[0][j] = temp[0][j-1] + cost[0][j]; 34 | 35 | for (int i = 1; i <= m; i++) { 36 | for (int j = 1; j <=n; j++) { 37 | temp[i][j] = cost[i][j] + min(temp[i-1][j], temp[i-1][j-1], temp[i][j-1]); 38 | } 39 | } 40 | return temp[m][n]; 41 | } 42 | 43 | int min(int a, int b, int c) { 44 | int temp = (a < b) ? a : b; 45 | return (temp < c) ? temp : c; 46 | } 47 | 48 | void printMatrix(int *m, int rows, int cols) { 49 | if (m == NULL) 50 | return; 51 | for (int i = 0; i < rows; i++) { 52 | for (int j = 0; j < cols; j++) 53 | cout<<*(m + i*cols + j)<<" "; 54 | cout< 4 | #define INF numeric_limits::max() 5 | using namespace std; 6 | 7 | void optimalBst(int *keys, int* freq, int n); 8 | int freqSum(int *arr, int i, int j); 9 | 10 | void optimalBst(int *keys, int* freq, int n) { 11 | // cost[i][j] stores optimal cost for index from i to j 12 | int cost[n][n]; 13 | // storing cost for single key bst 14 | for (int i = 0; i < n; i++) 15 | cost[i][i] = freq[i]; 16 | // for more than one key i.e i != j 17 | for (int L = 2; L <= n; L++) { 18 | // i is starting index for key length L 19 | for (int i = 0; i < n-L+1; i++) { 20 | // j is ending index for key length L 21 | int j = i+L-1; 22 | if (j < n) 23 | cost[i][j] = INF; 24 | for (int r = i; r <= j; r++) { 25 | // c contains cost from i to j 26 | int c = ( ((r > i) ? cost[i][r-1] : 0) + 27 | ((r < j) ? cost[r+1][j] : 0) + 28 | freqSum(freq, i , j) 29 | ); 30 | if (cost[i][j] > c) 31 | cost[i][j] = c; 32 | } 33 | } 34 | } 35 | cout<<"optimal BST cost: "< 4 | #include 5 | using namespace std; 6 | 7 | int cutRodNaive(int *p, int n); 8 | void cutRodDP(int *p, int n); 9 | void printCutRodSol(int *s, int n, int max_value); 10 | int max(int a, int b); 11 | void printArray(int *arr, int size); 12 | 13 | int cutRodNaive(int *p, int n) { 14 | if (n == 0) 15 | return 0; 16 | int q = INT_MIN; 17 | for (int i = 1; i <= n; i++) 18 | q = max(q, p[i] + cutRodNaive(p, n-i)); 19 | return q; 20 | } 21 | 22 | void cutRodDP(int *p, int n) { 23 | // array r[j] stores max revenue for total length of rod = j 24 | // array s[j] stores first optimal cut length for total length of rod = j 25 | int r[n+1], s[n+1]; 26 | r[0] = 0; 27 | for (int j = 1; j <= n; j++) { 28 | int q = INT_MIN; 29 | for (int i = 1; i <=j ; i++) { 30 | if (q < p[i] + r[j-i]) { 31 | q = p[i] + r[j-i]; 32 | s[j] = i; 33 | } 34 | r[j] = q; 35 | } 36 | } 37 | printCutRodSol(s, n, r[n]); 38 | } 39 | 40 | void printCutRodSol(int *s, int n, int max_value) { 41 | cout< 0) { 44 | // print first optimal cut and update remaining length 45 | cout< b) ? a : b; 52 | } 53 | 54 | void printArray(int *arr, int size) { 55 | if (size < 1 || arr == NULL) 56 | return; 57 | for (int i = 0; i < size; i++) 58 | cout< 4 | #include 5 | using namespace std; 6 | 7 | string bigMultiply(string str, int n); 8 | void printReverseStr(string str); 9 | 10 | string bigFactorial(int n) { 11 | string fact; 12 | fact += ('0' + 1); 13 | for (int i = n; i > 1; i--) 14 | fact = bigMultiply(fact, i); 15 | return fact; 16 | } 17 | 18 | string bigMultiply(string str, int n) { 19 | int i, x, carry = 0; 20 | string result; 21 | for (i = 0; i < str.length(); i++) { 22 | x = ( (str[i]-'0') * n ) + carry; 23 | result += ('0' + x % 10); 24 | carry = x/10; 25 | } 26 | while (carry > 0) { 27 | result += ('0' + carry % 10); 28 | carry = carry/10; 29 | i++; 30 | } 31 | return result; 32 | } 33 | 34 | void printReverseStr(string str) { 35 | for (int i = str.length()-1; i >= 0; i--) 36 | cout< 5 | using namespace std; 6 | 7 | int factorialRecursion(int n) { 8 | if (n <= 1) 9 | return 1; 10 | return (n * factorialRecursion(n-1)); 11 | } 12 | 13 | int factorialIteration(int n) { 14 | if (n <= 1) 15 | return 1; 16 | int fact = 1; 17 | for (int i = n; i > 0; i--) 18 | fact = fact * i; 19 | return fact; 20 | } 21 | 22 | int main() { 23 | cout< 4 | using namespace std; 5 | 6 | void fibonacciRecursion(int max); 7 | void fibonacciRecursionAux(int a, int b, int max); 8 | void fibonacciIteration(int max); 9 | 10 | void fibonacciRecursion(int max) { 11 | int a = 0, b = 1; 12 | cout< max) 19 | return; 20 | cout< 4 | #include 5 | using namespace std; 6 | 7 | void frequencySort(int *arr, int size); 8 | void printArr(int *arr, int size); 9 | 10 | void frequencySort(int *arr, int size) { 11 | int i, count[100] = {0}; 12 | for (i = 0; i < size; i++) 13 | count[arr[i]]++; 14 | 15 | // making a vector of pair to store each distinct element and its count 16 | // pair.first contains count and pair.second contains actual element 17 | vector > temp; 18 | for (i = 0; i < 100; i++) 19 | if (count[i] > 0) 20 | temp.push_back(make_pair(count[i], i)); 21 | 22 | // sorting the vector based on count of element in descending order 23 | sort(temp.begin(), temp.end()); 24 | reverse(temp.begin(), temp.end()); 25 | 26 | // printing output 27 | vector >::iterator itr; 28 | for (itr = temp.begin(); itr < temp.end(); itr++) 29 | for (i = itr->first; i > 0; i--) 30 | cout<second<<" "; 31 | } 32 | 33 | void printArr(int *arr, int size) { 34 | for (int i = 0; i < size; i++) 35 | cout< 6 | #include 7 | using namespace std; 8 | 9 | class Node { 10 | public: 11 | int data; 12 | Node* next; 13 | Node(int data) { 14 | this->data = data; 15 | next = NULL; 16 | } 17 | }; 18 | 19 | class LinkedList { 20 | public: 21 | Node* start; 22 | int size; 23 | LinkedList(Node* start, int size) { 24 | this->start = start; 25 | this->size = size; 26 | } 27 | }; 28 | 29 | Node* createLinkedList(int size); 30 | void getNthFromLast(Node* start, int index); 31 | void getNthFromLastRecursive(Node* start, int index); 32 | void printLL(LinkedList* L); 33 | 34 | Node* createLinkedList(int size) { 35 | if (size < 1) 36 | return NULL; 37 | Node* start = new Node(1); 38 | Node* ptr = start; 39 | Node* ptr1; 40 | for (int i=2; i<=size; i++) { 41 | ptr1 = new Node(i); 42 | ptr->next = ptr1; 43 | ptr = ptr->next; 44 | } 45 | return start; 46 | } 47 | 48 | void getNthFromLast(Node* start, int index) { 49 | if (!start) { 50 | cout<<"List empty.. "; 51 | return; 52 | } 53 | Node* main = start; 54 | Node* ref = start; 55 | for (int i=0; inext; 57 | while (ref) { 58 | ref = ref->next; 59 | main = main->next; 60 | } 61 | cout<data; 62 | return; 63 | } 64 | 65 | void getNthFromLastRecursive(Node* start, int index) { 66 | static int i = 0; 67 | if (!start) 68 | return; 69 | getNthFromLastRecursive(start->next, index); 70 | if (++i == index) { 71 | cout<data; 72 | return; 73 | } 74 | } 75 | 76 | void printLL(LinkedList* L) { 77 | Node* start = L->start; 78 | if (!start) { 79 | cout<<"List empty.."<data<<" "; 84 | start = start->next; 85 | } 86 | return; 87 | } 88 | 89 | int main() { 90 | freopen("input.txt","r",stdin); 91 | 92 | int size, index; 93 | cin>>size>>index; 94 | LinkedList* L = new LinkedList(createLinkedList(size), size); 95 | cout<<"List: "; 96 | printLL(L); 97 | getNthFromLast(L->start, index); 98 | getNthFromLastRecursive(L->start, index); 99 | cout< 7 | #include 8 | using namespace std; 9 | 10 | class Node { 11 | public: 12 | int data; 13 | Node* next; 14 | Node(int data) { 15 | this->data = data; 16 | next = NULL; 17 | } 18 | }; 19 | 20 | class LinkedList { 21 | public: 22 | Node* start; 23 | int size; 24 | LinkedList(Node* start, int size) { 25 | this->start = start; 26 | this->size = size; 27 | } 28 | }; 29 | 30 | Node* createLinkedList(int size); 31 | int getNth(LinkedList* L, int index); 32 | void printLL(LinkedList* L); 33 | 34 | Node* createLinkedList(int size) { 35 | if (size < 1) 36 | return NULL; 37 | Node* start = new Node(1); 38 | Node* ptr = start; 39 | Node* ptr1; 40 | for (int i=2; i<=size; i++) { 41 | ptr1 = new Node(i); 42 | ptr->next = ptr1; 43 | ptr = ptr->next; 44 | } 45 | return start; 46 | } 47 | 48 | int getNth(LinkedList* L, int index) { 49 | if (index > L->size) { 50 | cout<<"Index should be less than size.. "; 51 | return -1; 52 | } 53 | Node* ptr = L->start; 54 | if (!ptr) { 55 | cout<<"List empty.."<next; 60 | return ptr->data; 61 | } 62 | 63 | void printLL(LinkedList* L) { 64 | Node* start = L->start; 65 | if (!start) { 66 | cout<<"List empty.."<data<<" "; 71 | start = start->next; 72 | } 73 | return; 74 | } 75 | 76 | int main() { 77 | freopen("input.txt","r",stdin); 78 | 79 | int size, index; 80 | cin>>size>>index; 81 | LinkedList* L = new LinkedList(createLinkedList(size), size); 82 | cout<<"List: "; 83 | printLL(L); 84 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *left, *right; 10 | Node(int data) { 11 | this->data = data; 12 | left = right = NULL; 13 | } 14 | }; 15 | 16 | Node* treeToList(Node *root); 17 | Node* joinList(Node *listA, Node *listB); // joins two circular linked lists 18 | void joinNode(Node *a, Node *b); // joins a to b 19 | void printCircularList(Node *head); // prints circular linked list 20 | Node* treeInsert(Node* root, int data); // inserts elements in BST 21 | 22 | Node* treeToList(Node *root) { 23 | if (root == NULL) 24 | return root; 25 | Node *leftList = treeToList(root->left); // convert left sub-tree to list 26 | Node *rightList = treeToList(root->right); // convert right sub-tree to list 27 | // make root an independent list of single element 28 | root->left = root; 29 | root->right = root; 30 | // join leftList & root and then the resultant list & rightList 31 | leftList = joinList(leftList, root); 32 | leftList = joinList(leftList, rightList); 33 | return leftList; 34 | } 35 | 36 | Node* joinList(Node *listA, Node *listB) { 37 | if (listA == NULL) 38 | return listB; 39 | if (listB == NULL) 40 | return listA; 41 | 42 | Node *aLast, *bLast; 43 | aLast = listA->left; 44 | bLast = listB->left; 45 | 46 | joinNode(aLast, listB); 47 | joinNode(bLast, listA); 48 | 49 | // still have to figure out why this doesn't work 50 | // joinNode(listA->left, listB); 51 | // joinNode(listB->left, listA); 52 | return listA; 53 | } 54 | 55 | void joinNode(Node *a, Node *b) { 56 | a->right = b; 57 | b->left = a; 58 | } 59 | 60 | void printCircularList(Node *head) { 61 | if (head == NULL) 62 | return; 63 | Node *start = head; 64 | while (true) { 65 | cout<data<<" "; 66 | head = head->right; 67 | if (head == start) 68 | return; 69 | } 70 | } 71 | 72 | void inorder(Node *root) { 73 | if (root == NULL) 74 | return; 75 | inorder(root->left); 76 | cout<data<<" "; 77 | inorder(root->right); 78 | } 79 | 80 | Node* treeInsert(Node* root, int data) { 81 | if (root == NULL) 82 | return new Node(data); 83 | else { 84 | if (data <= root->data) 85 | root->left = treeInsert(root->left, data); 86 | else 87 | root->right = treeInsert(root->right, data); 88 | } 89 | return root; 90 | } 91 | 92 | int main() { 93 | Node *root = NULL; 94 | root = treeInsert(root, 4); 95 | treeInsert(root, 2); 96 | treeInsert(root, 1); 97 | treeInsert(root, 3); 98 | treeInsert(root, 5); 99 | 100 | Node *head = treeToList(root); 101 | printCircularList(head); 102 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class node 8 | { 9 | public: 10 | int data; 11 | node* left; 12 | node* right; 13 | node(int data) { 14 | this->data = data; 15 | left = right = NULL; 16 | } 17 | }; 18 | 19 | node* createBinaryTree(node* root, int depth, int value) { 20 | if (depth < 1) 21 | return NULL; 22 | root = new node(value); 23 | root->left = createBinaryTree(root->left, depth-1, value*2); 24 | root->right = createBinaryTree(root->right, depth-1, value*2+1); 25 | return root; 26 | } 27 | 28 | void inorder(node* root, int left) { 29 | if (!root) 30 | return; 31 | inorder(root->left, 1); 32 | if (left == 1) 33 | cout< data<<" "; 34 | inorder(root->right, 0); 35 | return; 36 | } 37 | 38 | int main() { 39 | freopen("input.txt","r",stdin); 40 | 41 | int depth; 42 | cin>>depth; 43 | node* root; 44 | root = createBinaryTree(root, depth, 1); 45 | cout<<"Inorder traversal of nodes which have a left child and are themselves left child of some node: "< 6 | #include 7 | using namespace std; 8 | 9 | class Node { 10 | public: 11 | int data; 12 | Node* next; 13 | Node(int data) { 14 | this->data = data; 15 | next = NULL; 16 | } 17 | }; 18 | 19 | class LinkedList { 20 | public: 21 | Node* start; 22 | int size; 23 | LinkedList(Node* start, int size) { 24 | this->start = start; 25 | this->size = size; 26 | } 27 | }; 28 | 29 | Node* createLinkedList(int size); 30 | void printMiddle(LinkedList* L); 31 | void printLL(LinkedList* L); 32 | 33 | Node* createLinkedList(int size) { 34 | if (size < 1) 35 | return NULL; 36 | Node* start = new Node(1); 37 | Node* ptr = start; 38 | Node* ptr1; 39 | for (int i=2; i<=size; i++) { 40 | ptr1 = new Node(i); 41 | ptr->next = ptr1; 42 | ptr = ptr->next; 43 | } 44 | return start; 45 | } 46 | 47 | void printMiddle(LinkedList* L) { 48 | if (!L->start) 49 | return; 50 | Node* slow; 51 | Node* fast; 52 | slow = L->start; 53 | fast = L->start->next; 54 | while (fast && fast->next) { 55 | fast = fast->next->next; 56 | slow = slow->next; 57 | } 58 | cout<data; 59 | return; 60 | } 61 | 62 | void printLL(LinkedList* L) { 63 | Node* start = L->start; 64 | if (!start) { 65 | cout<<"List empty.."; 66 | return; 67 | } 68 | while (start) { 69 | cout<data<<" "; 70 | start = start->next; 71 | } 72 | return; 73 | } 74 | 75 | int main() { 76 | freopen("input.txt","r",stdin); 77 | 78 | int size, index; 79 | cin>>size>>index; 80 | LinkedList* L = new LinkedList(createLinkedList(size), size); 81 | cout<<"List: "; 82 | printLL(L); 83 | printMiddle(L); 84 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void addNumList(node *head1, node* head2); 17 | node* reverseList(node* head); 18 | node* addnode(node *head, int data); 19 | void printList(node *head); 20 | 21 | void addNumList(node *head1, node* head2) { 22 | if (head1 == NULL || head2 == NULL) 23 | return; 24 | node* num1 = reverseList(head1); 25 | node* num2 = reverseList(head2); 26 | node* result = NULL; 27 | int sum, rem = 0; 28 | while (num1 != NULL && num2 != NULL) { 29 | sum = num1->data + num2->data + rem; 30 | result = addnode(result, sum % 10); 31 | rem = sum / 10; 32 | num1 = num1->next; 33 | num2 = num2->next; 34 | } 35 | // if num1 finished first 36 | if (num1 == NULL && num2 != NULL) { 37 | while (num2 != NULL) { 38 | sum = num2->data + rem; 39 | result = addnode(result, sum % 10); 40 | rem = sum / 10; 41 | num2 = num2->next; 42 | } 43 | } 44 | // if num2 finished first 45 | if (num2 == NULL && num1 != NULL) { 46 | while (num1 != NULL) { 47 | sum = num1->data + rem; 48 | result = addnode(result, sum % 10); 49 | rem = sum / 10; 50 | num1 = num1->next; 51 | } 52 | } 53 | 54 | // last carry 55 | if (rem > 0) 56 | result = addnode(result, rem); 57 | 58 | // if both finished, reverse result 59 | result = reverseList(result); 60 | printList(result); 61 | } 62 | 63 | node* reverseList(node* head) { 64 | if (head == NULL || head->next == NULL) 65 | return head; 66 | node* new_head = reverseList(head->next); 67 | node* ptr = new_head; 68 | while (ptr->next != NULL) 69 | ptr = ptr->next; 70 | ptr->next = head; 71 | head->next = NULL; 72 | return new_head; 73 | } 74 | 75 | node* addnode(node *head, int data) { 76 | if (head == NULL) 77 | return new node(data); 78 | node *ptr = head; 79 | while (ptr->next != NULL) 80 | ptr = ptr->next; 81 | ptr->next = new node(data); 82 | return head; 83 | } 84 | 85 | void printList(node *head) { 86 | if (head == NULL) 87 | return; 88 | while (head != NULL) { 89 | cout<data<<" "; 90 | head = head->next; 91 | } 92 | } 93 | 94 | int main() { 95 | // num 1 = 925 96 | node *head1 = NULL; 97 | head1 = addnode(head1, 6); 98 | head1 = addnode(head1, 4); 99 | head1 = addnode(head1, 9); 100 | head1 = addnode(head1, 5); 101 | head1 = addnode(head1, 7); 102 | 103 | // num 2 = 456 104 | node *head2 = NULL; 105 | head2 = addnode(head2, 4); 106 | head2 = addnode(head2, 8); 107 | 108 | cout<<"num1: "; 109 | printList(head1); 110 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void alternateSplit(Node *head); 17 | Node* addNode(Node *head, int data); 18 | void printList(Node *head); 19 | 20 | void alternateSplit(Node *head) { 21 | if (head == NULL || head->next == NULL) 22 | return; 23 | Node* list2 = NULL; 24 | Node *prev = head, *curr = head->next; 25 | while (curr != NULL && curr->next != NULL) { 26 | prev->next = curr->next; 27 | prev = prev->next; 28 | list2 = addNode(list2, curr->data); 29 | curr = curr->next->next; 30 | } 31 | if (curr != NULL) 32 | list2 = addNode(list2, curr->data); 33 | if (prev->next != NULL) 34 | prev->next = NULL; 35 | cout<next != NULL) 46 | ptr = ptr->next; 47 | ptr->next = new Node(data); 48 | return head; 49 | } 50 | 51 | void printList(Node *head) { 52 | if (head == NULL) 53 | return; 54 | while (head != NULL) { 55 | cout<data<<" "; 56 | head = head->next; 57 | } 58 | } 59 | 60 | int main() { 61 | Node *head = NULL; 62 | head = addNode(head, 0); 63 | head = addNode(head, 1); 64 | head = addNode(head, 0); 65 | head = addNode(head, 1); 66 | head = addNode(head, 0); 67 | head = addNode(head, 1); 68 | 69 | cout<<"list: "; 70 | printList(head); 71 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void getIntersection(Node *head1, Node *head2); 17 | void printList(Node *head); 18 | 19 | void getIntersection(Node *head1, Node *head2) { 20 | if (head1 == NULL || head2 == NULL) 21 | return; 22 | bool visited[100] = {false}; 23 | // mark nodes in list 1 as visited 24 | while (head1 != NULL) { 25 | visited[head1->data] = true; 26 | head1 = head1->next; 27 | } 28 | // traverse list 2 and return if any node is already visited 29 | while (head2 != NULL) { 30 | if (visited[head2->data]) { 31 | cout<data; 32 | return; 33 | } 34 | head2 = head2->next; 35 | } 36 | cout<<"none"; 37 | } 38 | 39 | void printList(Node *head) { 40 | if (head == NULL) 41 | return; 42 | while (head != NULL) { 43 | cout<data<<" "; 44 | head = head->next; 45 | } 46 | } 47 | 48 | int main() 49 | { 50 | Node *head1 = new Node(3); 51 | head1->next = new Node(6); 52 | head1->next->next = new Node(9); 53 | head1->next->next->next = new Node(15); 54 | head1->next->next->next->next = new Node(30); 55 | 56 | Node *head2 = new Node(10); 57 | head2->next = head1->next->next->next; 58 | head2->next->next = head1->next->next->next->next; 59 | cout<<"list 1: "; 60 | printList(head1); 61 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void deleteMN(node *head, int m, int n); 17 | node* addnode(node *head, int data); 18 | void printList(node *head); 19 | 20 | void deleteMN(node *head, int m, int n) { 21 | if (head == NULL || head->next == NULL) 22 | return; 23 | 24 | node *ptr1 = head, *ptr2 = head; 25 | while (ptr2 != NULL) { 26 | for (int i = 0; ptr2 != NULL && i < m; i++) { 27 | ptr1 = ptr2; 28 | ptr2 = ptr2->next; 29 | } 30 | if (ptr2 == NULL) 31 | return; 32 | for (int i = 0; ptr2 != NULL && i < n; i++) { 33 | ptr2 = ptr2->next; 34 | } 35 | ptr1->next = ptr2; 36 | } 37 | } 38 | 39 | node* addnode(node *head, int data) { 40 | if (head == NULL) 41 | return new node(data); 42 | node *ptr = head; 43 | while (ptr->next != NULL) 44 | ptr = ptr->next; 45 | ptr->next = new node(data); 46 | return head; 47 | } 48 | 49 | void printList(node *head) { 50 | if (head == NULL) 51 | return; 52 | while (head != NULL) { 53 | cout<data<<" "; 54 | head = head->next; 55 | } 56 | } 57 | 58 | int main() { 59 | node *head = NULL; 60 | head = addnode(head, 1); 61 | head = addnode(head, 2); 62 | head = addnode(head, 3); 63 | head = addnode(head, 4); 64 | head = addnode(head, 5); 65 | head = addnode(head, 6); 66 | head = addnode(head, 7); 67 | head = addnode(head, 8); 68 | 69 | cout<<"list: "; 70 | printList(head); 71 | cout< 4 | #define INF numeric_limits::min() 5 | using namespace std; 6 | 7 | class node { 8 | public: 9 | int data; 10 | node *next; 11 | node(int data) { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | void deleteRight(node *head); 18 | node* deleteRightRecursive(node *head); 19 | node* reverseRecursive(node *head); 20 | node* addnode(node *head, int data); 21 | void printList(node *head); 22 | 23 | void deleteRight(node *head) { 24 | if (head == NULL || head->next == NULL) 25 | return; 26 | node *ptr = head, *ptr1; 27 | while (ptr != NULL) { 28 | ptr1 = ptr->next; 29 | while (ptr1 != NULL) { 30 | if (ptr1->data > ptr->data) { 31 | ptr->data = ptr1->data; 32 | ptr->next = ptr1->next; 33 | } 34 | ptr1 = ptr1->next; 35 | } 36 | ptr = ptr->next; 37 | } 38 | } 39 | 40 | node* deleteRightRecursive(node *head) { 41 | if (head == NULL || head->next == NULL) 42 | return head; 43 | // reverse the list 44 | head = reverseRecursive(head); 45 | // keep max and delete all nodes < max 46 | int max = head->data; 47 | node *ptr = head->next; 48 | node *temp = head; 49 | while (ptr != NULL) { 50 | if (ptr->data < max) { 51 | temp->next = ptr->next; 52 | ptr = temp->next; 53 | } 54 | else { 55 | max = ptr->data; 56 | temp = ptr; 57 | ptr = ptr->next; 58 | } 59 | } 60 | // reverse list again 61 | head = reverseRecursive(head); 62 | // return new head 63 | return head; 64 | } 65 | 66 | node* reverseRecursive(node *head) { 67 | if (head == NULL || head->next == NULL) 68 | return head; 69 | node *new_head = reverseRecursive(head->next); 70 | node *ptr = new_head; 71 | while (ptr->next != NULL) 72 | ptr = ptr->next; 73 | ptr->next = head; 74 | head->next = NULL; 75 | return new_head; 76 | } 77 | 78 | node* addnode(node *head, int data) { 79 | if (head == NULL) 80 | return new node(data); 81 | node *ptr = head; 82 | while (ptr->next != NULL) 83 | ptr = ptr->next; 84 | ptr->next = new node(data); 85 | return head; 86 | } 87 | 88 | void printList(node *head) { 89 | if (head == NULL) 90 | return; 91 | while (head != NULL) { 92 | cout<data<<" "; 93 | head = head->next; 94 | } 95 | } 96 | 97 | int main() { 98 | node *head1 = NULL; 99 | head1 = addnode(head1, 12); 100 | head1 = addnode(head1, 15); 101 | head1 = addnode(head1, 10); 102 | head1 = addnode(head1, 11); 103 | head1 = addnode(head1, 5); 104 | head1 = addnode(head1, 6); 105 | head1 = addnode(head1, 2); 106 | head1 = addnode(head1, 3); 107 | 108 | node *head2 = NULL; 109 | head2 = addnode(head2, 10); 110 | head2 = addnode(head2, 20); 111 | head2 = addnode(head2, 30); 112 | head2 = addnode(head2, 40); 113 | head2 = addnode(head2, 50); 114 | head2 = addnode(head2, 60); 115 | 116 | cout<<"list 1: "; 117 | printList(head1); 118 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | bool detectAndRemoveLoop(node *head); 17 | void removeLoop(node* head, node* ptr); 18 | node* addnode(node *head, int data); 19 | void printList(node *head); 20 | 21 | bool detectAndRemoveLoop(node *head) { 22 | if (head == NULL || head->next == NULL) 23 | return false; 24 | 25 | node *slow = head, *fast = head; 26 | while (fast != NULL && fast->next != NULL) { 27 | fast = fast->next->next; 28 | slow = slow->next; 29 | // if slow and fast meet, loop exists. 30 | if (fast == slow) { 31 | removeLoop(head, slow); 32 | return true; 33 | } 34 | } 35 | return false; 36 | } 37 | 38 | void removeLoop(node* head, node* slow) { 39 | // calculating no of nodes (k) in loop 40 | node *ptr = slow; 41 | int k = 1; 42 | while (ptr->next != slow) { 43 | ptr = ptr->next; 44 | k++; 45 | } 46 | // resetting slow to head and ptr to kth position 47 | slow = head; 48 | ptr = head; 49 | for (int i = 0; i < k; i++) 50 | ptr = ptr->next; 51 | // move slow and ptr together till they meet at loop's starting node 52 | // store last node in last and set last->next to NULL 53 | node *last; 54 | while (slow != ptr) { 55 | last = ptr; 56 | slow = slow->next; 57 | ptr = ptr->next; 58 | } 59 | // breaking loop 60 | last->next = NULL; 61 | } 62 | 63 | node* addnode(node *head, int data) { 64 | if (head == NULL) 65 | return new node(data); 66 | node *ptr = head; 67 | while (ptr->next != NULL) 68 | ptr = ptr->next; 69 | ptr->next = new node(data); 70 | return head; 71 | } 72 | 73 | void printList(node *head) { 74 | if (head == NULL) 75 | return; 76 | while (head != NULL) { 77 | cout<data<<" "; 78 | head = head->next; 79 | } 80 | } 81 | 82 | int main() { 83 | node *head = NULL; 84 | head = addnode(head, 1); 85 | head = addnode(head, 2); 86 | head = addnode(head, 3); 87 | head = addnode(head, 4); 88 | head = addnode(head, 5); 89 | head = addnode(head, 6); 90 | head->next->next->next->next->next->next = head->next->next; // linking 6 -> 3 91 | 92 | cout<<"cycle: "; 93 | detectAndRemoveLoop(head) ? cout<<"true" : cout<<"false"; 94 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *left, *right; 10 | Node(int data) { 11 | this->data = data; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | 17 | Node* reverseDoubleList(Node *head); 18 | Node* addNode(Node *head, int data); 19 | void printLeftToRight(Node *head); 20 | void printLeftToRight(Node *head); 21 | 22 | Node* reverseDoubleList(Node *head) { 23 | if (head->right == NULL) 24 | return head; 25 | Node *newhead = reverseDoubleList(head->right); 26 | Node *ptr = head, *temp = head; 27 | while (ptr->right != NULL) 28 | ptr = ptr->right; 29 | newhead->left = NULL; 30 | ptr->right = temp; 31 | temp->left = ptr; 32 | temp->right = NULL; 33 | return newhead; 34 | } 35 | 36 | Node* addNode(Node *head, int data) { 37 | if (head == NULL) 38 | return new Node(data); 39 | Node *ptr = head; 40 | while (ptr->right != NULL) 41 | ptr = ptr->right; 42 | ptr->right = new Node(data); 43 | ptr->right->left = ptr; 44 | return head; 45 | } 46 | 47 | void printLeftToRight(Node *head) { 48 | if (head == NULL) 49 | return; 50 | while (head != NULL) { 51 | cout<data<<" "; 52 | head = head->right; 53 | } 54 | } 55 | 56 | void printRightToLeft(Node *head) { 57 | if (head == NULL) 58 | return; 59 | while (head != NULL) { 60 | cout<data<<" "; 61 | head = head->left; 62 | } 63 | } 64 | 65 | int main() { 66 | Node *head = NULL; 67 | head = addNode(head, 1); 68 | head = addNode(head, 2); 69 | head = addNode(head, 3); 70 | head = addNode(head, 4); 71 | head = addNode(head, 5); 72 | 73 | cout<<"original: "; 74 | printLeftToRight(head); 75 | cout< 5 | using namespace std; 6 | 7 | class node { 8 | public: 9 | int data; 10 | node *next, *down; 11 | node(int data) { 12 | this->data = data; 13 | this->next = NULL; 14 | this->down = NULL; 15 | } 16 | }; 17 | 18 | node* flattenList(node *head); 19 | node* sortedMerge(node *a, node *b); 20 | int countList(node *head); 21 | node* addnodeDown(node *head, int data); 22 | node* addnodeAcross(node *head, int data); 23 | void printListAcross(node *head); 24 | void printListDown(node *head); 25 | 26 | node* flattenList(node *head) { 27 | if (head == NULL) 28 | return head; 29 | 30 | node *down, *across = head; 31 | int n = countList(head); 32 | for (int i = 0; i < n; i++) { 33 | down = across->down; 34 | head = sortedMerge(head, down); 35 | across = across->next; 36 | } 37 | return head; 38 | } 39 | 40 | node* sortedMerge(node *a, node *b) { 41 | if (a == NULL) return b; 42 | else if (b == NULL) return a; 43 | 44 | node *head = NULL; 45 | while (a != NULL && b != NULL) { 46 | if (a->data < b->data) { 47 | head = addnodeAcross(head, a->data); 48 | a = a->next; 49 | } 50 | else { 51 | head = addnodeAcross(head, b->data); 52 | b = b->down; 53 | } 54 | } 55 | if (a == NULL) 56 | while (b != NULL) { 57 | head = addnodeAcross(head, b->data); 58 | b = b->down; 59 | } 60 | if (b == NULL) 61 | while (a != NULL) { 62 | head = addnodeAcross(head, a->data); 63 | a = a->next; 64 | } 65 | return head; 66 | } 67 | 68 | int countList(node *head) { 69 | if (head == NULL) 70 | return 0; 71 | int count = 0; 72 | node *ptr = head; 73 | while (ptr != NULL) { 74 | count++; 75 | ptr = ptr->next; 76 | } 77 | return count; 78 | } 79 | 80 | node* addnodeDown(node *head, int data) { 81 | if (head == NULL) 82 | return new node(data); 83 | node *ptr = head; 84 | while (ptr->down != NULL) 85 | ptr = ptr->down; 86 | ptr->down = new node(data); 87 | return head; 88 | } 89 | 90 | node* addnodeAcross(node *head, int data) { 91 | if (head == NULL) 92 | return new node(data); 93 | node *ptr = head; 94 | while (ptr->next != NULL) 95 | ptr = ptr->next; 96 | ptr->next = new node(data); 97 | return head; 98 | } 99 | 100 | void printListAcross(node *head) { 101 | if (head == NULL) 102 | return; 103 | while (head != NULL) { 104 | cout<data<<" "; 105 | head = head->next; 106 | } 107 | } 108 | 109 | void printListDown(node *head) { 110 | if (head == NULL) 111 | return; 112 | while (head != NULL) { 113 | cout<data<<" "; 114 | head = head->down; 115 | } 116 | } 117 | 118 | int main() { 119 | node *head = NULL; 120 | head = addnodeDown(head, 5); 121 | head = addnodeDown(head, 7); 122 | head = addnodeDown(head, 8); 123 | head = addnodeDown(head, 30); 124 | 125 | head->next = addnodeDown(head->next, 10); 126 | head->next = addnodeDown(head->next, 20); 127 | 128 | head->next->next = addnodeDown(head->next->next, 19); 129 | head->next->next = addnodeDown(head->next->next, 22); 130 | head->next->next = addnodeDown(head->next->next, 50); 131 | 132 | head->next->next->next = addnodeDown(head->next->next->next, 28); 133 | head->next->next->next = addnodeDown(head->next->next->next, 35); 134 | head->next->next->next = addnodeDown(head->next->next->next, 40); 135 | head->next->next->next = addnodeDown(head->next->next->next, 45); 136 | 137 | cout<<"flattened: "; 138 | head = flattenList(head); 139 | printListAcross(head); 140 | 141 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | Node* kReverse(Node *head, int k); 17 | Node* addNode(Node *head, int data); 18 | void printList(Node *head); 19 | 20 | Node* kReverse(Node *head, int k) { 21 | Node *curr = head; 22 | Node *prev = NULL; 23 | Node *next = NULL; 24 | int count = 0; 25 | while (curr != NULL && count < k) { 26 | next = curr->next; 27 | curr->next = prev; 28 | prev = curr; 29 | curr = next; 30 | count++; 31 | } 32 | if (next != NULL) 33 | head->next = kReverse(next, k); 34 | return prev; 35 | } 36 | 37 | Node* addNode(Node *head, int data) { 38 | if (head == NULL) 39 | return new Node(data); 40 | Node *ptr = head; 41 | while (ptr->next != NULL) 42 | ptr = ptr->next; 43 | ptr->next = new Node(data); 44 | return head; 45 | } 46 | 47 | void printList(Node *head) { 48 | if (head == NULL) 49 | return; 50 | while (head != NULL) { 51 | cout<data<<" "; 52 | head = head->next; 53 | } 54 | } 55 | 56 | int main() { 57 | Node *head = NULL; 58 | head = addNode(head, 1); 59 | head = addNode(head, 2); 60 | head = addNode(head, 3); 61 | head = addNode(head, 4); 62 | head = addNode(head, 5); 63 | head = addNode(head, 6); 64 | head = addNode(head, 7); 65 | head = addNode(head, 8); 66 | 67 | cout<<"list: "; 68 | printList(head); 69 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void mergeAlternate(node *p, node **q); 17 | node* addnode(node *head, int data); 18 | void printList(node *head); 19 | 20 | void mergeAlternate(node *p, node **q) { 21 | if (p == NULL || (*q) == NULL) 22 | return; 23 | 24 | node *p_curr = p, *p_next; 25 | node *q_curr = (*q), *q_next; 26 | while (p_curr != NULL && q_curr != NULL) { 27 | p_next = p_curr->next; 28 | q_next = q_curr->next; 29 | 30 | q_curr->next = p_next; 31 | p_curr->next = q_curr; 32 | 33 | p_curr = p_next; 34 | q_curr = q_next; 35 | } 36 | // updating head of q 37 | (*q) = q_curr; 38 | } 39 | 40 | node* addnode(node *head, int data) { 41 | if (head == NULL) 42 | return new node(data); 43 | node *ptr = head; 44 | while (ptr->next != NULL) 45 | ptr = ptr->next; 46 | ptr->next = new node(data); 47 | return head; 48 | } 49 | 50 | void printList(node *head) { 51 | if (head == NULL) 52 | return; 53 | while (head != NULL) { 54 | cout<data<<" "; 55 | head = head->next; 56 | } 57 | } 58 | 59 | int main() { 60 | node *p = NULL; 61 | p = addnode(p, 1); 62 | p = addnode(p, 2); 63 | p = addnode(p, 3); 64 | 65 | node *q = NULL; 66 | q = addnode(q, 4); 67 | q = addnode(q, 5); 68 | q = addnode(q, 6); 69 | q = addnode(q, 7); 70 | q = addnode(q, 8); 71 | 72 | cout<<"list1: "; 73 | printList(p); 74 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | Node* mergeSort(Node *head); 17 | Node* sortedMerge(Node *a, Node *b); 18 | Node* splitListHalf(Node* head); 19 | Node* addNode(Node *head, int data); 20 | void printList(Node *head); 21 | 22 | Node* mergeSort(Node *head) { 23 | if (head == NULL || head->next == NULL) 24 | return head; 25 | 26 | // splitting list in half 27 | Node *a = head, *b; 28 | b = splitListHalf(a); 29 | 30 | // sorting both lists 31 | a = mergeSort(a); 32 | b = mergeSort(b); 33 | 34 | // merging both sorted lists 35 | Node* newhead = sortedMerge(a, b); 36 | return newhead; 37 | } 38 | 39 | Node* sortedMerge(Node *a, Node *b) { 40 | if (a == NULL) return b; 41 | else if (b == NULL) return a; 42 | Node *head = NULL; 43 | while (a != NULL && b != NULL) { 44 | if (a->data < b->data) { 45 | head = addNode(head, a->data); 46 | a = a->next; 47 | } 48 | else { 49 | head = addNode(head, b->data); 50 | b = b->next; 51 | } 52 | } 53 | if (a == NULL) 54 | while (b != NULL) { 55 | head = addNode(head, b->data); 56 | b = b->next; 57 | } 58 | if (b == NULL) 59 | while (a != NULL) { 60 | head = addNode(head, a->data); 61 | a = a->next; 62 | } 63 | return head; 64 | } 65 | 66 | Node* splitListHalf(Node* head) { 67 | if (head == NULL || head->next == NULL) 68 | return head; 69 | Node *slow = head, *fast = head->next; 70 | while (fast != NULL && fast->next != NULL) { 71 | fast = fast->next->next; 72 | slow = slow->next; 73 | } 74 | Node* ptr = slow->next; 75 | slow->next = NULL; 76 | return ptr; 77 | } 78 | 79 | Node* addNode(Node *head, int data) { 80 | if (head == NULL) 81 | return new Node(data); 82 | Node *ptr = head; 83 | while (ptr->next != NULL) 84 | ptr = ptr->next; 85 | ptr->next = new Node(data); 86 | return head; 87 | } 88 | 89 | void printList(Node *head) { 90 | if (head == NULL) 91 | return; 92 | while (head != NULL) { 93 | cout<data<<" "; 94 | head = head->next; 95 | } 96 | } 97 | 98 | int main() { 99 | Node *head = NULL; 100 | head = addNode(head, 1); 101 | head = addNode(head, 3); 102 | head = addNode(head, 0); 103 | head = addNode(head, 11); 104 | head = addNode(head, 2); 105 | head = addNode(head, 6); 106 | head = addNode(head, 7); 107 | 108 | cout<<"list: "; 109 | printList(head); 110 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | node* pairSwap(node *head); 17 | node* addnode(node *head, int data); 18 | void printList(node *head); 19 | 20 | node* pairSwap(node *head) { 21 | if (head == NULL || head->next == NULL) 22 | return head; 23 | // store new head as 2nd node 24 | node *new_head = head->next; 25 | 26 | node *ptr1 = head; 27 | node *ptr2 = head->next; 28 | node *temp = ptr2->next; 29 | 30 | ptr2->next = ptr1; 31 | ptr1->next = pairSwap(temp); 32 | 33 | return new_head; 34 | } 35 | 36 | node* addnode(node *head, int data) { 37 | if (head == NULL) 38 | return new node(data); 39 | node *ptr = head; 40 | while (ptr->next != NULL) 41 | ptr = ptr->next; 42 | ptr->next = new node(data); 43 | return head; 44 | } 45 | 46 | void printList(node *head) { 47 | if (head == NULL) 48 | return; 49 | while (head != NULL) { 50 | cout<data<<" "; 51 | head = head->next; 52 | } 53 | } 54 | 55 | int main() { 56 | node *head = NULL; 57 | head = addnode(head, 1); 58 | head = addnode(head, 2); 59 | head = addnode(head, 3); 60 | head = addnode(head, 4); 61 | head = addnode(head, 5); 62 | head = addnode(head, 6); 63 | head = addnode(head, 7); 64 | 65 | cout<<"list: "; 66 | printList(head); 67 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void printList(Node *head); 17 | 18 | void removeDuplicates(Node *head) { 19 | if (head == NULL || head->next == NULL) 20 | return; 21 | Node *prev, *curr; 22 | prev = head; 23 | curr = head->next; 24 | while (curr != NULL) { 25 | if (prev->data == curr->data) { 26 | prev->next = curr->next; 27 | curr = prev->next; 28 | continue; 29 | } 30 | prev = prev->next; 31 | curr = curr->next; 32 | } 33 | } 34 | 35 | void printList(Node *head) { 36 | if (head == NULL) 37 | return; 38 | while (head != NULL) { 39 | cout<data<<" "; 40 | head = head->next; 41 | } 42 | } 43 | 44 | int main() 45 | { 46 | Node *head = new Node(1); 47 | head->next = new Node(3); 48 | head->next->next = new Node(3); 49 | head->next->next->next = new Node(3); 50 | head->next->next->next->next = new Node(4); 51 | 52 | cout<<"original: "; 53 | printList(head); 54 | removeDuplicates(head); 55 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | node* kAltReverse(node *head, int k); 17 | node* addnode(node *head, int data); 18 | void printList(node *head); 19 | 20 | node* kAltReverse(node *head, int k) { 21 | if (head == NULL || head->next == NULL) 22 | return head; 23 | node *curr = head; 24 | node *prev = NULL; 25 | node *next = NULL; 26 | int count = 0; 27 | // reverse first k nodes 28 | while (curr != NULL && count < k) { 29 | next = curr->next; 30 | curr->next = prev; 31 | prev = curr; 32 | curr = next; 33 | count++; 34 | } 35 | head->next = next; 36 | // skip next k nodes 37 | count = 0; 38 | while (count < k && next != NULL) { 39 | curr = next; 40 | next = next->next; 41 | count++; 42 | } 43 | // recursively call for remaining lists 44 | if (next != NULL) 45 | curr->next = kAltReverse(next, k); 46 | return prev; 47 | } 48 | 49 | node* addnode(node *head, int data) { 50 | if (head == NULL) 51 | return new node(data); 52 | node *ptr = head; 53 | while (ptr->next != NULL) 54 | ptr = ptr->next; 55 | ptr->next = new node(data); 56 | return head; 57 | } 58 | 59 | void printList(node *head) { 60 | if (head == NULL) 61 | return; 62 | while (head != NULL) { 63 | cout<data<<" "; 64 | head = head->next; 65 | } 66 | } 67 | 68 | int main() { 69 | node *head = NULL; 70 | head = addnode(head, 1); 71 | head = addnode(head, 2); 72 | head = addnode(head, 3); 73 | head = addnode(head, 4); 74 | head = addnode(head, 5); 75 | head = addnode(head, 6); 76 | head = addnode(head, 7); 77 | head = addnode(head, 8); 78 | head = addnode(head, 9); 79 | 80 | cout<<"list: "; 81 | printList(head); 82 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | node* rotateList(node *head, int k); 17 | int countList(node *head); 18 | node* addnode(node *head, int data); 19 | void printList(node *head); 20 | 21 | node* rotateList(node *head, int k) { 22 | if (head == NULL || head->next == NULL) 23 | return head; 24 | 25 | node *temp, *end; 26 | int n = countList(head); 27 | // to rotate k times counter clockwise, rotate n-k times clockwise, where n is length of list 28 | for (int i = 0; i < n-k; i++) { 29 | end = head; 30 | while (end->next != NULL) { 31 | temp = end; 32 | end = end->next; 33 | } 34 | end->next = head; 35 | temp->next = NULL; 36 | head = end; 37 | } 38 | return head; 39 | } 40 | 41 | int countList(node *head) { 42 | if (head == NULL) 43 | return 0; 44 | int count = 0; 45 | node *ptr = head; 46 | while (ptr != NULL) { 47 | count++; 48 | ptr = ptr->next; 49 | } 50 | return count; 51 | } 52 | 53 | node* addnode(node *head, int data) { 54 | if (head == NULL) 55 | return new node(data); 56 | node *ptr = head; 57 | while (ptr->next != NULL) 58 | ptr = ptr->next; 59 | ptr->next = new node(data); 60 | return head; 61 | } 62 | 63 | void printList(node *head) { 64 | if (head == NULL) 65 | return; 66 | while (head != NULL) { 67 | cout<data<<" "; 68 | head = head->next; 69 | } 70 | } 71 | 72 | int main() { 73 | node *head = NULL; 74 | head = addnode(head, 10); 75 | head = addnode(head, 20); 76 | head = addnode(head, 30); 77 | head = addnode(head, 40); 78 | head = addnode(head, 50); 79 | head = addnode(head, 60); 80 | 81 | cout<<"list: "; 82 | printList(head); 83 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void separateEvenOdd(node *head); 17 | node* addnode(node *head, int data); 18 | void printList(node *head); 19 | 20 | void separateEvenOdd(node *head) { 21 | if (head == NULL || head->next == NULL) 22 | return; 23 | 24 | // first even element becomes new head 25 | node *ptr = head; 26 | while (ptr->data % 2 != 0) 27 | ptr = ptr->next; 28 | node *new_head = ptr; 29 | 30 | // store end and aux end 31 | ptr = head; 32 | node *aux_end, *end; 33 | while (ptr->next != NULL) 34 | ptr = ptr->next; 35 | aux_end = end = ptr; 36 | ptr = head; 37 | 38 | // move all odd nodes at the end of list 39 | node *temp; 40 | while (ptr != aux_end) { 41 | // if odd, delete node and add it to the end 42 | if (ptr->data % 2 != 0) { 43 | temp->next = ptr->next; 44 | end->next = new node(ptr->data); 45 | end = end->next; 46 | ptr = temp->next; 47 | } 48 | // if even, increment both temp and ptr 49 | else { 50 | temp = ptr; 51 | ptr = ptr->next; 52 | } 53 | } 54 | // print result 55 | printList(new_head); 56 | } 57 | 58 | node* addnode(node *head, int data) { 59 | if (head == NULL) 60 | return new node(data); 61 | node *ptr = head; 62 | while (ptr->next != NULL) 63 | ptr = ptr->next; 64 | ptr->next = new node(data); 65 | return head; 66 | } 67 | 68 | void printList(node *head) { 69 | if (head == NULL) 70 | return; 71 | while (head != NULL) { 72 | cout<data<<" "; 73 | head = head->next; 74 | } 75 | } 76 | 77 | int main() { 78 | node *head = NULL; 79 | head = addnode(head, 17); 80 | head = addnode(head, 15); 81 | head = addnode(head, 8); 82 | head = addnode(head, 12); 83 | head = addnode(head, 10); 84 | head = addnode(head, 5); 85 | head = addnode(head, 4); 86 | head = addnode(head, 1); 87 | head = addnode(head, 7); 88 | head = addnode(head, 6); 89 | 90 | cout<<"list: "; 91 | printList(head); 92 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | node* sortList(node *head); // time complexity O(n), space complexity O(1) 17 | node* addnode(node *head, int data); 18 | void printList(node *head); 19 | 20 | node* sortList(node *head) { 21 | if (head == NULL) 22 | return head; 23 | 24 | int count[3] = {0}; 25 | node *ptr; 26 | // count occurence of each element 27 | ptr = head; 28 | while (ptr != NULL) { 29 | count[ptr->data]++; 30 | ptr = ptr->next; 31 | } 32 | // traverse list and copy each element into list until its count is zero 33 | ptr = head; 34 | int i = 0; 35 | while(ptr != NULL) { 36 | if (count[i] == 0) 37 | i++; 38 | ptr->data = i; 39 | count[i]--; 40 | ptr = ptr->next; 41 | } 42 | return head; 43 | } 44 | 45 | node* addnode(node *head, int data) { 46 | if (head == NULL) 47 | return new node(data); 48 | node *ptr = head; 49 | while (ptr->next != NULL) 50 | ptr = ptr->next; 51 | ptr->next = new node(data); 52 | return head; 53 | } 54 | 55 | void printList(node *head) { 56 | if (head == NULL) 57 | return; 58 | while (head != NULL) { 59 | cout<data<<" "; 60 | head = head->next; 61 | } 62 | } 63 | 64 | int main() { 65 | node *head = NULL; 66 | head = addnode(head, 0); 67 | head = addnode(head, 1); 68 | head = addnode(head, 0); 69 | head = addnode(head, 2); 70 | head = addnode(head, 1); 71 | head = addnode(head, 1); 72 | head = addnode(head, 2); 73 | head = addnode(head, 1); 74 | head = addnode(head, 2); 75 | 76 | cout<<"list: "; 77 | printList(head); 78 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | Node* intersection1(Node *head1, Node *head2); // complexity (m+n) 17 | Node* intersection2(Node *head1, Node *head2); // complexity (mn) 18 | Node* addNode(Node *head, int data); 19 | void printList(Node *head); 20 | 21 | Node* intersection1(Node *head1, Node *head2) { 22 | Node *head = NULL; 23 | Node *ptr = head2; 24 | while (head1 != NULL && head2 != NULL) { 25 | // if nodes of both lists are equal, copy it to new list and advance both lists 26 | if (head1->data == head2->data) { 27 | head = addNode(head, head1->data); 28 | head1 = head2->next; 29 | head2 = head2->next; 30 | } 31 | // else advance only smaller list 32 | else if (head1->data < head2->data) 33 | head1 = head1->next; 34 | else head2 = head2->next; 35 | } 36 | return head; 37 | } 38 | 39 | Node* intersection2(Node *head1, Node *head2) { 40 | Node *head = NULL; 41 | Node *ptr = head2; 42 | // two nested loops.. results in time complexity of (mn) 43 | while (head1 != NULL) { 44 | ptr = head2; 45 | while (ptr != NULL) { 46 | if (ptr->data == head1->data) 47 | head = addNode(head, ptr->data); 48 | ptr = ptr->next; 49 | } 50 | head1 = head1->next; 51 | } 52 | return head; 53 | } 54 | 55 | Node* addNode(Node *head, int data) { 56 | if (head == NULL) 57 | return new Node(data); 58 | Node *ptr = head; 59 | while (ptr->next != NULL) 60 | ptr = ptr->next; 61 | ptr->next = new Node(data); 62 | return head; 63 | } 64 | 65 | void printList(Node *head) { 66 | if (head == NULL) 67 | return; 68 | while (head != NULL) { 69 | cout<data<<" "; 70 | head = head->next; 71 | } 72 | } 73 | 74 | int main() { 75 | Node *head1 = NULL; 76 | head1 = addNode(head1, 1); 77 | head1 = addNode(head1, 2); 78 | head1 = addNode(head1, 3); 79 | head1 = addNode(head1, 4); 80 | head1 = addNode(head1, 6); 81 | 82 | Node *head2 = NULL; 83 | head2 = addNode(head2, 2); 84 | head2 = addNode(head2, 4); 85 | head2 = addNode(head2, 6); 86 | head2 = addNode(head2, 8); 87 | 88 | 89 | cout<<"list 1: "; 90 | printList(head1); 91 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | Node* mergeSortedLists(Node *head1, Node *head2); 17 | Node* addNode(Node *head, int data); 18 | void printList(Node *head); 19 | 20 | Node* mergeSortedLists(Node *head1, Node *head2) { 21 | if (head1 == NULL) return head2; 22 | if (head2 == NULL) return head1; 23 | Node *head = NULL; 24 | 25 | while (head1 != NULL && head2 != NULL) { 26 | if (head1->data < head2->data) { 27 | head = addNode(head, head1->data); 28 | head1 = head1->next; 29 | } 30 | else { 31 | head = addNode(head, head2->data); 32 | head2 = head2->next; 33 | } 34 | } 35 | 36 | if (head1 == NULL) 37 | while (head2 != NULL) { 38 | head = addNode(head, head2->data); 39 | head2 = head2->next; 40 | } 41 | else if (head2 == NULL) 42 | while (head1 != NULL) { 43 | head = addNode(head, head1->data); 44 | head1 = head1->next; 45 | } 46 | 47 | return head; 48 | } 49 | 50 | Node* addNode(Node *head, int data) { 51 | if (head == NULL) 52 | return new Node(data); 53 | Node *ptr = head; 54 | while (ptr->next != NULL) 55 | ptr = ptr->next; 56 | ptr->next = new Node(data); 57 | return head; 58 | } 59 | 60 | void printList(Node *head) { 61 | if (head == NULL) 62 | return; 63 | while (head != NULL) { 64 | cout<data<<" "; 65 | head = head->next; 66 | } 67 | } 68 | 69 | int main() { 70 | Node *head1 = NULL; 71 | head1 = addNode(head1, 1); 72 | head1 = addNode(head1, 3); 73 | head1 = addNode(head1, 6); 74 | head1 = addNode(head1, 8); 75 | head1 = addNode(head1, 9); 76 | 77 | Node *head2 = NULL; 78 | head2 = addNode(head2, 2); 79 | head2 = addNode(head2, 4); 80 | head2 = addNode(head2, 5); 81 | head2 = addNode(head2, 7); 82 | head2 = addNode(head2, 10); 83 | head2 = addNode(head2, 12); 84 | 85 | 86 | cout<<"list 1: "; 87 | printList(head1); 88 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void findTriplet(node *a, node *b, node *c, int num); 17 | node* reverseList(node *head); 18 | node* mergeSort(node *head); 19 | node* sortedMerge(node* a, node *b); 20 | node* splitListInHalf(node* head); 21 | node* addnode(node *head, int data); 22 | void printList(node *head); 23 | 24 | void findTriplet(node *head1, node *head2, node *head3, int num) { 25 | if (head1 == NULL || head2 == NULL || head3 == NULL) 26 | return; 27 | 28 | head2 = mergeSort(head2); // sort head2 in increasing order 29 | head3 = mergeSort(head3); // sort head3 in increasing order 30 | head3 = reverseList(head3); // reverse head3 to make it in decreasing order 31 | node *a = head1, *b = head2, *c = head3; 32 | int sum = 0; 33 | while (a != NULL) { 34 | b = head2; // resetting b 35 | c = head3; // resetting c 36 | while (b != NULL && c != NULL) { 37 | sum = a->data + b->data + c->data; 38 | if (sum == num) { 39 | cout<data<<" "<data<<" "<data; 40 | return; 41 | } 42 | else if (sum < num) 43 | b = b->next; 44 | else c = c->next; 45 | } 46 | a = a->next; 47 | } 48 | cout<next == NULL) 53 | return head; 54 | 55 | node *newhead = reverseList(head->next); 56 | node *ptr = newhead; 57 | while (ptr->next != NULL) 58 | ptr = ptr->next; 59 | ptr->next = head; 60 | head->next = NULL; 61 | return newhead; 62 | } 63 | 64 | node* mergeSort(node *head) { 65 | if (head == NULL || head->next == NULL) 66 | return head; 67 | 68 | node *first = head; 69 | node *second = splitListInHalf(head); 70 | 71 | first = mergeSort(first); 72 | second = mergeSort(second); 73 | 74 | node *result = sortedMerge(first, second); 75 | return result; 76 | } 77 | 78 | node* sortedMerge(node* a, node *b) { 79 | if (a == NULL) return b; 80 | if (b == NULL) return a; 81 | 82 | node *head = NULL; 83 | while (a != NULL && b!= NULL) { 84 | if (a->data < b->data) { 85 | head = addnode(head, a->data); 86 | a = a->next; 87 | } 88 | else { 89 | head = addnode(head, b->data); 90 | b = b->next; 91 | } 92 | } 93 | if (a == NULL && b != NULL) 94 | while (b != NULL) { 95 | head = addnode(head, b->data); 96 | b = b->next; 97 | } 98 | if (b == NULL && a != NULL) 99 | while (a != NULL) { 100 | head = addnode(head, a->data); 101 | a = a->next; 102 | } 103 | return head; 104 | } 105 | 106 | node* splitListInHalf(node* head) { 107 | if (head == NULL || head->next == NULL) 108 | return head; 109 | node *slow = head; 110 | node *fast = head->next; 111 | while (fast != NULL && fast->next != NULL) { 112 | fast = fast->next->next; 113 | slow = slow->next; 114 | } 115 | node *second = slow->next; 116 | slow->next = NULL; 117 | return second; 118 | } 119 | 120 | node* addnode(node *head, int data) { 121 | if (head == NULL) 122 | return new node(data); 123 | node *ptr = head; 124 | while (ptr->next != NULL) 125 | ptr = ptr->next; 126 | ptr->next = new node(data); 127 | return head; 128 | } 129 | 130 | void printList(node *head) { 131 | if (head == NULL) 132 | return; 133 | while (head != NULL) { 134 | cout<data<<" "; 135 | head = head->next; 136 | } 137 | } 138 | 139 | int main() { 140 | node *head1 = NULL; 141 | head1 = addnode(head1, 12); 142 | head1 = addnode(head1, 6); 143 | head1 = addnode(head1, 29); 144 | head1 = addnode(head1, 4); 145 | 146 | node *head2 = NULL; 147 | head2 = addnode(head2, 23); 148 | head2 = addnode(head2, 5); 149 | head2 = addnode(head2, 8); 150 | 151 | node *head3 = NULL; 152 | head3 = addnode(head3, 90); 153 | head3 = addnode(head3, 20); 154 | 155 | cout<<"list1: "; 156 | printList(head1); 157 | cout< 5 | using namespace std; 6 | 7 | class node { 8 | public: 9 | int data; 10 | node *next; 11 | node(int data) { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | node* listUnion(node *head1, node *head2); // complexity (m+n) 18 | node* listIntersection(node *head1, node *head2); // complexity (mn) 19 | bool findInList(node *head, node* ele); 20 | node* addnode(node *head, int data); 21 | void printList(node *head); 22 | 23 | node* listUnion(node *head1, node *head2) { 24 | if (head1 == NULL || head2 == NULL) 25 | return NULL; 26 | node *result = NULL; 27 | node *ptr; 28 | 29 | // scan list 1 and if node is not in result, add it 30 | ptr = head1; 31 | while (ptr != NULL) { 32 | if (!findInList(result, ptr)) 33 | result = addnode(result, ptr->data); 34 | ptr = ptr->next; 35 | } 36 | 37 | // scan list 2 and if node is not in result, add it 38 | ptr = head2; 39 | while (ptr != NULL) { 40 | if (!findInList(result, ptr)) 41 | result = addnode(result, ptr->data); 42 | ptr = ptr->next; 43 | } 44 | return result; 45 | } 46 | 47 | node* listIntersection(node *head1, node *head2) { 48 | if (head1 == NULL || head2 == NULL) 49 | return NULL; 50 | node *result = NULL; 51 | node *ptr1 = head1; 52 | node *ptr2; 53 | 54 | while (ptr1 != NULL) { 55 | ptr2 = head2; 56 | while (ptr2 != NULL) { 57 | // if node is in both lists and not already in result, add it 58 | if (ptr1->data == ptr2->data && !findInList(result, ptr1)) 59 | 60 | result = addnode(result, ptr1->data); 61 | ptr2 = ptr2->next; 62 | } 63 | ptr1 = ptr1->next; 64 | } 65 | return result; 66 | } 67 | 68 | bool findInList(node *head, node* ele) { 69 | if (head == NULL || ele == NULL) 70 | return false; 71 | node *ptr = head; 72 | while (ptr != NULL) { 73 | if (ptr->data == ele->data) 74 | return true; 75 | ptr = ptr->next; 76 | } 77 | return false; 78 | } 79 | 80 | node* addnode(node *head, int data) { 81 | if (head == NULL) 82 | return new node(data); 83 | node *ptr = head; 84 | while (ptr->next != NULL) 85 | ptr = ptr->next; 86 | ptr->next = new node(data); 87 | return head; 88 | } 89 | 90 | void printList(node *head) { 91 | if (head == NULL) 92 | return; 93 | while (head != NULL) { 94 | cout<data<<" "; 95 | head = head->next; 96 | } 97 | } 98 | 99 | int main() { 100 | node *head1 = NULL; 101 | head1 = addnode(head1, 10); 102 | head1 = addnode(head1, 15); 103 | head1 = addnode(head1, 4); 104 | head1 = addnode(head1, 20); 105 | head1 = addnode(head1, 10); 106 | 107 | node *head2 = NULL; 108 | head2 = addnode(head2, 8); 109 | head2 = addnode(head2, 4); 110 | head2 = addnode(head2, 2); 111 | head2 = addnode(head2, 10); 112 | head2 = addnode(head2, 4); 113 | 114 | cout<<"list1: "; 115 | printList(head1); 116 | cout< 5 | using namespace std; 6 | 7 | class node { 8 | public: 9 | int data; 10 | node *next; 11 | node(int data) { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | node* listUnion(node *head1, node *head2); // complexity (m+n) 18 | node* listIntersection(node *head1, node *head2); // complexity (m+n) 19 | node* addnode(node *head, int data); 20 | void printList(node *head); 21 | 22 | node* listUnion(node *head1, node *head2) { 23 | if (head1 == NULL || head2 == NULL) 24 | return NULL; 25 | int arr[1000] = {0}; // array hash table upto 1000 26 | node *result = NULL, *ptr; 27 | 28 | // scan list 1, if node is not in hash table, add it to hash table and result list 29 | ptr = head1; 30 | while (ptr != NULL) { 31 | if (arr[ptr->data] == 0) { 32 | result = addnode(result, ptr->data); 33 | arr[ptr->data] = 1; 34 | } 35 | ptr = ptr->next; 36 | } 37 | 38 | // scan list 2, if node is not in hash table, add it to hash table and result list 39 | ptr = head2; 40 | while (ptr != NULL) { 41 | if (arr[ptr->data] == 0) { 42 | result = addnode(result, ptr->data); 43 | arr[ptr->data] = 1; 44 | } 45 | ptr = ptr->next; 46 | } 47 | return result; 48 | } 49 | 50 | node* listIntersection(node *head1, node *head2) { 51 | if (head1 == NULL || head2 == NULL) 52 | return NULL; 53 | int arr[1000] = {0}; // array hash table upto 1000 54 | node *result = NULL, *ptr; 55 | 56 | // add unique nodes of list 1 into hash table 57 | ptr = head1; 58 | while (ptr != NULL) { 59 | if (arr[ptr->data] == 0) 60 | arr[ptr->data] = 1; 61 | ptr = ptr->next; 62 | } 63 | 64 | // scan list 2, if node is already in hash table, add it to result list 65 | ptr = head2; 66 | while (ptr != NULL) { 67 | if (arr[ptr->data] == 1) { 68 | result = addnode(result, ptr->data); 69 | arr[ptr->data] = 0; 70 | } 71 | ptr = ptr->next; 72 | } 73 | return result; 74 | } 75 | 76 | node* addnode(node *head, int data) { 77 | if (head == NULL) 78 | return new node(data); 79 | node *ptr = head; 80 | while (ptr->next != NULL) 81 | ptr = ptr->next; 82 | ptr->next = new node(data); 83 | return head; 84 | } 85 | 86 | void printList(node *head) { 87 | if (head == NULL) 88 | return; 89 | while (head != NULL) { 90 | cout<data<<" "; 91 | head = head->next; 92 | } 93 | } 94 | 95 | int main() { 96 | node *head1 = NULL; 97 | head1 = addnode(head1, 10); 98 | head1 = addnode(head1, 15); 99 | head1 = addnode(head1, 4); 100 | head1 = addnode(head1, 20); 101 | head1 = addnode(head1, 10); 102 | 103 | node *head2 = NULL; 104 | head2 = addnode(head2, 8); 105 | head2 = addnode(head2, 4); 106 | head2 = addnode(head2, 2); 107 | head2 = addnode(head2, 10); 108 | head2 = addnode(head2, 4); 109 | 110 | cout<<"list1: "; 111 | printList(head1); 112 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void removeDuplicates(Node *head); 17 | Node* addNode(Node *head, int data); 18 | void printList(Node *head); 19 | 20 | void removeDuplicates(Node *head) { 21 | if (head == NULL || head->next == NULL) 22 | return; 23 | int count[1000]; 24 | Node *prev = head, *curr = head; 25 | 26 | while (curr != NULL) { 27 | // if element already in hash table, delete it 28 | if (count[curr->data] == 1) { 29 | if (curr->next != NULL) 30 | prev->next = curr->next; 31 | else 32 | prev->next = NULL; 33 | } 34 | // if element not in hash table, add it 35 | else { 36 | count[curr->data] = 1; 37 | prev = curr; 38 | } 39 | curr = curr->next; 40 | } 41 | } 42 | 43 | Node* addNode(Node *head, int data) { 44 | if (head == NULL) 45 | return new Node(data); 46 | Node *ptr = head; 47 | while (ptr->next != NULL) 48 | ptr = ptr->next; 49 | ptr->next = new Node(data); 50 | return head; 51 | } 52 | 53 | void printList(Node *head) { 54 | if (head == NULL) 55 | return; 56 | while (head != NULL) { 57 | cout<data<<" "; 58 | head = head->next; 59 | } 60 | } 61 | 62 | int main() { 63 | Node *head = NULL; 64 | head = addNode(head, 8); 65 | head = addNode(head, 1); 66 | head = addNode(head, 2); 67 | head = addNode(head, 3); 68 | head = addNode(head, 4); 69 | head = addNode(head, 7); 70 | head = addNode(head, 5); 71 | head = addNode(head, 3); 72 | head = addNode(head, 1); 73 | head = addNode(head, 5); 74 | 75 | cout<<"original: "; 76 | printList(head); 77 | removeDuplicates(head); 78 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | bool isEqual(Node *head1, Node *head2); 17 | Node* addNode(Node *head, int data); 18 | void printList(Node *head); 19 | 20 | bool isEqual(Node *head1, Node *head2) { 21 | while (true) { 22 | if (head1 == NULL && head2 == NULL) 23 | return true; 24 | if (head1 == NULL && head2 != NULL) 25 | return false; 26 | if (head1 != NULL && head2 == NULL) 27 | return false; 28 | if (head1->data != head2->data) 29 | return false; 30 | head1 =head1->next; 31 | head2 =head2->next; 32 | } 33 | } 34 | 35 | Node* addNode(Node *head, int data) { 36 | if (head == NULL) 37 | return new Node(data); 38 | Node *ptr = head; 39 | while (ptr->next != NULL) 40 | ptr = ptr->next; 41 | ptr->next = new Node(data); 42 | return head; 43 | } 44 | 45 | void printList(Node *head) { 46 | if (head == NULL) 47 | return; 48 | while (head != NULL) { 49 | cout<data<<" "; 50 | head = head->next; 51 | } 52 | } 53 | 54 | int main() { 55 | Node *head1 = NULL; 56 | head1 = addNode(head1, 1); 57 | head1 = addNode(head1, 2); 58 | head1 = addNode(head1, 3); 59 | head1 = addNode(head1, 5); 60 | 61 | Node *head2 = NULL; 62 | head2 = addNode(head2, 1); 63 | head2 = addNode(head2, 2); 64 | head2 = addNode(head2, 3); 65 | head2 = addNode(head2, 4); 66 | 67 | cout<<"list 1: "; 68 | printList(head1); 69 | cout< 9 | using namespace std; 10 | 11 | void majorityElement(int* arr, int size) { 12 | int count[100] = {0}, i; 13 | for (i = 0; i < size; i++) 14 | ++count[arr[i]]; 15 | for (i = 0; i < 100; i++) 16 | if (count[i] > size/2) { 17 | cout<>size; 28 | int arr[size]; 29 | for(int i = 0; i < size; i++) 30 | cin>>arr[i]; 31 | majorityElement(arr, size); 32 | cout< 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | class result // for returning low index, high index and sum of max sub-array 12 | { 13 | public: 14 | int lo, hi, sum; 15 | result(int lo, int hi, int sum) { 16 | this->lo = lo; 17 | this->hi = hi; 18 | this->sum = sum; 19 | } 20 | }; 21 | 22 | result* maxSubArray(int* arr, int lo, int hi); 23 | result* maxCrossingSubArray(int* arr, int lo, int mid, int hi); 24 | 25 | result* maxSubArray(int* arr, int lo, int hi) { 26 | if (lo == hi) 27 | return new result(lo, hi, arr[lo]); 28 | int mid = (lo + hi)/2; 29 | result* left = maxSubArray(arr, lo, mid); 30 | result* right = maxSubArray(arr, mid+1, hi); 31 | result* cross = maxCrossingSubArray(arr, lo, mid, hi); 32 | if (left->sum > right->sum && left->sum > cross->sum) 33 | return left; 34 | else if (right->sum > left->sum && right->sum > cross->sum) 35 | return right; 36 | else return cross; 37 | } 38 | 39 | result* maxCrossingSubArray(int* arr, int lo, int mid, int hi) { 40 | int sum, left_sum, left_index, right_sum, right_index; 41 | sum = 0; 42 | left_sum = numeric_limits::min(); 43 | for (int i=mid; i>=lo; i--) { 44 | sum = sum + arr[i]; 45 | if (sum > left_sum) { 46 | left_sum = sum; 47 | left_index = i; 48 | } 49 | } 50 | sum = 0; 51 | right_sum = numeric_limits::min(); 52 | for (int i=mid+1; i<=hi; i++) { 53 | sum = sum + arr[i]; 54 | if (sum > right_sum) { 55 | right_sum = sum; 56 | right_index = i; 57 | } 58 | } 59 | return new result(left_index, right_index, left_sum+right_sum); 60 | } 61 | 62 | int main() { 63 | freopen("input.txt","r",stdin); 64 | 65 | int size; 66 | cin>>size; 67 | int arr[size]; 68 | for(int i=0; i>arr[i]; 70 | result* r = maxSubArray(arr, 0, size-1); 71 | cout<<"Max sub-array.."<lo<hi<sum< 5 | using namespace std; 6 | 7 | class Node { 8 | public: 9 | int data; 10 | Node *next; 11 | Node(int data) { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | Node* pairSwapRecursion(Node *head); 18 | Node* pairSwapIteration(Node *head); 19 | Node* addNode(Node *head, int data); 20 | void printList(Node *head); 21 | 22 | Node* pairSwapRecursion(Node *head) { 23 | if (head == NULL || head->next == NULL) 24 | return head; 25 | 26 | Node *remain = head->next->next; 27 | Node *newhead = head->next; 28 | newhead->next = head; 29 | head->next = pairSwapRecursion(remain); 30 | return newhead; 31 | } 32 | 33 | Node* pairSwapIteration(Node *head) { 34 | Node *prev, *curr, *next; 35 | prev = head; 36 | curr = head->next; 37 | next = head->next->next; 38 | 39 | // change head before beginning 40 | head = head->next; 41 | 42 | while (next != NULL && next->next != NULL) { 43 | curr->next = prev; 44 | prev->next = next->next; 45 | prev = next; 46 | curr = prev->next; 47 | next = next->next->next; 48 | } 49 | curr->next = prev; 50 | prev->next = next; 51 | return head; 52 | } 53 | 54 | Node* addNode(Node *head, int data) { 55 | if (head == NULL) 56 | return new Node(data); 57 | Node *ptr = head; 58 | while (ptr->next != NULL) 59 | ptr = ptr->next; 60 | ptr->next = new Node(data); 61 | return head; 62 | } 63 | 64 | void printList(Node *head) { 65 | if (head == NULL) 66 | return; 67 | while (head != NULL) { 68 | cout<data<<" "; 69 | head = head->next; 70 | } 71 | } 72 | 73 | int main() { 74 | Node *head = NULL; 75 | head = addNode(head, 1); 76 | head = addNode(head, 2); 77 | head = addNode(head, 3); 78 | head = addNode(head, 4); 79 | head = addNode(head, 5); 80 | head = addNode(head, 6); 81 | head = addNode(head, 7); 82 | 83 | cout<<"original: "; 84 | printList(head); 85 | cout< 5 | using namespace std; 6 | 7 | class Node { 8 | public: 9 | int data; 10 | Node *next; 11 | Node(int data) { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | void pairSwapRecursion(Node *head); 18 | void pairSwapIteration(Node *head); 19 | void swap(Node *a, Node *b); 20 | Node* addNode(Node *head, int data); 21 | void printList(Node *head); 22 | 23 | void pairSwapRecursion(Node *head) { 24 | // there must be atleast two nodes in the list 25 | if (head == NULL || head->next == NULL) 26 | return; 27 | swap(head, head->next); 28 | pairSwapRecursion(head->next->next); 29 | } 30 | 31 | void pairSwapIteration(Node *head) { 32 | // swap data of nodes till there are two nodes in the list 33 | while (head != NULL && head->next != NULL) { 34 | swap(head, head->next); 35 | head = head->next->next; 36 | } 37 | } 38 | 39 | void swap(Node *a, Node *b) { 40 | int temp = a->data; 41 | a->data = b->data; 42 | b->data = temp; 43 | } 44 | 45 | Node* addNode(Node *head, int data) { 46 | if (head == NULL) 47 | return new Node(data); 48 | Node *ptr = head; 49 | while (ptr->next != NULL) 50 | ptr = ptr->next; 51 | ptr->next = new Node(data); 52 | return head; 53 | } 54 | 55 | void printList(Node *head) { 56 | if (head == NULL) 57 | return; 58 | while (head != NULL) { 59 | cout<data<<" "; 60 | head = head->next; 61 | } 62 | } 63 | 64 | int main() { 65 | Node *head = NULL; 66 | head = addNode(head, 1); 67 | head = addNode(head, 2); 68 | head = addNode(head, 3); 69 | head = addNode(head, 4); 70 | head = addNode(head, 5); 71 | head = addNode(head, 6); 72 | head = addNode(head, 7); 73 | 74 | cout<<"original: "; 75 | printList(head); 76 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class Node { 8 | public: 9 | char c; 10 | Node *next; 11 | Node(char c) { 12 | this->c = c; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | void checkPalindrome(char* str) { 18 | // creating a linked list of Node objects from passed array 19 | Node *head, *ptr, *ptr1; 20 | head = new Node(str[0]); 21 | ptr = ptr1 = head; 22 | for (int i = 1; str[i] != '\0'; i++) { 23 | ptr1 = new Node(str[i]); 24 | ptr->next = ptr1; 25 | ptr = ptr->next; 26 | } 27 | // pushing each element of linked list onto stack 28 | stack stack; 29 | ptr = head; 30 | while (ptr != NULL) { 31 | stack.push(ptr->c); 32 | ptr = ptr->next; 33 | } 34 | // comparing elements of stack and linked list 35 | ptr = head; 36 | while (!stack.empty() && ptr != NULL) { 37 | if (stack.top() != ptr->c) { 38 | cout<<"string is not a palindrome.."<next; 43 | } 44 | cout<<"string is palindrome.."< 5 | #include 6 | #include 7 | #include 8 | #define INF INT_MAX // definition of infinite 9 | using namespace std; 10 | 11 | class Graph { 12 | private: 13 | vector > > G; // data structure to store graph 14 | int vertex_size; // total no of vertices 15 | 16 | public: 17 | Graph(int vertex_size); 18 | void addEdge(int head, int tail, int weight); 19 | void printGraph(); 20 | void shortestPath(int source); 21 | void topologicalSortUtil(int i, bool* visited, stack* stack); 22 | }; 23 | 24 | Graph::Graph(int vertex_size) { 25 | this->vertex_size = vertex_size; 26 | this->G.resize(vertex_size); 27 | } 28 | 29 | void Graph::addEdge(int head, int tail, int weight) { 30 | G[head].push_back(make_pair(tail, weight)); 31 | } 32 | 33 | void Graph::printGraph() { 34 | cout<<"adj list rep of graph.."< "; 37 | for (int j = 0; j < G[i].size(); j++) 38 | cout< stack; 50 | topologicalSortUtil(source, visited, &stack); 51 | 52 | // initialize distance to all vertices as infinite and distance to source as 0 53 | int* dist = new int[vertex_size]; 54 | for (int i = 0; i < vertex_size; i++) 55 | dist[i] = INF; 56 | dist[source] = 0; 57 | 58 | // scan the adj list of each vertex in the list and calculate shortest path 59 | vector >::iterator v; 60 | while (!stack.empty()) { 61 | int u = stack.top(); 62 | stack.pop(); 63 | if (dist[u] != INF) 64 | for (v = G[u].begin(); v != G[u].end(); v++) 65 | if (dist[v->first] > dist[u] + v->second) 66 | dist[v->first] = dist[u] + v->second; 67 | } 68 | 69 | // print the calculated shortest paths 70 | for (int i = 0; i < vertex_size; i++) 71 | (dist[i] == INF) ? cout<<"INF " : cout<* stack) { 75 | visited[i] = true; 76 | vector >::iterator itr; 77 | for (itr = G[i].begin(); itr != G[i].end(); itr++) 78 | if (!visited[itr->first]) 79 | topologicalSortUtil(itr->first, visited, stack); 80 | stack->push(i); 81 | } 82 | 83 | int main() { 84 | Graph* G = new Graph(6); 85 | // adding edges 86 | G->addEdge(0, 1, 5); 87 | G->addEdge(0, 2, 3); 88 | G->addEdge(1, 3, 6); 89 | G->addEdge(1, 2, 2); 90 | G->addEdge(2, 4, 4); 91 | G->addEdge(2, 5, 2); 92 | G->addEdge(2, 3, 7); 93 | G->addEdge(3, 4, -1); 94 | G->addEdge(4, 5, -2); 95 | G->printGraph(); 96 | 97 | G->shortestPath(1); 98 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class Singleton { 8 | private: 9 | int data; 10 | static Singleton* ptr; 11 | // private constructor 12 | Singleton(int data) { 13 | this->data = data; 14 | } 15 | public: 16 | static Singleton* getInstance(int data); 17 | void printInstance(); 18 | }; 19 | 20 | Singleton* Singleton::ptr = NULL; 21 | Singleton* Singleton::getInstance(int data) { 22 | if (!ptr) 23 | ptr = new Singleton(data); 24 | return ptr; 25 | } 26 | void Singleton::printInstance() { 27 | cout<<"value: "<printInstance(); 37 | s2->printInstance(); 38 | cout< 5 | using namespace std; 6 | 7 | class node { 8 | public: 9 | int data; 10 | node *left, *right; 11 | node(int data) { 12 | this->data = data; 13 | this->left = NULL; 14 | this->right = NULL; 15 | } 16 | }; 17 | 18 | node* sortedListToBst(node *head); 19 | node* sortedListToBstAux(node **head, int n); 20 | int countNodes(node *head); 21 | node* addnode(node *head, int data); 22 | void printLeftToRight(node *head); 23 | void preorder(node *root); 24 | void inorder(node *root); 25 | 26 | node* sortedListToBst(node *head) { 27 | int n = countNodes(head); 28 | return sortedListToBstAux(&head, n); 29 | } 30 | 31 | node* sortedListToBstAux(node **head, int n) { 32 | if (n <= 0) 33 | return NULL; 34 | 35 | node *left = sortedListToBstAux(head, n/2); 36 | node *root = (*head); 37 | root->left = left; 38 | (*head) = (*head)->right; 39 | root->right = sortedListToBstAux(head, n-n/2-1); 40 | 41 | return root; 42 | } 43 | 44 | int countNodes(node *head) { 45 | int count = 0; 46 | node *ptr = head; 47 | while (ptr != NULL) { 48 | count++; 49 | ptr = ptr->right; 50 | } 51 | return count; 52 | } 53 | 54 | node* addnode(node *head, int data) { 55 | if (head == NULL) 56 | return new node(data); 57 | node *ptr = head; 58 | while (ptr->right != NULL) 59 | ptr = ptr->right; 60 | ptr->right = new node(data); 61 | ptr->right->left = ptr; 62 | return head; 63 | } 64 | 65 | void printLeftToRight(node *head) { 66 | if (head == NULL) 67 | return; 68 | while (head != NULL) { 69 | cout<data<<" "; 70 | head = head->right; 71 | } 72 | } 73 | 74 | void preorder(node *root) { 75 | if (root == NULL) 76 | return; 77 | cout<data<<" "; 78 | preorder(root->left); 79 | preorder(root->right); 80 | } 81 | 82 | void inorder(node *root) { 83 | if (root == NULL) 84 | return; 85 | inorder(root->left); 86 | cout<data<<" "; 87 | inorder(root->right); 88 | } 89 | 90 | int main() { 91 | node *head = NULL; 92 | head = addnode(head, 1); 93 | head = addnode(head, 2); 94 | head = addnode(head, 3); 95 | head = addnode(head, 4); 96 | head = addnode(head, 5); 97 | head = addnode(head, 6); 98 | head = addnode(head, 7); 99 | 100 | cout<<"list: "; 101 | printLeftToRight(head); 102 | 103 | cout< 5 | using namespace std; 6 | 7 | class lnode { 8 | public: 9 | int data; 10 | lnode *next; 11 | lnode(int data) { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | class tnode { 18 | public: 19 | int data; 20 | tnode *left, *right; 21 | tnode(int data) { 22 | this->data = data; 23 | this->left = NULL; 24 | this->right = NULL; 25 | } 26 | }; 27 | 28 | tnode* sortedListToBst(lnode *head); 29 | tnode* sortedListToBstAux(lnode **head, int n); 30 | int countNodes(lnode *head); 31 | lnode* addnode(lnode *head, int data); 32 | void printList(lnode *head); 33 | void preorder(tnode *root); 34 | void inorder(tnode *root); 35 | 36 | tnode* sortedListToBst(lnode *head) { 37 | int n = countNodes(head); 38 | return sortedListToBstAux(&head, n); 39 | } 40 | 41 | tnode* sortedListToBstAux(lnode **head, int n) { 42 | if (n <= 0) 43 | return NULL; 44 | 45 | tnode *left = sortedListToBstAux(head, n/2); 46 | tnode *root = new tnode((*head)->data); 47 | root->left = left; 48 | (*head) = (*head)->next; 49 | root->right = sortedListToBstAux(head, n-n/2-1); 50 | 51 | return root; 52 | } 53 | 54 | int countNodes(lnode *head) { 55 | int count = 0; 56 | lnode *ptr = head; 57 | while (ptr != NULL) { 58 | count++; 59 | ptr = ptr->next; 60 | } 61 | return count; 62 | } 63 | 64 | lnode* addnode(lnode *head, int data) { 65 | if (head == NULL) 66 | return new lnode(data); 67 | lnode *ptr = head; 68 | while (ptr->next != NULL) 69 | ptr = ptr->next; 70 | ptr->next = new lnode(data); 71 | return head; 72 | } 73 | 74 | void printList(lnode *head) { 75 | if (head == NULL) 76 | return; 77 | while (head != NULL) { 78 | cout<data<<" "; 79 | head = head->next; 80 | } 81 | } 82 | 83 | void preorder(tnode *root) { 84 | if (root == NULL) 85 | return; 86 | cout<data<<" "; 87 | preorder(root->left); 88 | preorder(root->right); 89 | } 90 | 91 | void inorder(tnode *root) { 92 | if (root == NULL) 93 | return; 94 | inorder(root->left); 95 | cout<data<<" "; 96 | inorder(root->right); 97 | } 98 | 99 | int main() { 100 | lnode *head = NULL; 101 | head = addnode(head, 1); 102 | head = addnode(head, 2); 103 | head = addnode(head, 3); 104 | head = addnode(head, 4); 105 | head = addnode(head, 5); 106 | head = addnode(head, 6); 107 | head = addnode(head, 7); 108 | 109 | cout<<"list: "; 110 | printList(head); 111 | 112 | cout< 4 | using namespace std; 5 | 6 | class Node { 7 | public: 8 | int data; 9 | Node *next; 10 | Node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void splitCircular(Node *head); 17 | void makeCircular(Node *head); 18 | Node* addNode(Node *head, int data); 19 | void printCircularList(Node *head); 20 | 21 | void splitCircular(Node *head) { 22 | if (head == NULL || head->next == NULL) 23 | return; 24 | Node *slow = head; 25 | Node *fast = head; 26 | while (fast->next != head && fast->next->next != head) { 27 | fast = fast->next->next; 28 | slow = slow->next; 29 | } 30 | 31 | // setting head of second list 32 | Node *list2 = slow->next; 33 | if (fast->next->next == head) 34 | fast = fast->next; 35 | fast->next = list2; 36 | // setting head of first list 37 | Node *list1 = head; 38 | slow->next = list1; 39 | 40 | cout<next != NULL) 51 | ptr = ptr->next; 52 | ptr->next = head; 53 | } 54 | 55 | Node* addNode(Node *head, int data) { 56 | if (head == NULL) 57 | return new Node(data); 58 | Node *ptr = head; 59 | while (ptr->next != NULL) 60 | ptr = ptr->next; 61 | ptr->next = new Node(data); 62 | return head; 63 | } 64 | 65 | void printCircularList(Node *head) { 66 | if (head == NULL) 67 | return; 68 | Node *start = head; 69 | while (head->next != start) { 70 | cout<data<<" "; 71 | head = head->next; 72 | } 73 | cout<data; 74 | } 75 | 76 | int main() { 77 | Node *head = NULL; 78 | head = addNode(head, 1); 79 | head = addNode(head, 2); 80 | head = addNode(head, 3); 81 | head = addNode(head, 4); 82 | head = addNode(head, 5); 83 | head = addNode(head, 6); 84 | head = addNode(head, 7); 85 | makeCircular(head); 86 | 87 | cout<<"original: "; 88 | printCircularList(head); 89 | cout< 5 | #include 6 | using namespace std; 7 | 8 | const double ERROR = 0.000000001; 9 | 10 | double findSqrt (double n) { 11 | double low = 0; 12 | double high = n; 13 | while ((high-low) > ERROR) { 14 | double mid = (low+high)/2; 15 | if ((mid * mid) > n) 16 | high = mid; 17 | else low = mid; 18 | } 19 | return low; 20 | } 21 | 22 | int main() { 23 | freopen("input.txt","r",stdin); 24 | 25 | double n = 0, result = 0; 26 | cin>>n; 27 | cout<<"Square root of "< 5 | #include 6 | #define INF INT_MIN 7 | using namespace std; 8 | 9 | class node { 10 | public: 11 | int data; 12 | node *left, *right; 13 | node(int data) { 14 | this->data = data; 15 | this->left = NULL; 16 | this->right = NULL; 17 | } 18 | }; 19 | 20 | class Stack { 21 | public: 22 | node *top; 23 | node *mid; 24 | int count; 25 | 26 | void push(Stack *stack, int data); 27 | int pop(Stack *stack); 28 | int findMiddle(Stack *stack); 29 | void deleteMiddle(Stack *stack); 30 | 31 | Stack() { 32 | this->top = NULL; 33 | this->mid = NULL; 34 | this->count = 0; 35 | } 36 | }; 37 | 38 | void Stack::push(Stack *stack, int data) { 39 | node *new_node = new node(data); 40 | if (stack->count == 0) { 41 | top = new_node; 42 | stack->mid = top; 43 | ++(stack->count); 44 | return; 45 | } 46 | new_node->right = top; 47 | top->left = new_node; 48 | top = new_node; 49 | ++(stack->count); 50 | 51 | // if count goes from odd to even, mid shifts up 52 | if (stack->count % 2 == 0) 53 | stack->mid = stack->mid->left; 54 | } 55 | 56 | int Stack::pop(Stack *stack) { 57 | if (stack->count == 0) { 58 | cout<top->data; 62 | stack->top = stack->top->right; 63 | if (stack->top != NULL) 64 | stack->top->left = NULL; 65 | --(stack->count); 66 | 67 | // if count goes from even to odd, mid shifts down 68 | if (stack->count % 2 != 0) 69 | stack->mid = stack->mid->right; 70 | return temp; 71 | } 72 | 73 | int Stack::findMiddle(Stack *stack) { 74 | if (stack->mid != NULL && stack->count != 0) 75 | return stack->mid->data; 76 | else return INF; 77 | } 78 | 79 | void Stack::deleteMiddle(Stack *stack) { 80 | if (stack->count == 0) { 81 | cout<<"stack empty.."; 82 | return; 83 | } 84 | if (stack->mid == stack->top) { 85 | --(stack->count); 86 | stack->top = NULL; 87 | stack->mid = NULL; 88 | return; 89 | } 90 | stack->mid->left->right = stack->mid->right; 91 | stack->mid->right->left = stack->mid->left; 92 | --(stack->count); 93 | 94 | // if count goes from even to odd, mid shifts down 95 | if (stack->count % 2 != 0) 96 | stack->mid = stack->mid->right; 97 | // if count goes from odd to even, mid shifts up 98 | if (stack->count % 2 == 0) 99 | stack->mid = stack->mid->left; 100 | } 101 | 102 | void printStack(Stack *stack) { 103 | if (stack->count == 0) { 104 | cout<<"stack empty.."; 105 | return; 106 | } 107 | node *ptr = stack->top; 108 | while (ptr != NULL) { 109 | cout<data<<" "; 110 | ptr = ptr->right; 111 | } 112 | } 113 | 114 | int main() { 115 | Stack *stack = new Stack(); 116 | 117 | stack->push(stack, 4); 118 | stack->push(stack, 10); 119 | stack->push(stack, 1); 120 | stack->push(stack, 11); 121 | stack->push(stack, 12); 122 | stack->push(stack, 99); 123 | stack->push(stack, 100); 124 | 125 | cout<<"stack: "; 126 | printStack(stack); 127 | 128 | cout<pop(stack); 130 | stack->pop(stack); 131 | 132 | printStack(stack); 133 | 134 | cout<findMiddle(stack) == INF) ? cout<<"empty" : cout<findMiddle(stack); 136 | cout<deleteMiddle(stack); 138 | printStack(stack); 139 | cout<findMiddle(stack) == INF) ? cout<<"empty" : cout<findMiddle(stack); 141 | 142 | cout< 4 | using namespace std; 5 | 6 | class node { 7 | public: 8 | int data; 9 | node *next; 10 | node(int data) { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | void swapKth(node* head, int k); 17 | int countList(node *head); 18 | node* addnode(node *head, int data); 19 | void printList(node *head); 20 | 21 | void swapKth(node* head, int k) { 22 | if (head == NULL || head->next == NULL) 23 | return; 24 | int n = countList(head); 25 | // k is not appropriate 26 | if (k > n || k < 0) { 27 | cout<next; 44 | } 45 | node *y = head, *p_y, *n_y = NULL; 46 | for (int i = 0; i < n-k; i++) { 47 | p_y = y; 48 | y = y->next; 49 | } 50 | if (y->next != NULL) 51 | n_y = y->next; 52 | 53 | // begin changing pointers 54 | 55 | // if x is next to y 56 | if (x == y->next) { 57 | node *temp = x; 58 | x = y; 59 | y = temp; 60 | 61 | temp = p_x; 62 | p_x = p_y; 63 | p_y = temp; 64 | 65 | n_y = y->next; 66 | } 67 | 68 | p_x->next = y; 69 | if (y == x->next) { 70 | y->next = x; 71 | } 72 | else { 73 | y->next = x->next; 74 | p_y->next = x; 75 | } 76 | if (n_y != NULL) 77 | x->next = n_y; 78 | } 79 | 80 | int countList(node *head) { 81 | if (head == NULL) 82 | return 0; 83 | int count = 0; 84 | node *ptr = head; 85 | while (ptr != NULL) { 86 | count++; 87 | ptr = ptr->next; 88 | } 89 | return count; 90 | } 91 | 92 | node* addnode(node *head, int data) { 93 | if (head == NULL) 94 | return new node(data); 95 | node *ptr = head; 96 | while (ptr->next != NULL) 97 | ptr = ptr->next; 98 | ptr->next = new node(data); 99 | return head; 100 | } 101 | 102 | void printList(node *head) { 103 | if (head == NULL) 104 | return; 105 | while (head != NULL) { 106 | cout<data<<" "; 107 | head = head->next; 108 | } 109 | } 110 | 111 | int main() { 112 | node *head = NULL; 113 | head = addnode(head, 1); 114 | head = addnode(head, 2); 115 | head = addnode(head, 3); 116 | head = addnode(head, 4); 117 | head = addnode(head, 5); 118 | head = addnode(head, 6); 119 | head = addnode(head, 7); 120 | head = addnode(head, 8); 121 | 122 | cout<<"list: "; 123 | printList(head); 124 | cout< 4 | using namespace std; 5 | 6 | int trailingZeros(int n); 7 | int factorialRecursion(int n); 8 | 9 | int trailingZeros(int n) { 10 | int fact = factorialRecursion(n); 11 | int count = 0; 12 | while (fact > 0) { 13 | if ((fact % 10) != 0) 14 | return count; 15 | count++; 16 | fact = fact/10; 17 | } 18 | return count; 19 | } 20 | 21 | int factorialRecursion(int n) { 22 | if (n <= 1) 23 | return 1; 24 | return (n * factorialRecursion(n-1)); 25 | } 26 | 27 | int main() { 28 | cout< 5 | #include 6 | using namespace std; 7 | 8 | class Node { 9 | public: 10 | int data; 11 | Node* left; 12 | Node* right; 13 | 14 | Node(int data) { 15 | this->data = data; 16 | this->left = NULL; 17 | this->right = NULL; 18 | } 19 | }; 20 | 21 | void postOrderSingleStack(Node* root) { 22 | stack stack; 23 | do { 24 | if (root != NULL) { 25 | if (root->right != NULL) 26 | stack.push(root->right); 27 | stack.push(root); 28 | root = root->left; 29 | } 30 | else { 31 | root = stack.top(); 32 | stack.pop(); 33 | if (root->right != NULL && !stack.empty() && root->right == stack.top()) { 34 | Node* temp = stack.top(); 35 | stack.pop(); 36 | stack.push(root); 37 | root = root->right; 38 | } 39 | else { 40 | cout<data<<" "; 41 | root = NULL; 42 | } 43 | } 44 | } while (!stack.empty()); 45 | } 46 | 47 | int main() { 48 | Node* root = new Node(1); 49 | root->left = new Node(2); 50 | root->right = new Node(3); 51 | root->left->left = new Node(4); 52 | root->left->right = new Node(5); 53 | root->left->right->right = new Node(8); 54 | root->right->left = new Node(6); 55 | root->right->right = new Node(7); 56 | root->right->right->left = new Node(9); 57 | 58 | cout<<"Postorder traversal (single stack): "; 59 | postOrderSingleStack(root); 60 | cout< 4 | using namespace std; 5 | 6 | class node 7 | { 8 | public: 9 | int data; 10 | node* left; 11 | node* right; 12 | node(int data) { 13 | this->data = data; 14 | this->left = NULL; 15 | this->right = NULL; 16 | } 17 | }; 18 | 19 | void binaryTreeToBST(node *root); 20 | void arrayToBST(node *root, int *arr); 21 | int countNodes(node *root); 22 | void storeInorder(node *root, int *arr); 23 | void printInorder(node* root); 24 | 25 | void binaryTreeToBST(node *root) { 26 | if (root == NULL) 27 | return; 28 | int n = countNodes(root); 29 | int *arr = new int[n]; 30 | storeInorder(root, arr); 31 | sort(arr, arr+n); 32 | arrayToBST(root, arr); 33 | } 34 | 35 | void arrayToBST(node *root, int *arr) { 36 | if (root == NULL) 37 | return; 38 | static int i = 0; 39 | arrayToBST(root->left, arr); 40 | root->data = arr[i]; 41 | i++; 42 | arrayToBST(root->right, arr); 43 | } 44 | 45 | int countNodes(node *root) { 46 | if (root == NULL) 47 | return 0; 48 | return (1 + 49 | countNodes(root->left) + 50 | countNodes(root->right)); 51 | } 52 | 53 | void storeInorder(node *root, int *arr) { 54 | static int i = 0; 55 | if (root == NULL) 56 | return; 57 | storeInorder(root->left, arr); 58 | arr[i] = root->data; 59 | i++; 60 | storeInorder(root->right, arr); 61 | } 62 | 63 | void printInorder(node* root) { 64 | if (root == NULL) 65 | return; 66 | printInorder(root->left); 67 | cout<data<<" "; 68 | printInorder(root->right); 69 | } 70 | 71 | int main() { 72 | node *root = NULL; 73 | root = new node(10); 74 | root->left = new node(30); 75 | root->right = new node(15); 76 | root->left->left = new node(20); 77 | root->right->right = new node(5); 78 | 79 | cout<<"binary tree (inorder): "; 80 | printInorder(root); 81 | 82 | cout< 4 | #define MIN numeric_limits::min() 5 | #define MAX numeric_limits::max() 6 | using namespace std; 7 | 8 | class node 9 | { 10 | public: 11 | int data; 12 | node* left; 13 | node* right; 14 | node(int data) { 15 | this->data = data; 16 | this->left = NULL; 17 | } 18 | }; 19 | 20 | int isBst(node *root); 21 | int isBstAux(node *root, int min, int max); 22 | void inorder(node *root); 23 | void printArray(int *arr, int n); 24 | 25 | int isBst(node *root) { 26 | return isBstAux(root, MIN, MAX); 27 | } 28 | 29 | int isBstAux(node *root, int min, int max) { 30 | if (root == NULL) 31 | return 1; 32 | 33 | if (root->data < min || root->data > max) 34 | return 0; 35 | 36 | return ( 37 | isBstAux(root->left, min, root->data-1) && 38 | isBstAux(root->right, root->data+1, max) 39 | ); 40 | } 41 | 42 | void inorder(node *root) { 43 | if (root == NULL) 44 | return; 45 | inorder(root->left); 46 | cout<data<<" "; 47 | inorder(root->right); 48 | } 49 | 50 | void printArray(int *arr, int n) { 51 | for (int i = 0 ; i < n ; i++) 52 | cout<left = new node(8); 58 | root->right = new node(22); 59 | root->left->left = new node(4); 60 | root->left->right = new node(12); 61 | root->left->right->left = new node(10); 62 | root->left->right->right = new node(14); 63 | 64 | cout<<"inorder: "; 65 | inorder(root); 66 | 67 | if (isBst(root)) 68 | cout< 4 | using namespace std; 5 | 6 | class node 7 | { 8 | public: 9 | char data; 10 | node* left; 11 | node* right; 12 | node(int data) { 13 | this->data = data; 14 | this->left = NULL; 15 | this->right = NULL; 16 | } 17 | }; 18 | 19 | node* buildTree(char *in, char *pre, int beg, int end); 20 | int searchNode(char *arr, int beg, int end, char x); 21 | void inorder(node* root); 22 | 23 | node* buildTree(char *in, char *pre, int beg, int end) { 24 | if (beg > end) 25 | return NULL; 26 | static int pre_index = beg; 27 | node *root = new node(pre[pre_index++]); 28 | // if root has no children, return it 29 | if (beg == end) 30 | return root; 31 | 32 | int in_index = searchNode(in, beg, end, root->data); 33 | // recursively build left and right subtree 34 | root->left = buildTree(in, pre, beg, in_index-1); 35 | root->right = buildTree(in, pre, in_index+1, end); 36 | 37 | return root; 38 | } 39 | 40 | int searchNode(char *arr, int beg, int end, char x) { 41 | int i; 42 | for (i = beg; i <= end; i++) 43 | if (x == arr[i]) 44 | break; 45 | return i; 46 | } 47 | 48 | void inorder(node* root) { 49 | if (root == NULL) 50 | return; 51 | inorder(root->left); 52 | cout<data<<" "; 53 | inorder(root->right); 54 | } 55 | 56 | int main() { 57 | char in[] = {'D', 'B', 'E', 'A', 'F', 'C'}; 58 | char pre[] = {'A', 'B', 'D', 'E', 'C', 'F'}; 59 | int len = sizeof(in)/sizeof(in[0]); 60 | 61 | node *root = buildTree(in, pre, 0, len-1); 62 | cout<<"inorder: "; 63 | inorder(root); 64 | 65 | cout< 4 | using namespace std; 5 | 6 | class node 7 | { 8 | public: 9 | int data; 10 | node* left; 11 | node* right; 12 | node(int data) { 13 | this->data = data; 14 | this->left = NULL; 15 | } 16 | }; 17 | 18 | bool isSumProperty(node *root); 19 | void inorder(node *root); 20 | 21 | bool isSumProperty(node *root) { 22 | if (root == NULL || (root->left == NULL && root->right == NULL)) 23 | return true; 24 | int left_data = 0; 25 | int right_data = 0; 26 | 27 | if (root->left != NULL) 28 | left_data = root->left->data; 29 | if (root->right != NULL) 30 | right_data = root->right->data; 31 | 32 | return ((root->data == left_data+right_data) && 33 | isSumProperty(root->left)&& 34 | isSumProperty(root->right) 35 | ); 36 | } 37 | 38 | void inorder(node *root) { 39 | if (root == NULL) 40 | return; 41 | inorder(root->left); 42 | cout<data<<" "; 43 | inorder(root->right); 44 | } 45 | 46 | int main() { 47 | node *root = new node(10); 48 | root->left = new node(8); 49 | root->right = new node(2); 50 | root->left->left = new node(3); 51 | root->left->right = new node(5); 52 | root->right->left = new node(2); 53 | 54 | cout<<"inorder: "; 55 | inorder(root); 56 | 57 | cout< 4 | #include 5 | using namespace std; 6 | 7 | class node 8 | { 9 | public: 10 | int data; 11 | node* left; 12 | node* right; 13 | node(int data) { 14 | this->data = data; 15 | this->left = NULL; 16 | } 17 | }; 18 | 19 | void convertTree(node *root); 20 | void incrementSubTree(node *root, int diff); 21 | void levelOrderIteration(node *root); 22 | 23 | void convertTree(node *root) { 24 | if (root == NULL || (root->left == NULL && root->right == NULL)) 25 | return; 26 | 27 | convertTree(root->left); 28 | convertTree(root->right); 29 | 30 | int left_data = 0; 31 | int right_data = 0; 32 | int diff; 33 | 34 | if (root->left != NULL) 35 | left_data = root->left->data; 36 | if (root->right != NULL) 37 | right_data = root->right->data; 38 | 39 | diff = root->data - (left_data + right_data); 40 | if (diff < 0) 41 | root->data = root->data + -diff; 42 | if (diff > 0) 43 | incrementSubTree(root, diff); 44 | } 45 | 46 | void incrementSubTree(node *root, int diff) { 47 | if (root->left != NULL) { 48 | root->left->data = root->left->data + diff; 49 | incrementSubTree(root->left, diff); 50 | } 51 | else if (root->right != NULL) { 52 | root->right->data = root->right->data + diff; 53 | incrementSubTree(root->right, diff); 54 | } 55 | } 56 | 57 | void levelOrderIteration(node *root) { 58 | if (root == NULL) 59 | return; 60 | queue q; 61 | q.push(root); 62 | while (!q.empty()) { 63 | root = q.front(); 64 | q.pop(); 65 | if (root->left != NULL) 66 | q.push(root->left); 67 | if (root->right != NULL) 68 | q.push(root->right); 69 | cout<data<<" "; 70 | } 71 | } 72 | 73 | int main() { 74 | node *root = new node(50); 75 | root->left = new node(7); 76 | root->right = new node(2); 77 | root->left->left = new node(3); 78 | root->left->right = new node(5); 79 | root->right->left = new node(1); 80 | root->right->right = new node(30); 81 | 82 | cout<<"level order: "; 83 | levelOrderIteration(root); 84 | 85 | cout< 4 | using namespace std; 5 | 6 | class node 7 | { 8 | public: 9 | int data; 10 | node* left; 11 | node* right; 12 | node(int data) { 13 | this->data = data; 14 | this->left = NULL; 15 | this->right = NULL; 16 | } 17 | }; 18 | 19 | void doubleTree(node *root); 20 | void printInorder(node* root); 21 | 22 | void doubleTree(node *root) { 23 | if (root == NULL) 24 | return; 25 | doubleTree(root->left); 26 | doubleTree(root->right); 27 | node *new_node = new node(root->data); 28 | new_node->left = root->left; 29 | root->left = new_node; 30 | } 31 | 32 | void printInorder(node* root) { 33 | if (root == NULL) 34 | return; 35 | printInorder(root->left); 36 | cout<data<<" "; 37 | printInorder(root->right); 38 | } 39 | 40 | int main() { 41 | node *root = NULL; 42 | root = new node(2); 43 | root->left = new node(1); 44 | root->right = new node(3); 45 | 46 | cout<<"binary tree (inorder): "; 47 | printInorder(root); 48 | 49 | cout< 4 | using namespace std; 5 | 6 | class node 7 | { 8 | public: 9 | int data; 10 | node* left; 11 | node* right; 12 | node(int data) { 13 | this->data = data; 14 | this->left = NULL; 15 | this->right = NULL; 16 | } 17 | }; 18 | 19 | bool isBalanced(node *root); 20 | int height(node *root); 21 | 22 | bool isBalanced(node *root) { 23 | if (root == NULL) 24 | return true; 25 | int l_height = height(root->left); 26 | int r_height = height(root->right); 27 | 28 | return ( 29 | (abs(l_height - r_height) < 2) && 30 | isBalanced(root->left) && 31 | isBalanced(root->right) 32 | ); 33 | } 34 | 35 | int height(node *root) { 36 | if (root == NULL) 37 | return 0; 38 | int lh = height(root->left); 39 | int rh = height(root->right); 40 | return (1 + max(lh, rh)); 41 | } 42 | 43 | int main() { 44 | node *root1 = new node(1); 45 | root1->left = new node(2); 46 | root1->right = new node(3); 47 | root1->left->left = new node(4); 48 | root1->left->right = new node(5); 49 | root1->left->left->left = new node(8); 50 | 51 | node *root2 = new node(1); 52 | root2->left = new node(2); 53 | root2->right = new node(3); 54 | root2->left->left = new node(4); 55 | root2->left->right = new node(5); 56 | 57 | cout< 4 | using namespace std; 5 | 6 | class node 7 | { 8 | public: 9 | int data; 10 | node* left; 11 | node* right; 12 | node(int data) { 13 | this->data = data; 14 | this->left = NULL; 15 | } 16 | }; 17 | 18 | bool isNodeInBST(node *root, int x); 19 | void inorder(node *root); 20 | void printArray(int *arr, int n); 21 | 22 | node* lca(node *root, int a, int b) { 23 | if (root == NULL || !isNodeInBST(root, a) || !isNodeInBST(root, b)) 24 | return NULL; 25 | 26 | if (root->data > a && root->data > b) 27 | return lca(root->left, a, b); 28 | if (root->data < a && root->data < b) 29 | return lca(root->right, a, b); 30 | return root; 31 | } 32 | 33 | bool isNodeInBST(node *root, int x) { 34 | if (root == NULL) 35 | return false; 36 | while (root != NULL) { 37 | if (x == root->data) 38 | return true; 39 | if (x < root->data) 40 | root = root->left; 41 | else root = root->right; 42 | } 43 | return false; 44 | } 45 | 46 | void inorder(node *root) { 47 | if (root == NULL) 48 | return; 49 | inorder(root->left); 50 | cout<data<<" "; 51 | inorder(root->right); 52 | } 53 | 54 | void printArray(int *arr, int n) { 55 | for (int i = 0 ; i < n ; i++) 56 | cout<left = new node(8); 62 | root->right = new node(22); 63 | root->left->left = new node(4); 64 | root->left->right = new node(12); 65 | root->left->right->left = new node(10); 66 | root->left->right->right = new node(14); 67 | 68 | cout<<"inorder: "; 69 | inorder(root); 70 | 71 | cout<data; 73 | cout<data; 74 | cout<data; 75 | cout<data : cout<<"NULL"; 78 | 79 | cout< 4 | using namespace std; 5 | 6 | class node 7 | { 8 | public: 9 | int data; 10 | node* left; 11 | node* right; 12 | node(int data) { 13 | this->data = data; 14 | this->left = NULL; 15 | } 16 | }; 17 | 18 | void inorder(node *root); 19 | void printArray(int *arr, int n); 20 | 21 | int countLeaf(node *root) { 22 | if (root == NULL) 23 | return 0; 24 | if (root->left == NULL && root->right == NULL) 25 | return 1; 26 | int left_count = countLeaf(root->left); 27 | int right_count = countLeaf(root->right); 28 | return left_count+right_count; 29 | } 30 | 31 | void inorder(node *root) { 32 | if (root == NULL) 33 | return; 34 | inorder(root->left); 35 | cout<data<<" "; 36 | inorder(root->right); 37 | } 38 | 39 | void printArray(int *arr, int n) { 40 | for (int i = 0 ; i < n ; i++) 41 | cout<left = new node(2); 47 | root->right = new node(3); 48 | root->left->left = new node(4); 49 | root->left->right = new node(5); 50 | 51 | cout<<"inorder: "; 52 | inorder(root); 53 | 54 | cout< 4 | using namespace std; 5 | 6 | class node 7 | { 8 | public: 9 | int data; 10 | node* left; 11 | node* right; 12 | node(int data) { 13 | this->data = data; 14 | this->left = NULL; 15 | } 16 | }; 17 | 18 | void levelOrderSpiral(node *root); 19 | void printGivenLevel(node *root, int level, int ltr); 20 | int findHeight(node *root); 21 | void inorder(node *root); 22 | void printArray(int *arr, int n); 23 | 24 | void levelOrderSpiral(node *root) { 25 | if (root == NULL) 26 | return; 27 | int ltr = -1; 28 | int height = findHeight(root); 29 | for (int i = 1; i <= height; i++) { 30 | printGivenLevel(root, i, ltr); 31 | ltr = -ltr; 32 | } 33 | } 34 | 35 | void printGivenLevel(node *root, int level, int ltr) { 36 | if (root == NULL) 37 | return; 38 | if (level == 1) { 39 | cout<data<<" "; 40 | return; 41 | } 42 | else if (level > 1) { 43 | if (ltr == 1) { 44 | printGivenLevel(root->left, level-1, ltr); 45 | printGivenLevel(root->right, level-1, ltr); 46 | } 47 | else { 48 | printGivenLevel(root->right, level-1, ltr); 49 | printGivenLevel(root->left, level-1, ltr); 50 | } 51 | } 52 | } 53 | 54 | int findHeight(node *root) { 55 | if (root == NULL) 56 | return 0; 57 | int lh = findHeight(root->left); 58 | int rh = findHeight(root->right); 59 | if (lh > rh) 60 | return lh+1; 61 | else return rh+1; 62 | } 63 | 64 | void inorder(node *root) { 65 | if (root == NULL) 66 | return; 67 | inorder(root->left); 68 | cout<data<<" "; 69 | inorder(root->right); 70 | } 71 | 72 | void printArray(int *arr, int n) { 73 | for (int i = 0 ; i < n ; i++) 74 | cout<left = new node(2); 80 | root->right = new node(3); 81 | root->left->left = new node(4); 82 | root->left->right = new node(5); 83 | root->right->left = new node(6); 84 | root->right->right = new node(7); 85 | 86 | cout<<"inorder: "; 87 | inorder(root); 88 | 89 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int data; 8 | node* left; 9 | node* right; 10 | node(int data) { 11 | this->data = data; 12 | this->left = NULL; 13 | this->right = NULL; 14 | } 15 | }; 16 | 17 | int getMaxWidth(node *root); 18 | void inorder(node *root); 19 | 20 | int getMaxWidth(node *root) { 21 | if (root == NULL) 22 | return 0; 23 | int max = 1; 24 | int count = 0; 25 | queue Q; 26 | Q.push(root); 27 | Q.push(NULL); 28 | while (true) { 29 | count = 0; 30 | while (Q.front() != NULL) { 31 | node *temp = Q.front(); 32 | Q.pop(); 33 | count++; 34 | if (temp->left != NULL) 35 | Q.push(temp->left); 36 | if (temp->right != NULL) 37 | Q.push(temp->right); 38 | } 39 | Q.pop(); 40 | if (Q.empty()) 41 | break; 42 | if (count > max) 43 | max = count; 44 | Q.push(NULL); 45 | } 46 | if (count > max) 47 | max = count; 48 | return max; 49 | } 50 | 51 | void inorder(node *root) { 52 | if (root == NULL) 53 | return; 54 | inorder(root->left); 55 | cout<data<<" "; 56 | inorder(root->right); 57 | } 58 | 59 | int main() { 60 | node *root = new node(1); 61 | root->left = new node(2); 62 | root->right = new node(3); 63 | root->left->left = new node(4); 64 | root->left->right = new node(5); 65 | root->left->right->left = new node(10); 66 | root->left->right->right = new node(11); 67 | root->right->right = new node(8); 68 | root->right->right->left = new node(6); 69 | root->right->right->right = new node(7); 70 | 71 | cout<<"inorder of tree.."< 4 | #include 5 | using namespace std; 6 | 7 | class node 8 | { 9 | public: 10 | int data; 11 | node* left; 12 | node* right; 13 | node(int data) { 14 | this->data = data; 15 | this->left = NULL; 16 | } 17 | }; 18 | 19 | void mirrorTree(node* root); 20 | void swapTree(node* root); 21 | void levelOrderIteration(node *root); 22 | 23 | void mirrorTree(node* root) { 24 | if (root == NULL) 25 | return; 26 | mirrorTree(root->left); 27 | mirrorTree(root->right); 28 | swapTree(root); 29 | } 30 | 31 | void swapTree(node* root) { 32 | node* temp = root->left; 33 | root->left = root->right; 34 | root->right = temp; 35 | } 36 | 37 | void levelOrderIteration(node *root) { 38 | if (root == NULL) 39 | return; 40 | queue q; 41 | q.push(root); 42 | while (!q.empty()) { 43 | root = q.front(); 44 | q.pop(); 45 | if (root->left != NULL) 46 | q.push(root->left); 47 | if (root->right != NULL) 48 | q.push(root->right); 49 | cout<data<<" "; 50 | } 51 | } 52 | 53 | int main() { 54 | node *root = new node(1); 55 | root->left = new node(2); 56 | root->right = new node(3); 57 | root->left->left = new node(4); 58 | root->left->right = new node(5); 59 | 60 | cout<<"level order: "; 61 | levelOrderIteration(root); 62 | 63 | mirrorTree(root); 64 | cout< 4 | using namespace std; 5 | 6 | class node 7 | { 8 | public: 9 | int data; 10 | node* left; 11 | node* right; 12 | node(int data) { 13 | this->data = data; 14 | this->left = NULL; 15 | this->right = NULL; 16 | } 17 | }; 18 | 19 | void rootToLeafPath(node *root, int x); 20 | void rootToLeafPathAux(node *root, int *path, int pathlen, int x); 21 | int arraySum(int *arr, int n); 22 | 23 | void rootToLeafPath(node *root, int x) { 24 | if (root == NULL) 25 | return; 26 | int path[1000]; 27 | rootToLeafPathAux(root, path, 0, x); 28 | } 29 | 30 | void rootToLeafPathAux(node *root, int *path, int pathlen, int x) { 31 | if (root != NULL) { 32 | path[pathlen] = root->data; 33 | pathlen++; 34 | } 35 | if (root->left == NULL && root->right == NULL) { 36 | if (x == arraySum(path, pathlen)) 37 | cout<<"path equal to "<left != NULL) 42 | rootToLeafPathAux(root->left, path, pathlen, x); 43 | if (root->right != NULL) 44 | rootToLeafPathAux(root->right, path, pathlen, x); 45 | } 46 | } 47 | 48 | int arraySum(int *arr, int n) { 49 | int sum = 0; 50 | for (int i = 0; i < n; i++) 51 | sum = sum + arr[i]; 52 | return sum; 53 | } 54 | 55 | int main() { 56 | node *root = new node(1); 57 | root->left = new node(2); 58 | root->right = new node(3); 59 | root->left->left = new node(4); 60 | root->left->right = new node(5); 61 | root->left->left->left = new node(6); 62 | 63 | cout<<"root to leaf path sum.."< 4 | using namespace std; 5 | 6 | class node 7 | { 8 | public: 9 | int data; 10 | node* left; 11 | node* right; 12 | node(int data) { 13 | this->data = data; 14 | this->left = NULL; 15 | } 16 | }; 17 | 18 | void rootToLeafPath(node *root); 19 | void rootToLeafPathAux(node *root, int *path, int pathlen); 20 | void printArray(int *arr, int n); 21 | 22 | void rootToLeafPath(node *root) { 23 | if (root == NULL) 24 | return; 25 | int path[1000]; 26 | rootToLeafPathAux(root, path, 0); 27 | } 28 | 29 | void rootToLeafPathAux(node *root, int *path, int pathlen) { 30 | if (root == NULL) 31 | return; 32 | 33 | path[pathlen] = root->data; 34 | pathlen++; 35 | 36 | if (root->left == NULL && root->right == NULL) { 37 | printArray(path, pathlen); 38 | cout<left, path, pathlen); 42 | rootToLeafPathAux(root->right, path, pathlen); 43 | } 44 | } 45 | 46 | void printArray(int *arr, int n) { 47 | for (int i = 0 ; i < n ; i++) 48 | cout<left = new node(2); 54 | root->right = new node(3); 55 | root->left->left = new node(4); 56 | root->left->right = new node(5); 57 | root->right->left = new node(6); 58 | root->right->left->right = new node(7); 59 | 60 | rootToLeafPath(root); 61 | 62 | cout<