├── .DS_Store ├── Lecture 1 ├── run ├── sum_of_n.cpp ├── prime.cpp └── basics.cpp ├── Lecture 10 ├── run └── recursion_3rd.cpp ├── Lecture 11 ├── run └── recursion_backtracking.cpp ├── Lecture 14 ├── run ├── oop2.cpp └── oop.cpp ├── Lecture 15 ├── run └── LinkesList.cpp ├── Lecture 2 ├── run ├── pattern_1.cpp ├── pattern_2.cpp ├── pattern_3.cpp ├── factorial.cpp ├── pattern_4.cpp ├── basics.cpp ├── zero_one_pattern.cpp ├── pattern_5.cpp ├── datatypes.cpp └── pointers.cpp ├── Lecture 21 ├── run ├── HashMapStl.cpp ├── SubarraywithSum0.cpp ├── LongestSubarrayWithSum0.cpp ├── LongestSubarrayWithSumK.cpp └── hashtable.cpp ├── Lecture 24 ├── run ├── mergeKsortedArrays.cpp ├── priorityQueue.cpp ├── heap.cpp ├── questions.cpp └── heap_sanket.cpp ├── Lecture 25 ├── run ├── trie.cpp └── maxXorPair.cpp ├── Lecture 26 ├── run ├── greedy.cpp ├── dijkstra.cpp └── graph.cpp ├── Lecture 3 ├── run ├── square_root.cpp ├── fibonacci_function.cpp ├── diamond.cpp ├── test_functions.cpp ├── functions_explained.cpp ├── call_by.cpp └── functions.cpp ├── Lecture 6 ├── rujn ├── run ├── kadaneAlgo.cpp ├── array_revision.cpp ├── insertion_sort.cpp ├── char_array.cpp ├── substrings_ques.cpp └── string_questions.cpp ├── Lecture 7 ├── run ├── bitmasking_op.cpp └── unique_numbers.cpp ├── Lecture 8 ├── run └── recursion_basics.cpp ├── Lecture 9 ├── run └── recursion_2nd.cpp ├── Doubts ├── Second │ ├── run │ └── doubts.cpp └── doubts.cpp ├── Lecture 16-17 ├── run ├── StackSTL.cpp ├── StackImplementation.cpp ├── StackGeneric.cpp ├── Queue.cpp └── StackQuestions.cpp ├── Lecture 22-23 ├── run └── dp_basics.cpp ├── Lecture 5 ├── a.out ├── strings.cpp ├── rotateArray.cpp ├── dynamic_memory.cpp ├── charArray.cpp ├── reverseWords.cpp ├── 2darray.cpp └── brackets.cpp ├── Lecture 12 - 13 ├── run ├── timeComplexity.cpp └── divide_and_conquer.cpp ├── Lecture 18-19-20 ├── run ├── binaryTree.cpp └── bst.cpp ├── LP01 - Basics of Problem Solving.pptx ├── Lecture 4 ├── iput_and_output and array.cpp ├── reverse an array.cpp ├── bubble sort 2.cpp ├── subarray printing.cpp ├── selection sort.cpp ├── insertion sort.cpp ├── bubble_sort.cpp ├── print array in reverse order.cpp └── kadanes algo.cpp └── doubts2.cpp /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/.DS_Store -------------------------------------------------------------------------------- /Lecture 1/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 1/run -------------------------------------------------------------------------------- /Lecture 10/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 10/run -------------------------------------------------------------------------------- /Lecture 11/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 11/run -------------------------------------------------------------------------------- /Lecture 14/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 14/run -------------------------------------------------------------------------------- /Lecture 15/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 15/run -------------------------------------------------------------------------------- /Lecture 2/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 2/run -------------------------------------------------------------------------------- /Lecture 21/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 21/run -------------------------------------------------------------------------------- /Lecture 24/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 24/run -------------------------------------------------------------------------------- /Lecture 25/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 25/run -------------------------------------------------------------------------------- /Lecture 26/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 26/run -------------------------------------------------------------------------------- /Lecture 3/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 3/run -------------------------------------------------------------------------------- /Lecture 6/rujn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 6/rujn -------------------------------------------------------------------------------- /Lecture 6/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 6/run -------------------------------------------------------------------------------- /Lecture 7/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 7/run -------------------------------------------------------------------------------- /Lecture 8/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 8/run -------------------------------------------------------------------------------- /Lecture 9/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 9/run -------------------------------------------------------------------------------- /Doubts/Second/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Doubts/Second/run -------------------------------------------------------------------------------- /Lecture 16-17/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 16-17/run -------------------------------------------------------------------------------- /Lecture 22-23/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 22-23/run -------------------------------------------------------------------------------- /Lecture 5/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 5/a.out -------------------------------------------------------------------------------- /Lecture 12 - 13/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 12 - 13/run -------------------------------------------------------------------------------- /Lecture 18-19-20/run: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/Lecture 18-19-20/run -------------------------------------------------------------------------------- /LP01 - Basics of Problem Solving.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khandelwalpranav05/Launchpad-18-August/HEAD/LP01 - Basics of Problem Solving.pptx -------------------------------------------------------------------------------- /Lecture 1/sum_of_n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n = 5; 8 | int sum = 0; 9 | 10 | for(int i=1;i<=n;i++){ 11 | sum = sum + i; 12 | } 13 | 14 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n=5; 8 | 9 | for(int row = 1; row<=n; row++){ 10 | 11 | for(int col=1;col<=row;col++){ 12 | cout<<"*"; 13 | } 14 | 15 | cout< 2 | 3 | using namespace std; 4 | 5 | int sqrt(int n){ 6 | int ans = 1; 7 | 8 | while(ans*ans<=n){ 9 | ans = ans + 1; 10 | } 11 | 12 | return ans-1; 13 | } 14 | 15 | int main(){ 16 | 17 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n; 8 | cin>>n; 9 | 10 | for(int row = 1; row<=n; row++){ 11 | 12 | for(int col=1;col<=row;col++){ 13 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n; 8 | cin>>n; 9 | 10 | for(int row = 1; row<=n; row++){ 11 | 12 | for(int col=1;col<=row;col++){ 13 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n; 8 | cout<<"Enter the Number"<>n; 10 | 11 | int mul = 1; 12 | 13 | for(int i=1;i<=n;i++){ 14 | mul = mul*i; 15 | } 16 | 17 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int i, n; 8 | cin >> n; 9 | int a[n]; 10 | for (i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | for (i = 0; i < n; i++) { 14 | cout << a[i]; 15 | } 16 | } -------------------------------------------------------------------------------- /Lecture 2/pattern_4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n; 8 | cin>>n; 9 | 10 | int val = 1; 11 | 12 | for(int row = 1; row<=n; row++){ 13 | 14 | for(int col=1;col<=row;col++){ 15 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int a,b,c,d; 8 | 9 | // cin>>a; 10 | // cin>>b; 11 | // cin>>c; 12 | // cin>>d; 13 | 14 | cin>>a>>b>>c>>d; 15 | 16 | float average = (a+b+c+d)/4.0; 17 | 18 | cout<<"Average is "< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int i, n; 8 | cin >> n; 9 | int a[n]; 10 | for (i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | for (i = 0; i < n / 2 ; i++) { 14 | a[i] = a[n - i - 1]; 15 | } 16 | for (i = 0; i < n; i++) { 17 | for (i = 0; i < n; i++) { 18 | cout << a[i] << " "; 19 | } 20 | } -------------------------------------------------------------------------------- /Lecture 16-17/StackSTL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | stack s; 9 | 10 | s.push(4); 11 | s.push(3); 12 | s.push(2); 13 | s.push(1); 14 | s.push(0); 15 | 16 | s.pop(); 17 | s.pop(); 18 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(int argc, char const *argv[]) 6 | { 7 | unordered_map mp; 8 | mp[10] = 11; 9 | mp[12] = 12; 10 | if(mp.find(1) != mp.end()) { 11 | // this exist]c 12 | cout<<"found"; 13 | } 14 | if(mp.find(10) == mp.end()) { 15 | cout<<"not found"; 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Lecture 5/strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | int main(int argc, char const *argv[]) 9 | { 10 | string str = "abcde fght"; 11 | string str2 = "xyz"; 12 | string str3 = str+str2; 13 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n; 8 | cout<<"Enter the Number"<>n; 10 | 11 | cout<<"Entered"< 2 | 3 | using namespace std; 4 | 5 | int fibonacci(int n){ 6 | if(n==0 or n==1){ 7 | return n; 8 | } 9 | 10 | int a = 0; 11 | int b = 1; 12 | 13 | int sum; 14 | 15 | for(int i=2;i<=n;i++){ 16 | sum = a+b; 17 | a = b; 18 | b = sum; 19 | } 20 | 21 | return sum; 22 | } 23 | 24 | int main(){ 25 | 26 | int n; 27 | cin>>n; 28 | 29 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool isZeroSumSub(int *arr, int n) { 6 | unordered_map mp; 7 | int sum = 0; 8 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // code starts 8 | int i, n, j; 9 | cin >> n; 10 | int a[n]; 11 | for (i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | } 14 | for (j = 0; j < n; j++) { 15 | for (i = 0; i < n - 1 - j; i++) { 16 | if (a[i] > a[i + 1]) { 17 | swap(a[i], a[i + 1]); 18 | } 19 | } 20 | } 21 | for (i = 0; i < n; i++) { 22 | cout << a[i] << " "; 23 | } 24 | } -------------------------------------------------------------------------------- /Lecture 4/subarray printing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // code starts 8 | int i, n, j; 9 | cin >> n; 10 | int a[n]; 11 | for (i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | } 14 | for (int sz = 1; sz <= n; sz++) { 15 | for (int st = 0; st + sz <= n; st++) { 16 | int en = st + sz - 1; 17 | for (i = st; i <= en; i++) { 18 | cout << a[i] << " "; 19 | } 20 | cout << '\n'; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Lecture 1/basics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | cout<<"Hello World!"; 8 | cout<=b and a>=c){ 19 | cout<=a and b>=c){ 22 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n; 8 | cin>>n; 9 | 10 | for(int row = 1; row<=n; row++){ 11 | 12 | int val = row%2==0 ? 0 : 1; 13 | 14 | for(int col=1;col<=row;col++){ 15 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // code starts 8 | int i, n, j; 9 | cin >> n; 10 | int a[n]; 11 | for (i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | } 14 | for (i = 0; i < n; i++) { 15 | int min_index = i; 16 | for (j = i; j < n; j++) { 17 | if (a[j] < a[min_index]) min_index = j; 18 | } 19 | swap(a[i], a[min_index]); 20 | } 21 | for (i = 0; i < n; i++) { 22 | cout << a[i] << " "; 23 | } 24 | } -------------------------------------------------------------------------------- /Lecture 4/insertion sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // code starts 8 | int i, n; 9 | cin >> n; 10 | int a[n]; 11 | for (i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | } 14 | for (i = 0; i < n; i++) { 15 | int j = i - 1; 16 | while (j >= 0 && a[j + 1] < a[j]) { 17 | swap(a[j + 1], a[j]); 18 | j--; 19 | } 20 | } 21 | for (i = 0; i < n; i++) { 22 | cout << a[i] << " "; 23 | } 24 | } -------------------------------------------------------------------------------- /Lecture 4/bubble_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // code starts 8 | int i, n; 9 | cin >> n; 10 | int a[n]; 11 | for (i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | } 14 | while (1) { 15 | bool check = false; 16 | for (i = 0; i <= n - 2; i++) { 17 | if (a[i] > a[i + 1]) { 18 | swap(a[i], a[i + 1]); 19 | check = true; 20 | } 21 | } 22 | if (check == false) { 23 | break; 24 | } 25 | } 26 | for (i = 0; i < n; i++) { 27 | cout << a[i] << " "; 28 | } 29 | } -------------------------------------------------------------------------------- /Lecture 4/print array in reverse order.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // code starts 8 | int n; 9 | cin >> n; 10 | // initialise the array 11 | // be careful to initialise after n has a constant value 12 | int array[n]; 13 | for (int i = 0; i < n; i++) { 14 | // input every element of the array 15 | cin >> array[i]; 16 | } 17 | // iterate backwards 18 | for (int i = n - 1; i >= 0; i--) { 19 | cout << array[i] << '\n'; 20 | } 21 | } -------------------------------------------------------------------------------- /Lecture 5/rotateArray.cpp: -------------------------------------------------------------------------------- 1 | //rotate arr[] of size n by k elements. 2 | #include 3 | using namespace std; 4 | int main(int argc, char const *argv[]) 5 | { 6 | int n,k; 7 | cin>>n>>k; 8 | char arr[100]; 9 | cin>>arr; 10 | for (int i = 0; i < k; ++i) 11 | { 12 | int temp = arr[n-1]; 13 | for (int i = n-2; i >= 0 ; i--) 14 | { 15 | arr[i+1]=arr[i]; 16 | } 17 | 18 | arr[0]=temp; 19 | } 20 | 21 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int i, n; 8 | cin >> n; 9 | int a[n]; 10 | for (i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | int max_end_here = 0, ans = -1e9; 14 | // handles cases when all numbers are negative 15 | for (i = 0; i < n; i++) { 16 | if (max_end_here < 0) { 17 | max_end_here = 0; 18 | } 19 | max_end_here = max_end_here + a[i]; 20 | ans = max(ans, max_end_here); 21 | } 22 | cout << ans; 23 | } -------------------------------------------------------------------------------- /Lecture 6/kadaneAlgo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int kadanesAlgo(int arr[],int n){ 6 | 7 | int max_subarray,global_max; 8 | max_subarray = global_max = arr[0]; 9 | 10 | for(int i=1;i global_max){ 14 | global_max = max_subarray; 15 | } 16 | } 17 | 18 | return global_max; 19 | } 20 | 21 | int main(){ 22 | 23 | int arr[] = {2,-3,2,3,-1}; 24 | int brr[] = {-2,-3,-1,-5}; 25 | 26 | cout< 2 | 3 | using namespace std; 4 | 5 | void swap(int arr[],int i,int j){ 6 | int temp = arr[i]; 7 | arr[i] = arr[j]; 8 | arr[j] = temp; 9 | } 10 | 11 | int main(){ 12 | 13 | int arr[] = {1,2,3,4,5,6,7}; 14 | 15 | for(int i=0;i<7;i++){ 16 | cout< 2 | using namespace std; 3 | 4 | int main(int argc, char const *argv[]) 5 | { 6 | int a = 5; 7 | int* ad = new int(10); 8 | 9 | cout<"<<*(ad)<>n; 22 | int* arr1 = new int[n]; 23 | for (int l = 0; l < n; ++l) 24 | { 25 | arr1[l] = l; 26 | } 27 | for (int k = 0; k < n; k++) 28 | { 29 | cout< 2 | 3 | using namespace std; 4 | 5 | int setbitsCount(int n){ 6 | 7 | int count = 0; 8 | 9 | while(n!=0){ 10 | int temp = (n&1); 11 | count += temp; //count = count + temp 12 | 13 | n = n>>1; 14 | } 15 | 16 | return count; 17 | } 18 | 19 | int main(){ 20 | 21 | // cout<<(5&7)<>1)< 2 | 3 | using namespace std; 4 | 5 | void swap(int arr[],int i,int j){ 6 | int temp = arr[i]; 7 | arr[i] = arr[j]; 8 | arr[j] = temp; 9 | } 10 | 11 | void insertionSort(int arr[],int n){ 12 | 13 | int divider = 1; 14 | 15 | while(divider0;i--){ 17 | if(arr[i-1]>arr[i]){ 18 | swap(arr,i,i-1); 19 | }else{ 20 | break; 21 | } 22 | } 23 | 24 | divider++; 25 | } 26 | } 27 | 28 | int main(){ 29 | 30 | int arr[] = {5,4,3,2,1}; 31 | 32 | insertionSort(arr,5); 33 | 34 | for(int i=0;i<5;i++){ 35 | cout< 2 | using namespace std; 3 | int main(int argc, char const *argv[]) 4 | { 5 | 6 | // char arr[5] = "word"; 7 | // cout<>charr2; 32 | cin.getline(charr2,40); 33 | cout< 2 | 3 | using namespace std; 4 | 5 | void printPattern(int n){ 6 | 7 | for(int row = 1;row <= n; row++){ 8 | 9 | for(int space = 1;space <= n - row; space++){ 10 | cout<<" "; 11 | } 12 | 13 | for(int star = 1;star <= 2*row - 1;star++){ 14 | cout<<"*"; 15 | } 16 | 17 | cout<=1;row--){ 21 | 22 | for(int space = 1;space <= n - row; space++){ 23 | cout<<" "; 24 | } 25 | 26 | for(int star = 1;star <= 2*row - 1;star++){ 27 | cout<<"*"; 28 | } 29 | cout<>n; 37 | 38 | printPattern(n); 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Lecture 2/pattern_5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // int x = 10; 8 | 9 | // x == 10 ? cout<<"Yes"<>n; 24 | 25 | for(int row = 1;row<=n;row++){ 26 | 27 | //Spaces 28 | for(int space = 1;space <= n - row; space++){ 29 | cout<<" "; 30 | } 31 | 32 | //Stars 33 | for(int star = 1;star <= 2*row - 1;star++){ 34 | cout<<"*"; 35 | } 36 | 37 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // int x = 10.23; 8 | 9 | // cout< 2 | #include 3 | using namespace std; 4 | 5 | int longestZeroSumSub(int *arr, int n) { 6 | unordered_map mp; // cumulativesum, first_index 7 | int sum = 0; 8 | int maxLen = INT_MIN; 9 | for(int i=0;i maxLen) { 15 | maxLen = newLen; 16 | } 17 | } else { 18 | mp[sum] = i; 19 | } 20 | } 21 | return maxLen; 22 | } 23 | 24 | int main(int argc, char const *argv[]) 25 | { 26 | int n; 27 | cin>>n; 28 | int *arr = new int[n](); 29 | for(int i=0;i>arr[i]; 31 | } 32 | cout< 2 | #include 3 | using namespace std; 4 | int longestKSumSub(int *arr, int n, int k) { 5 | unordered_map mp; // cumulativesum, first_index 6 | int sum = 0; 7 | int maxLen = INT_MIN; 8 | for(int i=0;i maxLen) { 14 | maxLen = newLen; 15 | } 16 | } else { 17 | mp[sum] = i; 18 | } 19 | } 20 | return maxLen; 21 | } 22 | 23 | 24 | int main(int argc, char const *argv[]) 25 | { 26 | int n, k; 27 | cin>>n>>k; 28 | int *arr = new int[n](); 29 | for(int i=0;i>arr[i]; 31 | } 32 | cout< 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | string reverseWord(string str){ 9 | //cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | // char arr[] = {'a','b','c'}; 9 | // cout<>verse; 20 | // cout<>s1; 33 | // cout< 2 | 3 | using namespace std; 4 | 5 | bool last(int nothing){ 6 | if(nothing){ 7 | return true; 8 | }else{ 9 | return false; 10 | } 11 | 12 | cout<<"This is the last value "< 2 | 3 | using namespace std; 4 | 5 | class Stack{ 6 | 7 | int top; 8 | int *arr; 9 | int capacity; 10 | 11 | public: 12 | Stack(int capacity = 10){ 13 | this->capacity = capacity; 14 | arr = new int[capacity]; 15 | top = -1; 16 | } 17 | 18 | int size(){ 19 | return top+1; 20 | } 21 | 22 | bool empty(){ 23 | return size()==0; 24 | } 25 | 26 | int last(){ 27 | return arr[top]; 28 | } 29 | 30 | void push(int data){ 31 | if(capacity==size()){ 32 | cout<<"Stack is full"< 2 | 3 | using namespace std; 4 | 5 | int factorial(int val){ 6 | int result = 1; 7 | 8 | for(int i=1;i<=val;i++){ 9 | result = result*i; 10 | } 11 | 12 | return result; 13 | } 14 | 15 | 16 | int main(){ 17 | 18 | int n = 6; 19 | int r = 4; 20 | int nr = n - r; 21 | 22 | // int n_fact = 1; 23 | // for(i = 1;i<=n;i++){ 24 | // n_fact = n_fact * i; 25 | // } 26 | 27 | // int r_fact = 1; 28 | // for(int i = 1;i<=r;i++){ 29 | // r_fact = r_fact * i; 30 | // } 31 | 32 | // int nr_fact = 1; 33 | // for(int i = 1;i <= n-r;i++){ 34 | // nr_fact = nr_fact*i; 35 | // } 36 | 37 | // int factorial = n_fact/(r_fact*nr_fact); 38 | 39 | // cout< 2 | 3 | // using namespace std; 4 | 5 | // void increment(int* aptr){ 6 | // *aptr = *aptr + 2; 7 | // cout<<"Inside Function "<<*aptr< 21 | 22 | using namespace std; 23 | 24 | // void increment(int* aptr){ 25 | // *aptr = *aptr + 2; 26 | // cout<<"Inside Function "<<*aptr< 2 | using namespace std; 3 | int main(int argc, char const *argv[]) 4 | { 5 | int arr1[2][3]; 6 | 7 | int row,col; 8 | cin>>row>>col; 9 | int** arr = new int*[row]; 10 | 11 | for (int i = 0; i < row; ++i) 12 | { 13 | arr[i] = new int[col]; 14 | } 15 | 16 | for (int i = 0; i < row; ++i) 17 | { 18 | for (int j = 0; j < col; ++j) 19 | { 20 | arr[i][j] = i+j; 21 | } 22 | } 23 | for (int i = 0; i < row; ++i) 24 | { 25 | for (int j = 0; j < col; ++j) 26 | { 27 | cout<= 0; j--) 45 | { 46 | cout< 2 | 3 | using namespace std; 4 | 5 | void printSubstring(string str){ 6 | 7 | for(int i=0;i=0 and i+j=0 and i+j+1 4 | using namespace std; 5 | 6 | 7 | string balancedBrackets(string str) 8 | { 9 | int depth = 0; 10 | 11 | int minDep = 0; 12 | 13 | for (int i = 0; i < str.length(); i++) { 14 | if (str[i] == '(') 15 | depth++; 16 | else 17 | depth--; 18 | 19 | if (minDep > depth) 20 | minDep = depth; 21 | } 22 | 23 | if (minDep < 0) { 24 | for (int i = 0; i < abs(minDep); i++) 25 | str = '(' + str; 26 | } 27 | 28 | depth = 0; 29 | 30 | for (int i = 0; i < str.length(); i++) { 31 | if (str[i] == '(') 32 | depth++; 33 | else 34 | depth--; 35 | } 36 | 37 | if (depth != 0) { 38 | for (int i = 0; i < depth; i++) 39 | str = str + ')'; 40 | } 41 | return str; 42 | } 43 | 44 | 45 | int main() 46 | { 47 | string str = "())))()("; 48 | cout << balancedBrackets(str); 49 | } -------------------------------------------------------------------------------- /Lecture 16-17/StackGeneric.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | class Stack{ 7 | 8 | int top; 9 | T *arr; 10 | int capacity; 11 | 12 | public: 13 | Stack(int capacity = 10){ 14 | this->capacity = capacity; 15 | arr = new T[capacity]; 16 | top = -1; 17 | } 18 | 19 | int size(){ 20 | return top+1; 21 | } 22 | 23 | bool empty(){ 24 | return size()==0; 25 | } 26 | 27 | T last(){ 28 | return arr[top]; 29 | } 30 | 31 | void push(T data){ 32 | if(capacity==size()){ 33 | cout<<"Stack is full"< s; 55 | // s.push(4); 56 | // s.push(3); 57 | // s.push(2); 58 | // s.push(1); 59 | 60 | // cout< s; 66 | 67 | s.push('X'); 68 | s.push('Y'); 69 | s.push('Z'); 70 | s.push('A'); 71 | s.push('B'); 72 | 73 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Car{ 8 | public: 9 | int speed; 10 | char *name; 11 | 12 | void startCar(){ 13 | cout<<"Ignition"<speed = speed; 27 | name = new char[strlen(str)+1]; 28 | strcpy(name,str); 29 | // name = str; 30 | } 31 | 32 | void printDetails(){ 33 | cout<speed<speed = X.speed; 39 | name = new char[strlen(X.name)+1]; 40 | strcpy(name,X.name); 41 | } 42 | 43 | void operator =(Car &X){ 44 | this->speed = X.speed; 45 | name = new char[strlen(X.name)+1]; 46 | strcpy(name,X.name); 47 | } 48 | 49 | ~Car(){ 50 | cout<<"Destruction"< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Point{ 9 | public: 10 | int value; 11 | int idx; 12 | int arrayPosition; 13 | 14 | Point(int value,int idx,int arrayPosition){ 15 | this->value = value; 16 | this->idx = idx; 17 | this->arrayPosition = arrayPosition; 18 | } 19 | }; 20 | 21 | class PointCompare{ 22 | public: 23 | bool operator()(Point a,Point b){ 24 | return a.value > b.value; 25 | } 26 | }; 27 | 28 | 29 | int main(){ 30 | 31 | priority_queue,PointCompare > pq; 32 | 33 | int arr[3][5] = { 34 | {1,5,7,10,0}, 35 | {2,4,8,9,0}, 36 | {3,6,9,11,0}, 37 | }; 38 | 39 | int k = 3; 40 | 41 | for(int i=0;i v; 47 | 48 | while(pq.top().value !=INT_MAX){ 49 | 50 | Point temp = pq.top(); 51 | pq.pop(); 52 | 53 | v.push_back(temp.value); 54 | int kth = temp.arrayPosition; 55 | int pos = temp.idx + 1; 56 | 57 | int newValue = arr[kth][pos]; 58 | 59 | if(newValue==0){ 60 | newValue = INT_MAX; 61 | } 62 | 63 | Point obj(newValue,pos,kth); 64 | pq.push(obj); 65 | } 66 | 67 | for(auto var:v){ 68 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | class node{ 7 | public: 8 | char data; 9 | bool isTerminal; 10 | unordered_map h; 11 | 12 | node(char data){ 13 | this->data = data; 14 | } 15 | }; 16 | 17 | class Trie{ 18 | node* root; 19 | 20 | public: 21 | Trie(){ 22 | root = new node('\0'); 23 | } 24 | 25 | void addWord(string word){ 26 | 27 | node* temp = root; 28 | 29 | for(int i=0;ih.count(ch)){ 33 | temp = temp->h[ch]; 34 | }else{ 35 | node* child = new node(ch); 36 | temp->h[ch] = child; 37 | temp = temp->h[ch]; 38 | } 39 | } 40 | 41 | temp->isTerminal = true; 42 | } 43 | 44 | bool search(string word){ 45 | node* temp = root; 46 | 47 | for(int i=0;ih.count(ch)){ 51 | temp = temp->h[ch]; 52 | }else{ 53 | return false; 54 | } 55 | } 56 | 57 | return temp->isTerminal; 58 | } 59 | }; 60 | int main(){ 61 | Trie t; 62 | string arr[] = {"apple","ape","application","note","news"}; 63 | for(int i=0;i<5;i++){ 64 | t.addWord(arr[i]); 65 | } 66 | cout< 2 | 3 | using namespace std; 4 | 5 | bool isPalindrome(string str){ 6 | 7 | int left = 0; 8 | int right = str.length() - 1; 9 | 10 | while(left<=right){ 11 | 12 | if(str[left]!=str[right]){ 13 | return false; 14 | } 15 | 16 | left++; 17 | right--; 18 | 19 | } 20 | 21 | return true; 22 | 23 | } 24 | 25 | string toggle(string str){ 26 | 27 | for(int i=0;i='A' and str[i]<='Z'){ 30 | str[i] = str[i] + 'a' - 'A'; 31 | }else if(str[i]>='a' and str[i]<='z'){ 32 | str[i] = str[i] + 'A' - 'a'; 33 | } 34 | } 35 | 36 | return str; 37 | } 38 | 39 | int maxFrequency(string str){ 40 | int freq[256] = {0}; 41 | 42 | for(int i=0;i max){ 52 | max = freq[i]; 53 | } 54 | } 55 | 56 | return max; 57 | } 58 | 59 | int main(){ 60 | 61 | // string str = "NamaN"; 62 | 63 | // cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | class ActivityTime{ 7 | public: 8 | int start,end; 9 | }; 10 | 11 | int compare(ActivityTime a,ActivityTime b){ 12 | return a.end 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // bool p = 12; 8 | // int p = 12; 9 | 10 | int i = 123; 11 | 12 | cout< 63 | 64 | // using namespace std; 65 | 66 | // int main(){ 67 | 68 | // if(123){ 69 | // int i = 0; 70 | // cout< 2 | 3 | using namespace std; 4 | 5 | class Queue{ 6 | 7 | int front; 8 | int rear; 9 | int maxSize; 10 | int currSize; 11 | int *arr; 12 | 13 | public: 14 | Queue(int capacity = 10){ 15 | maxSize = capacity; 16 | arr = new int[capacity]; 17 | front = 0; 18 | rear = maxSize-1; 19 | currSize = 0; 20 | } 21 | 22 | int size(){ 23 | return currSize; 24 | } 25 | 26 | bool isFull(){ 27 | return currSize==maxSize; 28 | } 29 | 30 | bool empty(){ 31 | return currSize==0; 32 | } 33 | 34 | int getFront(){ 35 | if(empty()){ 36 | cout<<"Queue is empty"< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Coordinates{ 8 | public: 9 | int x; 10 | int y; 11 | int point; 12 | 13 | Coordinates(int point,int x,int y){ 14 | this->point = point; 15 | this->x = x; 16 | this->y = y; 17 | } 18 | 19 | int distance(){ 20 | return x*x + y*y; 21 | } 22 | 23 | void display(){ 24 | cout<<"Point Number "< b.distance(); 32 | } 33 | }; 34 | 35 | int main(){ 36 | 37 | int arr[3][10] = { 38 | {1,5,7,10}, 39 | {2,4,8}, 40 | {3,6,9,11}, 41 | }; 42 | 43 | // priority_queue pq; 44 | 45 | // priority_queue,greater > pq; 46 | 47 | // pq.push(15); 48 | // pq.push(5); 49 | // pq.push(2); 50 | // pq.push(156); 51 | // pq.push(4); 52 | // pq.push(6); 53 | // pq.push(160); 54 | 55 | // while(!pq.empty()){ 56 | // cout<,CoordinatesCompare> crPQ; 61 | 62 | // int x[] = {1,2,1,5,6,4}; 63 | // int y[] = {1,1,0,2,1,2}; 64 | 65 | // for(int i=0;i<6;i++){ 66 | // Coordinates a(i,x[i],y[i]); 67 | // crPQ.push(a); 68 | // } 69 | 70 | // while(!crPQ.empty()){ 71 | // Coordinates a = crPQ.top(); 72 | // a.display(); 73 | // crPQ.pop(); 74 | // } 75 | 76 | // CoordinatesCompare cc; 77 | // cc(); 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /Lecture 24/heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Heap{ 7 | 8 | bool minHeap; 9 | vector v; 10 | 11 | void heapify(int i){ 12 | 13 | int left = 2*i; 14 | int right = 2*i + 1; 15 | 16 | int minIndex = i; 17 | 18 | if(leftb; 37 | } 38 | } 39 | 40 | public: 41 | Heap(bool type = true){ 42 | minHeap = type; 43 | v.push_back(-1); 44 | } 45 | 46 | void push(int data){ 47 | 48 | v.push_back(data); 49 | int index = v.size() - 1; 50 | int parent = index/2; 51 | 52 | while(index>1 and compare(v[index],v[parent])){ 53 | swap(v[index],v[parent]); 54 | index = parent; 55 | parent= index/2; 56 | } 57 | } 58 | 59 | int top(){ 60 | return v[1]; 61 | } 62 | 63 | bool empty(){ 64 | return v.size()==1; 65 | } 66 | 67 | void pop(){ 68 | int last = v.size() - 1; 69 | swap(v[1],v[last]); 70 | v.pop_back(); 71 | 72 | heapify(1); 73 | } 74 | }; 75 | 76 | int main(){ 77 | 78 | Heap h(false); 79 | 80 | h.push(15); 81 | h.push(5); 82 | h.push(2); 83 | h.push(156); 84 | h.push(4); 85 | h.push(6); 86 | h.push(160); 87 | 88 | // cout< 2 | 3 | using namespace std; 4 | 5 | int uniqueNumberTriplet(int arr[],int n){ 6 | 7 | int bitAdder[32] = {0}; 8 | 9 | for(int i=0;i=0;j--){ 14 | 15 | int temp = (no&1); 16 | bitAdder[j]+=temp; 17 | no = no>>1; 18 | 19 | if(no==0){ 20 | break; 21 | } 22 | } 23 | } 24 | 25 | int multiply = 1; 26 | int number = 0; 27 | 28 | for(int i=31;i>=0;i--){ 29 | bitAdder[i] = bitAdder[i]%3; 30 | // number += (1<<(31-i))*bitAdder[i]; 31 | number+= multiply*bitAdder[i]; 32 | multiply = multiply<<1; 33 | } 34 | 35 | return number; 36 | } 37 | 38 | int uniqueNumberPairs(int arr[],int n){ 39 | 40 | int Xor = arr[0]; 41 | 42 | for(int i=1;i>1; 65 | position++; 66 | } 67 | } 68 | 69 | int first_unique = 0; 70 | 71 | for(int i=0;i>position; 76 | 77 | if(shift&1){ 78 | first_unique = first_unique^arr[i]; 79 | } 80 | } 81 | 82 | int second_unique = Xor^first_unique; 83 | 84 | cout<<"First Number "< 2 | #include 3 | 4 | using namespace std; 5 | 6 | class node{ 7 | public: 8 | int data; 9 | node* left; 10 | node* right; 11 | 12 | node(int data){ 13 | this->data = data; 14 | this->left = NULL; 15 | this->right = NULL; 16 | } 17 | 18 | node(){ 19 | this->left = NULL; 20 | this->right = NULL; 21 | } 22 | }; 23 | 24 | void insert(node* root,int data){ 25 | 26 | for(int i=31;i>=0;i--){ 27 | 28 | int bit = (data>>i)&1; 29 | 30 | if(bit){ 31 | 32 | if(!root->right){ 33 | node* child = new node(); 34 | root->right = child; 35 | } 36 | root = root->right; 37 | }else{ 38 | 39 | if(!root->left){ 40 | node* child = new node(); 41 | root->left = child; 42 | } 43 | root = root->left; 44 | 45 | } 46 | } 47 | } 48 | 49 | int maxXor(int arr[],int n,node* head){ 50 | 51 | int maxVal = 0; 52 | 53 | for(int i=0;i=0;j--){ 61 | 62 | int bit = (val>>j)&1; 63 | 64 | if(bit){ 65 | 66 | if(root->left){ 67 | currXor += pow(2,j); 68 | root = root->left; 69 | }else{ 70 | root = root->right; 71 | } 72 | 73 | }else{ 74 | 75 | if(root->right){ 76 | currXor+=pow(2,j); 77 | root = root->right; 78 | }else{ 79 | root = root->left; 80 | } 81 | 82 | } 83 | } 84 | 85 | maxVal = max(maxVal,currXor); 86 | } 87 | 88 | return maxVal; 89 | } 90 | 91 | int main(){ 92 | 93 | int arr[] = {1,2,3,4}; 94 | 95 | node* root = new node(); 96 | 97 | for(int i=0;i<4;i++){ 98 | insert(root,arr[i]); 99 | } 100 | 101 | cout< 2 | 3 | using namespace std; 4 | 5 | string ultraFast(string s1,string s2){ 6 | 7 | 8 | 9 | for(int i=0;i build 53 | cout< closingB){ 58 | generateParathesis(n,openingB,closingB+1,ans+')'); 59 | } 60 | 61 | if(openingB < n){ 62 | generateParathesis(n,openingB+1,closingB,ans+'('); 63 | } 64 | } 65 | 66 | int optimalGame(int arr[],int i,int j){ 67 | if(i>j){ 68 | return 0; 69 | } 70 | 71 | int result; 72 | 73 | int first = arr[i] + min(optimalGame(arr,i+2,j),optimalGame(arr,i+1,j-1)); 74 | int last = arr[j] + min(optimalGame(arr,i+1,j-1),optimalGame(arr,i,j-2)); 75 | 76 | result = max(first,last); 77 | 78 | return result; 79 | } 80 | 81 | int tillingProblem(int n){ 82 | // if(n==1){ 83 | // return 1; 84 | // } 85 | 86 | // if(n==2){ 87 | // return 2; 88 | // } 89 | if(n==1 or n==2){ 90 | return n; 91 | } 92 | 93 | int horizontal = tillingProblem(n-2); 94 | int vertical = tillingProblem(n-1); 95 | 96 | int total=horizontal+vertical; 97 | return total; 98 | } 99 | 100 | int main(){ 101 | 102 | // string s1 = "10111"; 103 | // string s2 = "10000"; 104 | 105 | // cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | template 11 | class Graph{ 12 | 13 | unordered_map > > adjList; 14 | 15 | public: 16 | Graph(){} 17 | 18 | void addEdge(T u,T v,int weight,bool bidir = true){ 19 | adjList[u].push_back(make_pair(v,weight)); 20 | 21 | if(bidir){ 22 | adjList[v].push_back(make_pair(u,weight)); 23 | } 24 | } 25 | 26 | void display(){ 27 | for(auto node:adjList){ 28 | cout< "; 29 | 30 | for(auto vertex:node.second){ 31 | cout<<"( "< > s; 40 | 41 | unordered_map dist; 42 | 43 | for(auto node:adjList){ 44 | dist[node.first] = INT_MAX; 45 | } 46 | 47 | dist[src] = 0; 48 | 49 | s.insert(make_pair(0,src)); 50 | 51 | while(!s.empty()){ 52 | 53 | auto val = *(s.begin()); 54 | 55 | T node = val.second; 56 | int nodeDist = val.first; 57 | 58 | s.erase(s.begin()); 59 | 60 | for(auto neighbor:adjList[node]){ 61 | 62 | if(nodeDist + neighbor.second < dist[neighbor.first]){ 63 | 64 | T destination = neighbor.first; 65 | 66 | auto removal = s.find(make_pair(dist[destination],destination)); 67 | 68 | if(removal!=s.end()){ 69 | s.erase(removal); 70 | } 71 | 72 | dist[destination] = nodeDist + neighbor.second; 73 | s.insert(make_pair(dist[destination],destination)); 74 | } 75 | 76 | } 77 | } 78 | 79 | for(auto node:dist){ 80 | cout<<"Node "< g; 88 | 89 | g.addEdge(1,2,1); 90 | g.addEdge(1,3,4); 91 | g.addEdge(1,4,7); 92 | g.addEdge(4,3,2); 93 | g.addEdge(2,3,2); 94 | 95 | g.display(); 96 | 97 | g.dijkstra(1); 98 | 99 | return 0; 100 | } -------------------------------------------------------------------------------- /Lecture 24/questions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | long minCost(){ 8 | int arr[] = {3, 7, 9, 4}; 9 | 10 | priority_queue,greater > pq; 11 | // priority_queue pq; 12 | 13 | for(int i=0;i<4;i++){ 14 | pq.push(arr[i]); 15 | } 16 | 17 | long sum = 0; 18 | 19 | while (pq.size() > 1) { 20 | int first = pq.top(); 21 | pq.pop(); 22 | int second = pq.top(); 23 | pq.pop(); 24 | 25 | sum += first + second; 26 | pq.push(first + second); 27 | } 28 | 29 | return sum; 30 | } 31 | 32 | int KthLargestElement(int arr[],int n,int k){ 33 | 34 | priority_queue,greater > pq; 35 | 36 | for(int i =0;i pq.top()){ 45 | pq.pop(); 46 | pq.push(var); 47 | } 48 | } 49 | 50 | return pq.top(); 51 | } 52 | 53 | void downHeapify(int arr[],int i,int n){ 54 | 55 | int left = 2*i + 1; 56 | int right = 2*i + 2; 57 | 58 | int largestIndex = i; 59 | 60 | if(left=0;i--){ 77 | downHeapify(arr,i,n); 78 | } 79 | 80 | for(int i=n-1;i>=0;i--){ 81 | swap(arr[i],arr[0]); 82 | downHeapify(arr,0,i); 83 | } 84 | 85 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void upheapify(vector&heap,int idx) 6 | { 7 | if(idx==0) 8 | { 9 | return; 10 | } 11 | int parentidx = (idx-1)/2; 12 | if(heap[parentidx] < heap[idx]) 13 | { 14 | //swap 15 | swap(heap[parentidx] , heap[idx]); 16 | upheapify(heap , parentidx); 17 | } 18 | 19 | else 20 | { 21 | return ; 22 | } 23 | 24 | } 25 | void downheapify( vector &heap , int idx) 26 | { 27 | int lidx = 2*idx + 1; 28 | int ridx = 2*idx + 2; 29 | if(lidx >= heap.size() && ridx >= heap.size()) 30 | { 31 | return; 32 | } 33 | int tempidx = idx; 34 | if(lidx < heap.size() && heap[lidx] > heap[tempidx]) 35 | { 36 | tempidx = lidx; 37 | } 38 | if(lidx < heap.size() && heap[ridx] > heap[tempidx]) 39 | { 40 | tempidx=ridx; 41 | } 42 | if(tempidx==idx) 43 | return; 44 | swap(heap[tempidx],heap[idx]); 45 | downheapify(heap,tempidx); 46 | 47 | } 48 | void deletehighestpriority(vector&heap) 49 | { 50 | swap(heap[0] , heap[heap.size()-1]) ; 51 | heap.pop_back() ; 52 | downheapify(heap,0) ; 53 | } 54 | void insert(vector&heap,int key) 55 | { 56 | heap.push_back(key) ; 57 | upheapify(heap , heap.size() - 1) ; 58 | } 59 | void buildheap(vector&heap) 60 | { 61 | for(int i=heap.size()-1 ; i >= 0 ; i--) 62 | downheapify(heap,1); 63 | } 64 | void display(vector heap) 65 | { 66 | for(int i = 0 ; i < heap.size() ; i++) 67 | cout<< heap[i] <<" "; 68 | cout<< endl; 69 | } 70 | int main() 71 | { 72 | #ifndef ONLINE_JUDGE 73 | freopen("input.txt", "r", stdin); 74 | freopen("output.txt", "w", stdout); 75 | #endif 76 | 77 | vector heap; 78 | int n; 79 | cin>>n; 80 | 81 | for(int i = 0 ; i < n ; i++) 82 | { 83 | int x; 84 | cin>>x; 85 | heap.push_back(x); 86 | } 87 | buildheap(heap); 88 | display(heap); 89 | deletehighestpriority(heap); 90 | display(heap); 91 | return 0; 92 | 93 | } -------------------------------------------------------------------------------- /Lecture 16-17/StackQuestions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool balancedParathesis(string str){ 7 | 8 | stack s; 9 | 10 | for(int i=0;i s; 34 | 35 | for(int i=0;i s; 67 | int max_area = 0; 68 | 69 | int i=0; 70 | 71 | while(imax_area){ 89 | max_area = currArea; 90 | } 91 | 92 | } 93 | } 94 | 95 | while(!s.empty()){ 96 | int extractedTop = s.top(); 97 | s.pop(); 98 | 99 | int currArea=0; 100 | 101 | if(s.empty()){ 102 | currArea = arr[extractedTop]*i; 103 | }else{ 104 | currArea = arr[extractedTop]*(i - s.top() - 1); 105 | } 106 | 107 | if(currArea>max_area){ 108 | max_area = currArea; 109 | } 110 | } 111 | 112 | return max_area; 113 | } 114 | 115 | int main(){ 116 | 117 | // stack s; 118 | 119 | // cout< 2 | using namespace std; 3 | 4 | int factorial(int n){ 5 | if(n==0){ 6 | return 1; 7 | } 8 | 9 | int fact_n1 = factorial(n-1); 10 | int fact_n = fact_n1*n; 11 | return fact_n; 12 | } 13 | 14 | int power(int n,int p){ 15 | if(p==0){ 16 | return 1; 17 | } 18 | 19 | int power_n1 = power(n,p-1); 20 | int power_n = power_n1*n; 21 | return power_n; 22 | } 23 | 24 | void printIncreasing(int n){ 25 | if(n==0){ 26 | return; 27 | } 28 | 29 | printIncreasing(n-1); 30 | cout< 2 | using namespace std; 3 | 4 | void swap(int arr[],int i,int j){ 5 | int temp = arr[i]; 6 | arr[i] = arr[j]; 7 | arr[j] = temp; 8 | } 9 | 10 | void bubble(int arr[1000000], int n){ 11 | int counter = 1; 12 | 13 | while(counterarr[i+1]){ 16 | swap(arr,i,i+1); 17 | } 18 | } 19 | counter++; 20 | } 21 | } 22 | int main(int argc, char const *argv[]) 23 | { 24 | int arr[1000000]; 25 | for (int i = 0; i < 1000000 ; ++i) 26 | { 27 | arr[i] = 1000000-i; 28 | } 29 | clock_t start = clock(); 30 | bubble(arr,10000); 31 | clock_t end_1 = clock(); 32 | sort(arr,arr+10000); 33 | clock_t end_2 = clock(); 34 | 35 | cout<<(end_1 - start)<>n; 40 | // //O(n) 41 | // for(int i =0; i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // int x; 8 | // cout<<(&x)< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Car{ 8 | private: 9 | int speed; 10 | char name[20]; 11 | int model_no; 12 | public: 13 | 14 | void startCar(){ 15 | cout<<"Ignition"<speed = speed; 28 | strcpy(name,str); 29 | } 30 | 31 | Car(int speed,int model_no,char str[]){ 32 | this->speed = speed; 33 | strcpy(name,str); 34 | this->model_no = model_no; 35 | } 36 | 37 | void printDetails(){ 38 | cout<speed<speed; 45 | } 46 | 47 | void setSpeed(int speed){ 48 | if(speed<0){ 49 | cout<<"Not Valid"<speed = speed; 53 | } 54 | 55 | Car(Car &X){ 56 | this->speed = X.speed; 57 | this->model_no = X.model_no; 58 | strcpy(this->name,X.name); 59 | } 60 | }; 61 | 62 | int main(){ 63 | 64 | // Car droom(10); 65 | // droom.speed = 100; 66 | // cout<printDetails(); 118 | 119 | // int *a = new int(30); 120 | // int *arr = new int[3]; 121 | 122 | // for(int i=1;i<=3;i++){ 123 | // arr[i-1] = i; 124 | // cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | // bool isCBNumber(int n){ 7 | 8 | // int arr[] = {2,3,5,7,11,13,17,19,23,29}; 9 | 10 | // for(int i=0;i<10;i++){ 11 | // if(n==arr[i]){ 12 | // return true; 13 | // } 14 | // } 15 | 16 | // for(int i=0;i<10;i++){ 17 | // if(n%arr[i]==0){ 18 | // return false; 19 | // } 20 | // } 21 | 22 | // return true; 23 | // } 24 | 25 | // bool inRange(int s,int e,bool visited[]){ 26 | 27 | // for(int i=s;i<=e;i++){ 28 | // if(visited[i]){ 29 | // return false; 30 | // } 31 | // } 32 | 33 | // return true; 34 | // } 35 | 36 | // int cbNumberCount(){ 37 | // string str = "81615"; 38 | 39 | // int count = 0; 40 | 41 | // bool visited[str.length()]; 42 | 43 | // for(int i = 1;i<=str.length();i++){ 44 | // for(int j=0;j<=str.length()-i;j++){ 45 | 46 | // string substr = str.substr(j,i); 47 | // int num = stoi(substr); 48 | 49 | // int l = j; 50 | // int r = i+j-1; 51 | 52 | // if(inRange(l,r,visited) and isCBNumber(num)){ 53 | // count++; 54 | 55 | // for(int p = l;p<=r;p++){ 56 | // visited[p] = true; 57 | // } 58 | 59 | // } 60 | // } 61 | // } 62 | 63 | // return count; 64 | // } 65 | 66 | void printLexicographical(int n,int curr){ 67 | if(curr>n){ 68 | return; 69 | } 70 | 71 | cout<=0;i--){ 123 | cout<k){ 141 | count[str[left] - 'a']--; 142 | left++; 143 | }else{ 144 | ans++; 145 | } 146 | } 147 | 148 | return ans; 149 | } 150 | 151 | int main(){ 152 | 153 | // printLexicographical(21,0); 154 | 155 | // brokenCalculator(6); 156 | 157 | cout< 2 | 3 | using namespace std; 4 | 5 | void mappedString(string str,string ans){ 6 | if(str.length()==0){ 7 | cout<1){ 22 | char ch2 = str[1]; 23 | string ros2 = str.substr(2); 24 | 25 | int ch2_int = ch2 - '0'; 26 | ch2 = ch2_int + 'A' - 1; 27 | 28 | if(ch1_int*10 + ch2_int <= 26){ 29 | char second_char = ch1_int*10 + ch2_int + 'A' - 1; 30 | mappedString(ros2,ans+second_char); 31 | } 32 | } 33 | } 34 | 35 | string code[] = {" ",".","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 36 | 37 | void printKeypad(string str,string ans){ 38 | if(str.length()==0){ 39 | cout<end){ 79 | return 0; 80 | } 81 | 82 | int count = 0; 83 | 84 | for(int i=1;i<=6;i++){ 85 | count += countBoardPath(end,start+i); 86 | } 87 | 88 | return count; 89 | } 90 | 91 | int countMazePath(int sr,int sc,int er,int ec){ 92 | if(sr==er and sc==ec){ 93 | return 1; 94 | } 95 | 96 | if(sr>er or sc>ec){ 97 | return 0; 98 | } 99 | 100 | int count = 0; 101 | 102 | count+= countMazePath(sr+1,sc,er,ec); 103 | count+= countMazePath(sr,sc+1,er,ec); 104 | 105 | return count; 106 | } 107 | 108 | void printBoardPath(int end,int start,string path){ 109 | if(start==end){ 110 | cout<end){ 115 | return; 116 | } 117 | 118 | for(int i=1;i<=6;i++){ 119 | char x = i + '0'; 120 | printBoardPath(end,start+i,path + "->" +x); 121 | } 122 | 123 | return; 124 | } 125 | 126 | void printMazePath(int sr,int sc,int er,int ec,string path){ 127 | if(sr==er and sc==ec){ 128 | cout<er or sc>ec){ 133 | return; 134 | } 135 | 136 | printMazePath(sr+1,sc,er,ec,path + "-> V"); 137 | printMazePath(sr,sc+1,er,ec,path + "-> H"); 138 | 139 | return; 140 | } 141 | 142 | int main(){ 143 | 144 | // mappedString("2314",""); 145 | // printKeypad("34",""); 146 | 147 | // printPermutations("abc",""); 148 | 149 | // cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int countBinaryString(int n){ 8 | if(n==0 or n==1){ 9 | return n; 10 | } 11 | 12 | int withZero = countBinaryString(n-1); 13 | int withOne = countBinaryString(n-2); 14 | 15 | int total = withOne + withZero; 16 | return total; 17 | } 18 | 19 | int maxBiotonicSubarray(int arr[],int n){ 20 | 21 | int inc[n]; 22 | int dec[n]; 23 | 24 | inc[0]=1; 25 | dec[n-1]=1; 26 | 27 | for(int i=1;i=arr[i-1]){ 29 | inc[i] = inc[i-1]+1; 30 | }else{ 31 | inc[i] = 1; 32 | } 33 | } 34 | 35 | for(int i=n-2;i>=0;i--){ 36 | if(arr[i]>=arr[i+1]){ 37 | dec[i] = dec[i+1]+1; 38 | }else{ 39 | dec[i] = 1; 40 | } 41 | } 42 | 43 | int max = 0; 44 | 45 | for(int i=0;imax){ 49 | max = curr; 50 | } 51 | } 52 | 53 | return max; 54 | } 55 | 56 | int rainWaterHarvesting(int arr[],int n){ 57 | 58 | int left[n]; 59 | int right[n]; 60 | 61 | left[0] = arr[0]; 62 | right[n-1] = arr[n-1]; 63 | 64 | for(int i=1;ileft[i-1]){ 66 | left[i] = arr[i]; 67 | }else{ 68 | left[i] = left[i-1]; 69 | } 70 | } 71 | 72 | for(int i=n-2;i>=0;i--){ 73 | if(arr[i]>right[i+1]){ 74 | right[i] = arr[i]; 75 | }else{ 76 | right[i] = right[i+1]; 77 | } 78 | } 79 | 80 | int sum = 0; 81 | 82 | for(int i=0;i arr[i]){ 100 | currMax+=arr[i]; 101 | }else{ 102 | currMax = arr[i]; 103 | } 104 | 105 | totalMax = max(totalMax,currMax); 106 | 107 | if(currMin + arr[i] < arr[i]){ 108 | currMin+=arr[i]; 109 | }else{ 110 | currMin = arr[i]; 111 | } 112 | 113 | totalMin = min(currMin,totalMin); 114 | 115 | sum += arr[i]; 116 | } 117 | 118 | if(sum==totalMin){ 119 | return totalMax; 120 | } 121 | 122 | return max(sum-totalMin,totalMax); 123 | 124 | } 125 | 126 | int myCompare(int a,int b){ 127 | 128 | // if(a+b < 73645){ 129 | // return 1; 130 | // }else{return 0;} 131 | 132 | if(aba){ 145 | return 1; 146 | }else{ 147 | return 0; 148 | } 149 | 150 | } 151 | 152 | void biggestNumber(string arr[],int n){ 153 | 154 | // int brr[] = {5,4,3,2,1}; 155 | // sort(brr,brr+4,myCompare); 156 | 157 | // for(int i=0;i<5;i++){ 158 | // cout< 2 | 3 | using namespace std; 4 | 5 | int sumTilln(int n){ 6 | if(n==1){ 7 | return 1; 8 | } 9 | 10 | int sumtilln1 = sumTilln(n-1); 11 | int total = sumtilln1*n; 12 | return total; 13 | } 14 | 15 | void printID(int n,int temp){ 16 | if(temp==n+1){ 17 | return; 18 | } 19 | 20 | cout<brr[j]){ 147 | c[k] = brr[j]; 148 | k++; 149 | j++; 150 | }else{ 151 | c[k] = arr[i]; 152 | k++; 153 | i++; 154 | } 155 | } 156 | 157 | while(i 2 | 3 | using namespace std; 4 | 5 | template 6 | class node{ 7 | public: 8 | string key; 9 | T value; 10 | node* next; 11 | 12 | node(string key,T value){ 13 | this->key = key; 14 | this->value = value; 15 | next = NULL; 16 | } 17 | }; 18 | 19 | template 20 | class HashTable{ 21 | 22 | int maxSize; 23 | int currSize; 24 | node** bucket; 25 | public: 26 | HashTable(int default_size = 4){ 27 | maxSize = default_size; 28 | currSize = 0; 29 | bucket = new node*[maxSize]; 30 | } 31 | 32 | int hashFunction(string key){ 33 | int len = key.length(); 34 | int mulFactor = 1; 35 | 36 | int idx = 0; 37 | 38 | for(int i=0;imaxSize) + ((key[i]%this->maxSize)*(mulFactor%this->maxSize))%this->maxSize; 40 | idx = idx%this->maxSize; 41 | mulFactor =((mulFactor%this->maxSize)*(37%this->maxSize))%this->maxSize; 42 | } 43 | idx = idx%this->maxSize; 44 | return idx; 45 | } 46 | 47 | void rehash() { 48 | int oldSize = this->maxSize; 49 | node** temp = bucket; 50 | bucket = new node*[2*this->maxSize]; 51 | this->maxSize = 2*this->maxSize; 52 | for(int i = 0; i < this->maxSize; i++) { 53 | bucket[i] = NULL; 54 | } 55 | for(int i = 0; i < oldSize; i++) { 56 | node* curr_bucket = temp[i]; 57 | while(curr_bucket!=NULL) { 58 | insert(curr_bucket->key, curr_bucket->value); 59 | curr_bucket = curr_bucket->next; 60 | } 61 | } 62 | } 63 | 64 | void insert(string key,T val){ 65 | double lambda = (double)this->currSize / this->maxSize; 66 | if (lambda > 0.5) { 67 | rehash(); 68 | } 69 | int idx = hashFunction(key); 70 | node* n = new node(key,val); 71 | n->next = bucket[idx]; 72 | bucket[idx] = n; 73 | currSize++; 74 | 75 | } 76 | 77 | void display(){ 78 | for(int i=0;i* head = this->bucket[i]; 81 | 82 | while(head!=NULL){ 83 | cout<key<<" "<value<<", "; 84 | head=head->next; 85 | } 86 | 87 | cout<* temp = bucket[idx]; 96 | 97 | while(temp!=NULL){ 98 | if(key==temp->key){ 99 | return &(temp->value); 100 | } 101 | temp = temp->next; 102 | } 103 | 104 | return NULL; 105 | 106 | } 107 | 108 | void erase(string k){ 109 | 110 | int idx = hashFunction(k); 111 | 112 | node* temp = bucket[idx]; 113 | 114 | // cout<<(*temp)<key == k){ 123 | cout<<"If Check1"<next; 125 | temp->next = NULL; 126 | delete temp; 127 | return; 128 | } 129 | 130 | cout<<"Check2"<next==NULL){ 133 | return; 134 | } 135 | 136 | cout<<"Check3"<next->key!=k){ 139 | temp=temp->next; 140 | 141 | if(temp->next==NULL){ 142 | return; 143 | } 144 | } 145 | 146 | cout<<"Check4"<* toBeDeleted = temp->next; 149 | temp->next = temp->next->next; 150 | 151 | toBeDeleted->next = NULL; 152 | delete toBeDeleted; 153 | return; 154 | } 155 | }; 156 | 157 | int main(){ 158 | 159 | HashTable h; 160 | 161 | h.insert("India",140); 162 | h.insert("US",23); 163 | h.insert("Norwaskfhew",2); 164 | h.insert("Pakistan",112); 165 | h.insert("New Country",0); 166 | h.insert("a1", 0); 167 | h.insert("a2", 0); 168 | h.insert("a3", 0); 169 | h.insert("a4", 0); 170 | h.insert("a5", 0); 171 | h.insert("a6", 0); 172 | h.insert("a7", 0); 173 | h.insert("a8", 0); 174 | h.insert("a9", 0); 175 | h.insert("a10", 0); 176 | h.insert("a11", 0); 177 | 178 | // h.display(); 179 | 180 | // int* temp = h.search("India"); 181 | // cout<<(*temp)< 2 | 3 | using namespace std; 4 | 5 | bool isSafe(int board[][4],int row,int col,int n){ 6 | 7 | for(int i=0;i=0 and y>=0){ 17 | if(board[x][y]){ 18 | return false; 19 | } 20 | x--; 21 | y--; 22 | } 23 | 24 | x = row; 25 | y = col; 26 | 27 | while(x>=0 and yendrow or col>endcol){ 156 | return false; 157 | } 158 | 159 | if(maze[row][col]=='X'){ 160 | return false; 161 | } 162 | 163 | sol[row][col] = 1; 164 | 165 | bool horizontalWay = ratInMaze(maze,sol,row,col+1,endrow,endcol); 166 | bool verticalWay = ratInMaze(maze,sol,row+1,col,endrow,endcol); 167 | 168 | sol[row][col] = 0; 169 | 170 | if(horizontalWay or verticalWay){ 171 | return true; 172 | } 173 | 174 | return false; 175 | 176 | } 177 | 178 | bool canWePlace(int mat[][9],int row,int col,int n,int number){ 179 | 180 | for(int i=0;i>m>>n; 255 | 256 | // int arr[m][n]; 257 | 258 | // check(arr,m,n); 259 | 260 | // char maze[][4] = { 261 | // {'0','0','0','0'}, 262 | // {'0','0','0','0'}, 263 | // {'0','0','X','0'}, 264 | // {'0','X','0','0'}, 265 | // }; 266 | 267 | // int sol[4][4] = {0}; 268 | 269 | // cout< 2 | 3 | using namespace std; 4 | 5 | class node{ 6 | public: 7 | int data; 8 | node* next; 9 | 10 | node(int data){ 11 | this->data = data; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | void insertAtHead(node*&head,int data){ 17 | 18 | node* n = new node(data); 19 | n->next = head; 20 | head = n; 21 | } 22 | 23 | void display(node*&head){ 24 | 25 | node* temp = head; 26 | 27 | while(temp!=NULL){ 28 | cout<data<<"->"; 29 | temp = temp->next; 30 | } 31 | cout<next!=NULL){ 43 | tail = tail->next; 44 | } 45 | 46 | node* n = new node(data); 47 | 48 | tail->next = n; 49 | } 50 | 51 | int length(node*head){ 52 | 53 | int len = 0; 54 | node* temp = head; 55 | 56 | while(temp!=NULL){ 57 | temp = temp->next; 58 | len++; 59 | } 60 | 61 | return len; 62 | } 63 | 64 | bool searchIterative(node*head,int item){ 65 | node* temp = head; 66 | 67 | while(temp!=NULL){ 68 | if(temp->data == item){ 69 | return true; 70 | } 71 | 72 | temp = temp->next; 73 | } 74 | 75 | return false; 76 | } 77 | 78 | void insertAtIndex(node*&head,int data,int idx){ 79 | if(idx==0 || head==NULL){ 80 | insertAtHead(head,data); 81 | return; 82 | } 83 | 84 | if(idx>length(head)){ 85 | insertAtTail(head,data); 86 | } 87 | 88 | node* temp = head; 89 | int counter = 0; 90 | 91 | while(counternext; 93 | counter++; 94 | } 95 | 96 | node* n = new node(data); 97 | 98 | n->next = temp->next; 99 | temp->next = n; 100 | } 101 | 102 | // void deleteAtTail(node*head){ 103 | // node* temp = head; 104 | 105 | // int counter = 1; 106 | // int len = length(head); 107 | // cout<next; 110 | // counter++; 111 | // } 112 | 113 | // node* p = temp->next; 114 | // temp->next = NULL; 115 | // delete p; 116 | // } 117 | 118 | void deleteAtHead(node*&head){ 119 | if(head==NULL){ 120 | return; 121 | } 122 | 123 | node* temp = head; 124 | head = head->next; 125 | temp->next = NULL; 126 | 127 | delete temp; 128 | } 129 | 130 | void deleteAtTail(node*&head){ 131 | if(head==NULL){ 132 | return; 133 | } 134 | 135 | if(length(head)==1){ 136 | deleteAtHead(head); 137 | } 138 | 139 | node* temp = head; 140 | 141 | while(temp->next->next!=NULL){ 142 | temp = temp->next; 143 | } 144 | 145 | node* p = temp->next; 146 | temp->next = NULL; 147 | delete p; 148 | } 149 | 150 | void deleteAtIndex(node*&head,int idx){ 151 | if(idx==0){ 152 | deleteAtHead(head); 153 | return; 154 | } 155 | 156 | if(idx>=length(head)){ 157 | deleteAtTail(head); 158 | return; 159 | } 160 | 161 | int counter = 0; 162 | 163 | node*temp = head; 164 | 165 | while(counternext; 167 | counter++; 168 | } 169 | 170 | node* p = temp->next; 171 | temp->next = temp->next->next; 172 | 173 | p->next = NULL; 174 | delete p; 175 | } 176 | 177 | bool searchRecursive(node* head,int item){ 178 | if(head==NULL){ 179 | return false; 180 | } 181 | 182 | if(item==head->data){ 183 | return true; 184 | } 185 | 186 | return searchRecursive(head->next,item); 187 | } 188 | 189 | node* midPoint(node* head){ 190 | 191 | node* fast = head; 192 | node* slow = head; 193 | 194 | while(fast->next!=NULL and fast->next->next!=NULL){ 195 | fast = fast->next->next; 196 | slow = slow->next; 197 | } 198 | 199 | return slow; 200 | } 201 | 202 | node* reverseRecursive(node*&head){ 203 | if(head->next==NULL or head==NULL){ 204 | return head; 205 | } 206 | 207 | node* newHead = reverseRecursive(head->next); 208 | node* currentHead = head; 209 | currentHead->next->next = currentHead; 210 | currentHead->next = NULL; 211 | return newHead; 212 | } 213 | 214 | void reverseIterative(node*&head){ 215 | 216 | node* prev = NULL; 217 | node* curr = head; 218 | 219 | while(curr!=NULL){ 220 | 221 | node* n = curr->next; 222 | curr->next = prev; 223 | prev = curr; 224 | curr = n; 225 | } 226 | 227 | head = prev; 228 | } 229 | 230 | node* merge(node* a,node* b){ 231 | if(a==NULL){ 232 | return b; 233 | } 234 | 235 | if(b==NULL){ 236 | return a; 237 | } 238 | 239 | node* c; 240 | 241 | if(a->data < b->data){ 242 | c = a; 243 | c->next = merge(a->next,b); 244 | }else{ 245 | c = b; 246 | c->next = merge(a,b->next); 247 | } 248 | 249 | return c; 250 | } 251 | 252 | 253 | node* mergeSort(node*head){ 254 | if(head==NULL || head->next==NULL){ 255 | return head; 256 | } 257 | 258 | node* mid = midPoint(head); 259 | 260 | node* a = head; 261 | node* b = mid->next; 262 | mid->next = NULL; 263 | 264 | a = mergeSort(a); 265 | b = mergeSort(b); 266 | 267 | node* c = merge(a,b); 268 | 269 | return c; 270 | } 271 | 272 | int main(){ 273 | 274 | // node* head = NULL; 275 | // insertAtTail(head,0); 276 | // insertAtHead(head,4); 277 | // insertAtHead(head,3); 278 | // insertAtHead(head,2); 279 | // insertAtHead(head,1); 280 | 281 | // insertAtIndex(head,789,3); 282 | 283 | // display(head); 284 | // head = reverseRecursive(head); 285 | // display(head); 286 | // reverseIterative(head); 287 | // display(head); 288 | // deleteAtTail(head); 289 | 290 | // display(head); 291 | 292 | // deleteAtIndex(head,0); 293 | 294 | // display(head); 295 | 296 | // cout<data< 2 | #include 3 | 4 | using namespace std; 5 | 6 | void merge(int arr[],int s,int e){ 7 | 8 | int mid=(s+e)/2; 9 | 10 | int i = s; 11 | int j = mid+1; 12 | 13 | int temp[e-s+1]; 14 | 15 | int k = 0; 16 | 17 | while(i<=mid and j<=e){ 18 | 19 | if(arr[i]>arr[j]){ 20 | temp[k] = arr[j]; 21 | k++; 22 | j++; 23 | }else{ 24 | temp[k] = arr[i]; 25 | k++; 26 | i++; 27 | } 28 | } 29 | 30 | while(i<=mid){ 31 | temp[k] = arr[i]; 32 | k++; 33 | i++; 34 | } 35 | 36 | while(j<=e){ 37 | temp[k] = arr[j]; 38 | k++; 39 | j++; 40 | } 41 | 42 | int p = 0; 43 | for(int x = s; x <= e;x++){ 44 | arr[x] = temp[p]; 45 | p++; 46 | } 47 | 48 | } 49 | 50 | void mergeSort(int arr[],int s,int e){ 51 | if(s>=e){ 52 | return; 53 | } 54 | 55 | int mid = (s+e)/2; 56 | 57 | mergeSort(arr,s,mid); 58 | mergeSort(arr,mid+1,e); 59 | 60 | merge(arr,s,e); 61 | } 62 | 63 | int binarySearch(int arr[],int n,int data){ 64 | 65 | int s = 0; 66 | int e = n-1; 67 | 68 | while(s<=e){ 69 | 70 | // int mid = (s+e)/2; 71 | int mid = s + (e-s)/2; 72 | 73 | if(arr[mid]==data){ 74 | return mid; 75 | 76 | }else if(arr[mid]e){ 138 | return -1; 139 | } 140 | 141 | int mid = (s+e)/2; 142 | 143 | if(arr[mid]==data){ 144 | return mid; 145 | } 146 | 147 | if(arr[mid]>=arr[s]){ 148 | if(data>=arr[s] and data<=arr[mid]){ 149 | return rotatedSearch(arr,s,mid-1,data); 150 | }else{ 151 | return rotatedSearch(arr,mid+1,e,data); 152 | } 153 | }else{ 154 | 155 | if(data>=arr[mid] and data<=arr[e]){ 156 | return rotatedSearch(arr,mid+1,e,data); 157 | }else{ 158 | return rotatedSearch(arr,s,mid-1,data); 159 | } 160 | } 161 | } 162 | 163 | int uniqueNumber(int arr[],int s,int e){ 164 | if(s>e){ 165 | return -1; 166 | } 167 | 168 | int mid = s + (e-s)/2; 169 | 170 | if(arr[mid]!=arr[mid-1] and arr[mid]!=arr[mid+1]){ 171 | return mid; 172 | } 173 | 174 | if(arr[mid]==arr[mid+1]){ 175 | 176 | int dist = mid - s; 177 | 178 | if(dist&1){ 179 | return uniqueNumber(arr,s,mid-1); 180 | }else{ 181 | return uniqueNumber(arr,mid+2,e); 182 | } 183 | } 184 | 185 | if(arr[mid]==arr[mid-1]){ 186 | int dist = e - mid; 187 | 188 | if(dist&1){ 189 | return uniqueNumber(arr,mid+1,e); 190 | }else{ 191 | return uniqueNumber(arr,s,mid-2); 192 | } 193 | } 194 | } 195 | 196 | bool isValid(int arr[],int n,int k,int mid){ 197 | 198 | int painters = 1; 199 | int paintingTime = 0; 200 | 201 | for(int i=0;imid){ 206 | painters++; 207 | if(painters>k){ 208 | return false; 209 | } 210 | paintingTime = arr[i]; 211 | } 212 | } 213 | 214 | return true; 215 | } 216 | 217 | int painterProblem(int arr[],int n,int k,int time=1){ 218 | 219 | // int minTime = max(arr) 220 | int minTime = 40; 221 | 222 | // int maxTime = sum(arr) 223 | int maxTime = 100; 224 | 225 | int ans = maxTime; 226 | 227 | while(minTime mid){ 252 | cow++; 253 | alloted = arr[i]; 254 | 255 | if(cow==k){ 256 | return true; 257 | } 258 | } 259 | } 260 | 261 | return false; 262 | } 263 | 264 | int aggressiveCows(int arr[],int n,int k){ 265 | 266 | int minDist = arr[0]; 267 | int maxDist = arr[n-1] - arr[0]; 268 | 269 | int ans = maxDist; 270 | 271 | while(minDist <= maxDist){ 272 | 273 | int mid = minDist + (maxDist-minDist)/2; 274 | 275 | if(isPossible(arr,n,k,mid)){ 276 | 277 | }else{ 278 | 279 | } 280 | 281 | } 282 | 283 | return ans; 284 | } 285 | 286 | void quickSort(int arr[],int low,int high){ 287 | if(low>=high){ 288 | return; 289 | } 290 | 291 | int left = low; 292 | int right = high; 293 | 294 | int mid = (low+high)/2; 295 | int pivot = arr[mid]; 296 | 297 | while(left<=right){ 298 | 299 | while(arr[left]< pivot){ 300 | left++; 301 | } 302 | 303 | while(arr[right] > pivot){ 304 | right--; 305 | } 306 | 307 | if(left<=right){ 308 | swap(arr[left],arr[right]); 309 | left++; 310 | right--; 311 | } 312 | } 313 | 314 | quickSort(arr,low,right); 315 | quickSort(arr,left,high); 316 | 317 | } 318 | 319 | int main(){ 320 | 321 | // int arr[] = {5,4,3,2,1,6}; 322 | // mergeSort(arr,0,5); 323 | 324 | // for(int i=0;i<6;i++){ 325 | // cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | template 10 | class Graph{ 11 | 12 | unordered_map > AdjList; 13 | public: 14 | Graph(){} 15 | 16 | void addEdge(T u,T v,bool birdir = true){ 17 | 18 | AdjList[u].push_back(v); 19 | 20 | if(birdir){ 21 | AdjList[v].push_back(u); 22 | } 23 | } 24 | 25 | void display(){ 26 | for(auto node:AdjList){ 27 | cout< "; 28 | 29 | for(T vertex:node.second){ 30 | cout< q; 38 | unordered_map visited; 39 | 40 | q.push(src); 41 | visited[src] = true; 42 | 43 | while(!q.empty()){ 44 | 45 | T node = q.front(); 46 | q.pop(); 47 | 48 | cout< &visited){ 63 | 64 | cout< visited; 78 | // visited[src] = true; 79 | 80 | int component = 0; 81 | 82 | for(auto node:AdjList){ 83 | if(!visited[node.first]){ 84 | dfs_Helper(node.first,visited); 85 | cout< &visited,list &order){ 94 | 95 | visited[node] = true; 96 | 97 | for(T neighbor:AdjList[node]){ 98 | if(!visited[neighbor]){ 99 | dfsTopologicalSortHelper(neighbor,visited,order); 100 | } 101 | } 102 | 103 | order.push_front(node); 104 | } 105 | 106 | void dfsTopologicalSort(T src){ 107 | unordered_map visited; 108 | list order; 109 | 110 | dfsTopologicalSortHelper(src,visited,order); 111 | 112 | for(auto node:AdjList){ 113 | if(!visited[node.first]){ 114 | dfsTopologicalSortHelper(node.first,visited,order); 115 | } 116 | } 117 | 118 | for(T node:order){ 119 | cout< &visited,int &countrySize){ 125 | countrySize++; 126 | visited[node] = true; 127 | 128 | for(T neighbor:AdjList[node]){ 129 | if(!visited[neighbor]){ 130 | journeyToMoonHelper(neighbor,visited,countrySize); 131 | } 132 | } 133 | } 134 | 135 | int journeyToMoon(){ 136 | 137 | unordered_map visited; 138 | 139 | int n = AdjList.size(); 140 | 141 | int total = n*(n-1)/2; 142 | 143 | for(auto node:AdjList){ 144 | if(!visited[node.first]){ 145 | int countrySize = 0; 146 | journeyToMoonHelper(node.first,visited,countrySize); 147 | total -= countrySize*(countrySize-1)/2; 148 | } 149 | } 150 | 151 | return total; 152 | } 153 | 154 | void bfsTopologicalSort(){ 155 | 156 | queue q; 157 | unordered_map indegree; 158 | 159 | for(auto node:AdjList){ 160 | indegree[node.first] = 0; 161 | } 162 | 163 | for(auto node:AdjList){ 164 | for(T vertex:node.second){ 165 | indegree[vertex]++; 166 | } 167 | } 168 | 169 | for(auto node:AdjList){ 170 | if(indegree[node.first]==0){ 171 | q.push(node.first); 172 | } 173 | } 174 | 175 | while(!q.empty()){ 176 | 177 | T node = q.front(); 178 | q.pop(); 179 | 180 | cout< q; 197 | unordered_map visited; 198 | unordered_map parent; 199 | 200 | 201 | parent[src] = src; 202 | q.push(src); 203 | visited[src] = true; 204 | 205 | while(!q.empty()){ 206 | 207 | T node = q.front(); 208 | q.pop(); 209 | 210 | for(T neighbor:AdjList[node]){ 211 | 212 | if(visited[neighbor] and parent[node]!=neighbor){ 213 | return true; 214 | 215 | }else if(!visited[neighbor]){ 216 | q.push(neighbor); 217 | visited[neighbor] = true; 218 | parent[neighbor] = node; 219 | } 220 | 221 | } 222 | } 223 | 224 | return false; 225 | } 226 | 227 | void bfsShortestPath(T src){ 228 | 229 | queue q; 230 | unordered_map dist; 231 | 232 | for(auto node:AdjList){ 233 | dist[node.first] = INT_MAX; 234 | } 235 | 236 | dist[src] = 0; 237 | q.push(src); 238 | 239 | while(!q.empty()){ 240 | 241 | T node = q.front(); 242 | q.pop(); 243 | 244 | for(T neighbor:AdjList[node]){ 245 | 246 | if(dist[neighbor]==INT_MAX){ 247 | dist[neighbor] = dist[node] + 1; 248 | q.push(neighbor); 249 | } 250 | 251 | } 252 | } 253 | 254 | for(auto node:dist){ 255 | cout<<"Node "< q; 262 | unordered_map dist; 263 | 264 | for(auto node:AdjList){ 265 | dist[node.first] = INT_MAX; 266 | } 267 | 268 | dist[src] = 0; 269 | q.push(src); 270 | 271 | while(!q.empty()){ 272 | 273 | T node = q.front(); 274 | q.pop(); 275 | 276 | for(T neighbor:AdjList[node]){ 277 | 278 | if(dist[neighbor]==INT_MAX){ 279 | dist[neighbor] = dist[node] + 1; 280 | q.push(neighbor); 281 | } 282 | 283 | } 284 | } 285 | 286 | return dist[dest]; 287 | } 288 | 289 | }; 290 | 291 | int main(){ 292 | 293 | // Graph g; 294 | 295 | // g.addEdge(1,2); 296 | // g.addEdge(1,3); 297 | // g.addEdge(3,2); 298 | // // g.addEdge(3,2); 299 | 300 | // g.addEdge(4,5); 301 | // g.addEdge(6,5); 302 | // g.addEdge(7,5); 303 | 304 | 305 | // g.display(); 306 | 307 | // // g.bfs(1); 308 | // g.dfs(1); 309 | 310 | // Graph g; 311 | 312 | // g.addEdge("Coding Blocks","C++"); 313 | // g.addEdge("C++","Java"); 314 | // g.addEdge("Java","Python"); 315 | // g.addEdge("Python","Coding Blocks"); 316 | 317 | // g.display(); 318 | 319 | // Graph g; 320 | 321 | // g.addEdge("Maths","Programming",false); 322 | // g.addEdge("English","Programming",false); 323 | // g.addEdge("Programming","Python",false); 324 | // g.addEdge("Programming","Java",false); 325 | // g.addEdge("Java","Web",false); 326 | // g.addEdge("Python","Web",false); 327 | 328 | // // g.dfsTopologicalSort("Maths"); 329 | // g.bfsTopologicalSort(); 330 | 331 | // Graph g; 332 | 333 | // g.addEdge(1,2); 334 | // g.addEdge(1,3); 335 | // g.addEdge(1,4); 336 | // g.addEdge(5,6); 337 | // g.addEdge(6,7); 338 | 339 | // cout< g; 342 | 343 | // g.addEdge(1,2); 344 | // g.addEdge(1,3); 345 | // // g.addEdge(2,3); 346 | // g.addEdge(3,4); 347 | // g.addEdge(4,5); 348 | 349 | // cout< g; 352 | 353 | // g.addEdge(1,2); 354 | // g.addEdge(1,3); 355 | // g.addEdge(2,4); 356 | // g.addEdge(3,5); 357 | // g.addEdge(3,4); 358 | // g.addEdge(5,6); 359 | // g.addEdge(4,5); 360 | 361 | // g.bfsShortestPath(1); 362 | 363 | Graph g; 364 | 365 | int board[50] = {0}; 366 | 367 | board[2] = 13; 368 | board[5] = 2; 369 | board[9] = 18; 370 | board[18] = 11; 371 | board[17] = -13; 372 | board[20] = -14; 373 | board[24] = -8; 374 | board[25] = -10; 375 | board[32] = -2; 376 | board[34] = -22; 377 | 378 | for(int i=0;i<=36;i++){ 379 | for(int dice = 1;dice<=6;dice++){ 380 | 381 | int u = i; 382 | int v = u + dice + board[u+dice]; 383 | g.addEdge(u,v,false); 384 | } 385 | } 386 | 387 | cout< 2 | #include 3 | #include 4 | #include 5 | 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 | node* buildTree(node* root){ 22 | int data; 23 | cin>>data; 24 | 25 | if(data ==-1){ 26 | return NULL; 27 | } 28 | 29 | if(root==NULL){ 30 | node* n = new node(data); 31 | root=n; 32 | } 33 | 34 | root->left = buildTree(root->left); 35 | root->right = buildTree(root->right); 36 | 37 | return root; 38 | } 39 | 40 | void preorder(node*root){ 41 | if(root==NULL){ 42 | return; 43 | } 44 | 45 | cout<data<<" "; 46 | 47 | preorder(root->left); 48 | preorder(root->right); 49 | } 50 | 51 | void postorder(node*root){ 52 | if(root==NULL){ 53 | return; 54 | } 55 | 56 | postorder(root->left); 57 | postorder(root->right); 58 | 59 | cout<data<<" "; 60 | } 61 | 62 | void inorder(node*root){ 63 | if(root==NULL){ 64 | return; 65 | } 66 | 67 | inorder(root->left); 68 | 69 | cout<data<<" "; 70 | 71 | inorder(root->right); 72 | } 73 | 74 | int countNumberOfNodes(node*root){ 75 | if(root==NULL){ 76 | return 0; 77 | } 78 | 79 | int leftCount = countNumberOfNodes(root->left); 80 | int rightCount = countNumberOfNodes(root->right); 81 | 82 | int totalCount = leftCount + rightCount + 1; 83 | return totalCount; 84 | 85 | // return countNumberOfNodes(root->left) + countNumberOfNodes(root->right) + 1; 86 | } 87 | 88 | int sumOfNodes(node*root){ 89 | if(root==NULL){ 90 | return 0; 91 | } 92 | 93 | int leftSum = sumOfNodes(root->left); 94 | int rightSum = sumOfNodes(root->right); 95 | 96 | int totalSum = leftSum + rightSum + root->data; 97 | return totalSum; 98 | } 99 | 100 | int height(node*root){ 101 | //Base Case 102 | if(root==NULL){ 103 | return -1; 104 | } 105 | 106 | //Recursive Case 107 | int leftHeight = height(root->left); 108 | int rightHeight = height(root->right); 109 | 110 | int totalHeight = max(leftHeight,rightHeight) + 1; 111 | return totalHeight; 112 | } 113 | 114 | bool searchNode(node*root,int item){ 115 | //Base Case 116 | if(root==NULL){ 117 | return false; 118 | } 119 | 120 | // Recursive Case 121 | if(item==root->data){ 122 | return true; 123 | } 124 | 125 | bool leftSearch = searchNode(root->left,item); 126 | bool rightSearch = searchNode(root->right,item); 127 | 128 | if(leftSearch or rightSearch){ 129 | return true; 130 | } 131 | 132 | return false; 133 | } 134 | 135 | void printAllRootToLeaves(node*root,string path){ 136 | if(root==NULL){ 137 | return; 138 | } 139 | 140 | char data = root->data + '0'; 141 | 142 | if(root->left==NULL and root->right==NULL){ 143 | path = path + data; 144 | cout<left,path+data); 149 | printAllRootToLeaves(root->right,path+data); 150 | } 151 | 152 | int diameter(node*root){ 153 | if(root==NULL){ 154 | return 0; 155 | } 156 | 157 | int leftDiameter = diameter(root->left); 158 | int rightDiameter = diameter(root->right); 159 | 160 | int leftHeight = height(root->left); 161 | int rightHeight = height(root->right); 162 | 163 | int totalDia = leftHeight + rightHeight + 2; 164 | 165 | return max(totalDia,max(leftDiameter,rightDiameter)); 166 | } 167 | 168 | class DiaHeightPair{ 169 | public: 170 | int diameter; 171 | int height; 172 | }; 173 | 174 | DiaHeightPair diameterBetter(node*root){ 175 | DiaHeightPair val; 176 | 177 | if(root==NULL){ 178 | val.height = -1; 179 | val.diameter = 0; 180 | return val; 181 | } 182 | 183 | DiaHeightPair leftPair = diameterBetter(root->left); 184 | DiaHeightPair rightPair = diameterBetter(root->right); 185 | 186 | val.height = max(leftPair.height,rightPair.height) + 1; 187 | 188 | val.diameter = max(leftPair.height + rightPair.height + 2,max(leftPair.diameter,rightPair.diameter)); 189 | 190 | return val; 191 | } 192 | 193 | bool isBalanced(node*root){ 194 | if(root==NULL){ 195 | return true; 196 | } 197 | 198 | bool leftBalanced = isBalanced(root->left); 199 | bool rightBalanced = isBalanced(root->right); 200 | 201 | if(!leftBalanced or !rightBalanced){ 202 | return false; 203 | } 204 | 205 | int leftHeight = height(root->left); 206 | int rightHeight = height(root->right); 207 | 208 | int diff = abs(leftHeight - rightHeight); 209 | 210 | if(diff>1){ 211 | return false; 212 | }else{ 213 | return true; 214 | } 215 | } 216 | 217 | int replaceWithChildrenSum(node*root){ 218 | //Base Case 219 | if(root==NULL){ 220 | return 0; 221 | } 222 | 223 | if(root->left==NULL and root->right==NULL){ 224 | return root->data; 225 | } 226 | 227 | //Recursive Case 228 | int leftSum = replaceWithChildrenSum(root->left); 229 | int rightSum = replaceWithChildrenSum(root->right); 230 | 231 | int temp = root->data; 232 | root->data = leftSum + rightSum; 233 | return temp + root->data; 234 | } 235 | 236 | void levelOrder(node*root){ 237 | 238 | queue q; 239 | 240 | q.push(root); 241 | 242 | while(!q.empty()){ 243 | 244 | node* temp = q.front(); 245 | q.pop(); 246 | 247 | cout<data<<" "; 248 | 249 | if(temp->left!=NULL){ 250 | q.push(temp->left); 251 | } 252 | 253 | if(temp->right!=NULL){ 254 | q.push(temp->right); 255 | } 256 | 257 | } 258 | 259 | cout<left); 278 | HeightBalancedPair rightPair = isBalancedOptimized(root->right); 279 | 280 | if(!leftPair.balance or !rightPair.balance){ 281 | val.balance = false; 282 | } 283 | 284 | int diff = abs(leftPair.height - rightPair.height); 285 | 286 | val.height = max(leftPair.height,rightPair.height) + 1; 287 | 288 | if(diff>1){ 289 | val.balance = false; 290 | }else{ 291 | val.balance = true; 292 | } 293 | 294 | return val; 295 | } 296 | 297 | void serialize(node* root){ 298 | if(root==NULL){ 299 | cout<<(-1)<<" "; 300 | return; 301 | } 302 | 303 | cout<data<<" "; 304 | serialize(root->left); 305 | serialize(root->right); 306 | } 307 | 308 | node* lca(node*root,int data1,int data2){ 309 | if(root==NULL){ 310 | return NULL; 311 | } 312 | 313 | if(root->data==data1 or root->data==data2){ 314 | return root; 315 | } 316 | 317 | node* leftLCA = lca(root->left,data1,data2); 318 | node* rightLCA = lca(root->right,data1,data2); 319 | 320 | if(leftLCA==NULL and rightLCA==NULL){ 321 | return NULL; 322 | } 323 | 324 | if(leftLCA!=NULL and rightLCA!=NULL){ 325 | return root; 326 | } 327 | 328 | return leftLCA!=NULL ? leftLCA:rightLCA; 329 | } 330 | 331 | int findLevel(node*root,int k,int level){ 332 | if(root==NULL){ 333 | return -1; 334 | } 335 | 336 | if(root->data==k){ 337 | return level; 338 | } 339 | 340 | int leftDistance = findLevel(root->left,k,level+1); 341 | 342 | if(leftDistance==-1){ 343 | int rightDistance = findLevel(root->right,k,level+1); 344 | return rightDistance; 345 | } 346 | 347 | return leftDistance; 348 | } 349 | 350 | int findDistace(node*root,int a,int b){ 351 | 352 | node* common = lca(root,a,b); 353 | 354 | int d1 = findLevel(common,a,0); 355 | int d2 = findLevel(common,b,0); 356 | 357 | return d1 + d2; 358 | } 359 | 360 | int preorderIteration = 0; 361 | 362 | node* buildTreeFromPreorderInorder(int pre[],int in[],int s,int e){ 363 | if(s>e){ 364 | return NULL; 365 | } 366 | 367 | node* root = new node(pre[preorderIteration]); 368 | 369 | int mid = -1; 370 | 371 | for(int i= s; i<= e;i++){ 372 | if(in[i]==pre[preorderIteration]){ 373 | mid = i; 374 | break; 375 | } 376 | } 377 | 378 | preorderIteration++; 379 | 380 | root->left = buildTreeFromPreorderInorder(pre,in,s,mid-1); 381 | root->right = buildTreeFromPreorderInorder(pre,in,mid+1,e); 382 | 383 | return root; 384 | } 385 | 386 | 387 | int main(){ 388 | 389 | // node* root = NULL; 390 | 391 | // root = buildTree(root); 392 | // preorder(root); 393 | // cout<data< 2 | #include 3 | #include 4 | #include 5 | 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 | node* insertInBST(node*root,int data){ 22 | if(root==NULL){ 23 | node* n = new node(data); 24 | root = n; 25 | return root; 26 | // return new node(data); 27 | } 28 | 29 | if(data>root->data){ 30 | root->right = insertInBST(root->right,data); 31 | }else{ 32 | root->left = insertInBST(root->left,data); 33 | } 34 | 35 | return root; 36 | } 37 | 38 | node* construct(){ 39 | node* root = NULL; 40 | 41 | int data; 42 | cin>>data; 43 | 44 | while(data!=-1){ 45 | root = insertInBST(root,data); 46 | cin>>data; 47 | } 48 | 49 | return root; 50 | } 51 | 52 | node* buildTree(node* root){ 53 | int data; 54 | cin>>data; 55 | 56 | if(data ==-1){ 57 | return NULL; 58 | } 59 | 60 | if(root==NULL){ 61 | node* n = new node(data); 62 | root=n; 63 | } 64 | 65 | root->left = buildTree(root->left); 66 | root->right = buildTree(root->right); 67 | 68 | return root; 69 | } 70 | 71 | void preorder(node*root){ 72 | if(root==NULL){ 73 | return; 74 | } 75 | 76 | cout<data<<" "; 77 | 78 | preorder(root->left); 79 | preorder(root->right); 80 | } 81 | 82 | void inorder(node*root){ 83 | if(root==NULL){ 84 | return; 85 | } 86 | 87 | inorder(root->left); 88 | 89 | cout<data<<" "; 90 | 91 | inorder(root->right); 92 | } 93 | 94 | bool search(node*root,int item){ 95 | if(root==NULL){ 96 | return false; 97 | } 98 | 99 | if(root->data==item){ 100 | return true; 101 | } 102 | 103 | if(root->data > item){ 104 | return search(root->left,item); 105 | }else{ 106 | return search(root->right,item); 107 | } 108 | } 109 | 110 | node* buildTreeFromArray(int arr[],int s,int e){ 111 | if(s>e){ 112 | return NULL; 113 | } 114 | 115 | int mid = (s+e)/2; 116 | node* root = new node(arr[mid]); 117 | 118 | root->left = buildTreeFromArray(arr,s,mid-1); 119 | root->right = buildTreeFromArray(arr,mid+1,e); 120 | 121 | return root; 122 | } 123 | 124 | bool isBST(node*root,int leftRange = INT_MIN,int rightRange = INT_MAX){ 125 | if(root==NULL){ 126 | return true; 127 | } 128 | 129 | bool leftCheck = isBST(root->left,leftRange,root->data); 130 | bool rightCheck = isBST(root->right,root->data,rightRange); 131 | 132 | if(leftCheck and rightCheck and root->data < rightRange and root->data > leftRange){ 133 | return true; 134 | }else{ 135 | return false; 136 | } 137 | 138 | } 139 | 140 | int numberOfBST(int n){ 141 | if(n==0){ 142 | return 1; 143 | } 144 | 145 | int ans = 0; 146 | 147 | for(int i=1;i<=n;i++){ 148 | ans += numberOfBST(i-1)*numberOfBST(n-i); 149 | } 150 | 151 | return ans; 152 | } 153 | 154 | void levelOrderNewLine(node*root){ 155 | 156 | queue q; 157 | 158 | q.push(root); 159 | q.push(NULL); 160 | 161 | while(!q.empty()){ 162 | 163 | node* temp = q.front(); 164 | q.pop(); 165 | 166 | if(temp==NULL){ 167 | cout<data<<" "; 177 | 178 | if(temp->left!=NULL){ 179 | q.push(temp->left); 180 | } 181 | 182 | if(temp->right!=NULL){ 183 | q.push(temp->right); 184 | } 185 | 186 | } 187 | 188 | cout<data<<" "; 200 | firstVisitLevel++; 201 | } 202 | 203 | leftView(root->left,level+1); 204 | leftView(root->right,level+1); 205 | } 206 | 207 | void leftBoundary(node*root){ 208 | if(root==NULL){ 209 | return; 210 | } 211 | 212 | if(root->left==NULL and root->right==NULL){ 213 | return; 214 | } 215 | 216 | cout<data<<" "; 217 | 218 | if(root->left!=NULL){ 219 | leftBoundary(root->left); 220 | }else{ 221 | leftBoundary(root->right); 222 | } 223 | } 224 | 225 | void printLeaf(node*root){ 226 | if(root==NULL){ 227 | return; 228 | } 229 | 230 | if(root->left==NULL and root->right==NULL){ 231 | cout<data<<" "; 232 | return; 233 | } 234 | 235 | printLeaf(root->left); 236 | printLeaf(root->right); 237 | } 238 | 239 | int maxPath = INT_MIN; 240 | 241 | int maxPathNodes(node*root){ 242 | if(root==NULL){ 243 | return 0; 244 | } 245 | 246 | int leftMax = max(0,maxPathNodes(root->left)); 247 | int rightMax = max(0,maxPathNodes(root->right)); 248 | 249 | maxPath = max(maxPath,leftMax + rightMax + root->data); 250 | 251 | return max(leftMax,rightMax) + root->data; 252 | } 253 | 254 | class LinkedListPair{ 255 | public: 256 | node*head; 257 | node*tail; 258 | }; 259 | 260 | LinkedListPair treeToLinkedList(node*root){ 261 | LinkedListPair val; 262 | 263 | if(root==NULL){ 264 | val.head = NULL; 265 | val.tail = NULL; 266 | return val; 267 | } 268 | 269 | if(root->left==NULL and root->right==NULL){ 270 | val.head = root; 271 | val.tail = root; 272 | return val; 273 | } 274 | 275 | if(root->left!=NULL and root->right==NULL){ 276 | LinkedListPair leftPair = treeToLinkedList(root->left); 277 | 278 | leftPair.tail->right = root; 279 | val.tail = root; 280 | val.head = leftPair.head; 281 | return val; 282 | } 283 | 284 | if(root->left==NULL and root->right!=NULL){ 285 | 286 | LinkedListPair rightPair = treeToLinkedList(root->right); 287 | 288 | root->right = rightPair.head; 289 | val.head = root; 290 | val.tail = rightPair.tail; 291 | return val; 292 | } 293 | 294 | if(root->left!=NULL and root->right!=NULL){ 295 | LinkedListPair leftPair = treeToLinkedList(root->left); 296 | LinkedListPair rightPair = treeToLinkedList(root->right); 297 | 298 | leftPair.tail->right = root; 299 | root->right = rightPair.head; 300 | 301 | val.head = leftPair.head; 302 | val.tail = rightPair.tail; 303 | return val; 304 | } 305 | } 306 | 307 | // class TreeDetail{ 308 | // public: 309 | // int size; 310 | // bool bst; 311 | // int min; 312 | // int max; 313 | 314 | // TreeDetail(){ 315 | // size = 0; 316 | // bst = true; 317 | // min = INT_MAX; 318 | // max = INT_MIN; 319 | // } 320 | // }; 321 | 322 | // TreeDetail largestBSTinBinaryTree(node*root){ 323 | // TreeDetail val; 324 | // cout<data<<" "; 325 | // if(root==NULL){ 326 | // return val; 327 | // } 328 | 329 | // TreeDetail leftDetail = largestBSTinBinaryTree(root->left); 330 | // TreeDetail rightDetail = largestBSTinBinaryTree(root->right); 331 | 332 | // if(leftDetail.bst==false or rightDetail.bst==false or root->data < leftDetail.max or root->data > rightDetail.min){ 333 | // val.bst = false; 334 | // val.size = max(leftDetail.size,rightDetail.size); 335 | // return val; 336 | // } 337 | 338 | // val.bst = true; 339 | 340 | // val.size = leftDetail.size + rightDetail.size + 1; 341 | 342 | // val.min = root->left!=NULL ? leftDetail.min : root->data; 343 | 344 | // val.max = root->right!=NULL ? rightDetail.max : root->data; 345 | // return val; 346 | // } 347 | 348 | class TreeDetail{ 349 | public: 350 | int size; 351 | bool bst; 352 | int min; 353 | int max; 354 | 355 | TreeDetail(){ 356 | size = 0; 357 | bst = true; 358 | min = INT_MAX; 359 | max = INT_MIN; 360 | } 361 | }; 362 | 363 | TreeDetail largestBSTinBinaryTree(node*root){ 364 | TreeDetail val; 365 | 366 | if(root==NULL){ 367 | return val; 368 | } 369 | 370 | TreeDetail leftDetail = largestBSTinBinaryTree(root->left); 371 | TreeDetail rightDetail = largestBSTinBinaryTree(root->right); 372 | 373 | if(leftDetail.bst == false or rightDetail.bst==false or root->data < leftDetail.max or root->data > rightDetail.min){ 374 | val.bst = false; 375 | val.size = max(leftDetail.size,rightDetail.size); 376 | return val; 377 | } 378 | 379 | val.bst = true; 380 | val.size = leftDetail.size + rightDetail.size + 1; 381 | 382 | val.min = root->left!=NULL ? leftDetail.min : root->data; 383 | 384 | val.max = root->right!=NULL ? rightDetail.max : root->data; 385 | 386 | return val; 387 | } 388 | 389 | int main(){ 390 | 391 | // node* root = construct(); 392 | // preorder(root); 393 | // cout<data<<" "; 424 | // head = head->right; 425 | // } 426 | // cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // int fib(int n){ 8 | // if(n==1 or n==0){ 9 | // return n; 10 | // } 11 | 12 | // return fib(n-1) + fib(n-2); 13 | // } 14 | 15 | // int dp[100] = {0,1}; 16 | 17 | // int fibMemo(int n){ 18 | // if(n==1 or n==0){ 19 | // return n; 20 | // } 21 | 22 | // if(dp[n]!=0){ 23 | // return dp[n]; 24 | // } 25 | 26 | // int fibn1 = fibMemo(n-1); 27 | // int fibn2 = fibMemo(n-2); 28 | 29 | // int fibn = fibn1 + fibn2; 30 | 31 | // dp[n] = fibn; 32 | 33 | // // for(int i=0;iend){ 70 | // return 0; 71 | // } 72 | 73 | // int count = 0; 74 | 75 | // for(int dice=1;dice<=6;dice++){ 76 | // count+=countBoardPath(start+dice,end); 77 | // } 78 | 79 | // return count; 80 | // } 81 | 82 | 83 | // int countBoardPathMemo(int start,int end,int BoardDP[]){ 84 | // if(start==end){ 85 | // return 1; 86 | // } 87 | 88 | // if(start>end){ 89 | // return 0; 90 | // } 91 | 92 | // if(BoardDP[start]!=0){ 93 | // return BoardDP[start]; 94 | // } 95 | 96 | // int count = 0; 97 | 98 | // for(int dice=1;dice<=6;dice++){ 99 | // count+=countBoardPathMemo(start+dice,end,BoardDP); 100 | // } 101 | 102 | // BoardDP[start] = count; 103 | 104 | // for(int i=0;i<10;i++){ 105 | // cout<=start;i--){ 118 | // for(int dice = 1;dice<=6;dice++){ 119 | 120 | // if(i+dice>end){ 121 | // break; 122 | // } 123 | 124 | // countBoardDP[i]+=countBoardDP[i+dice]; 125 | // } 126 | // } 127 | 128 | // return countBoardDP[start]; 129 | // } 130 | 131 | // int reduceToOne(int n){ 132 | // if(n==1){ 133 | // return 0; 134 | // } 135 | 136 | // int count1 = INT_MAX; 137 | // int count2 = INT_MAX; 138 | // int count3 = INT_MAX; 139 | 140 | // if(n%3==0){ 141 | // count1 = 1 + reduceToOne(n/3); 142 | // } 143 | 144 | // if(n%2==0){ 145 | // count2 = 1 + reduceToOne(n/2); 146 | // } 147 | 148 | // count3 = 1 + reduceToOne(n-1); 149 | 150 | // return min(count1,min(count2,count3)); 151 | // } 152 | 153 | // int reduceToOneMemo(int n,int reduceDP[]){ 154 | // if(n==1){ 155 | // return 0; 156 | // } 157 | 158 | // if(reduceDP[n]!=0){ 159 | // return reduceDP[n]; 160 | // } 161 | 162 | // int count1 = INT_MAX; 163 | // int count2 = INT_MAX; 164 | // int count3 = INT_MAX; 165 | 166 | // if(n%3==0){ 167 | // count1 = 1 + reduceToOneMemo(n/3,reduceDP); 168 | // } 169 | 170 | // if(n%2==0){ 171 | // count2 = 1 + reduceToOneMemo(n/2,reduceDP); 172 | // } 173 | 174 | // count3 = 1 + reduceToOneMemo(n-1,reduceDP); 175 | 176 | // reduceDP[n] = min(count1,min(count2,count3)); 177 | 178 | // // for(int i=0;i arr[y]){ 269 | // dp[x] = max(dp[x],dp[y] + 1); 270 | // } 271 | 272 | // } 273 | 274 | // maxLength = max(dp[x],maxLength); 275 | // } 276 | 277 | // return maxLength; 278 | // } 279 | 280 | // int longestCommonSubsequence(string s1,string s2){ 281 | // if(s1.length()==0 or s2.length()==0){ 282 | // return 0; 283 | // } 284 | 285 | // char ch1 = s1[0]; 286 | // char ch2 = s2[0]; 287 | 288 | // string ros1 = s1.substr(1); 289 | // string ros2 = s2.substr(1); 290 | 291 | // int result = 0; 292 | 293 | // if(ch1==ch2){ 294 | // result = 1 + longestCommonSubsequence(ros1,ros2); 295 | // }else{ 296 | 297 | // int check1 = longestCommonSubsequence(ros1,s2); 298 | // int check2 = longestCommonSubsequence(s1,ros2); 299 | 300 | // result = max(check2,check1); 301 | // } 302 | 303 | // return result; 304 | // } 305 | 306 | // int dp[10][10]; 307 | 308 | // int longestCommonSubsequenceMemo(char s1[],int i,char s2[],int j){ 309 | // if(s1[i]=='\0' or s2[j]=='\0'){ 310 | // dp[i][j] = 0; 311 | // return 0; 312 | // } 313 | 314 | // if(dp[i][j]!=-1){ 315 | // return dp[i][j]; 316 | // } 317 | 318 | // char ch1 = s1[i]; 319 | // char ch2 = s2[j]; 320 | 321 | // int result = 0; 322 | 323 | // if(ch1==ch2){ 324 | // result = 1 + longestCommonSubsequenceMemo(s1,i+1,s2,j+1); 325 | // }else{ 326 | 327 | // int check1 = longestCommonSubsequenceMemo(s1,i+1,s2,j); 328 | // int check2 = longestCommonSubsequenceMemo(s1,i,s2,j+1); 329 | 330 | // result = max(check2,check1); 331 | // } 332 | 333 | // dp[i][j] = result; 334 | 335 | // for(int i=0;i<=5;i++){ 336 | // for(int j=0;j<=5;j++){ 337 | // if(dp[i][j]==-1){ 338 | // cout<<"* "; 339 | // }else{ 340 | // cout<=0;i--){ 355 | 356 | for(int j = 5;j>=0;j--){ 357 | 358 | if(i==5 or j==5){ 359 | lcsDP[i][j] = 0; 360 | continue; 361 | } 362 | 363 | if(s1[i]==s2[j]){ 364 | lcsDP[i][j] = 1 + lcsDP[i+1][j+1]; 365 | }else{ 366 | 367 | lcsDP[i][j] = max(lcsDP[i][j+1],lcsDP[i+1][j]); 368 | } 369 | 370 | } 371 | 372 | } 373 | 374 | return lcsDP[0][0]; 375 | 376 | } 377 | 378 | int dp[10][10]; 379 | 380 | int editDistance(char s1[],int i,char s2[],int j){ 381 | if(s1[i]=='\0'){ 382 | dp[i][j] = sizeof(s2) - sizeof(s1); 383 | cout<=0;j--){ 441 | editDP[strlen(s1)][j] = check; 442 | check++; 443 | } 444 | 445 | check = 0; 446 | for(int i=strlen(s1);i>=0;i--){ 447 | editDP[i][strlen(s2)] = check; 448 | check++; 449 | } 450 | 451 | for(int i = strlen(s1);i>= 0;i--){ 452 | 453 | for(int j = strlen(s2);j>= 0;j--){ 454 | if(i==strlen(s1) or j==strlen(s2)){ 455 | // editDP[i][j] =abs(abs(i-j) - abs(strlen(s1) - strlen(s2))); 456 | continue; 457 | } 458 | 459 | if(s1[i]==s2[j]){ 460 | editDP[i][j] = editDP[i+1][j+1]; 461 | }else{ 462 | editDP[i][j] = 1 + min(editDP[i+1][j],min(editDP[i][j+1],editDP[i+1][j+1])); 463 | } 464 | } 465 | } 466 | 467 | // for(int i=0;i<9;i++){ 468 | // for(int j=0;j<7;j++){ 469 | // cout<j){ 541 | storageKnapSack[i][j] = storageKnapSack[i-1][j]; 542 | }else{ 543 | storageKnapSack[i][j] = max(storageKnapSack[i-1][j],value[i] + storageKnapSack[i-1][j - weight[i]]); 544 | } 545 | 546 | cout<