├── .gitignore ├── DSA_Topics ├── Disjoint_Set │ └── basic.cpp ├── Divide and Conquer (DnC) │ ├── Maximum-SubArray │ │ ├── GivenCode_Maximum_SubArray_.cpp │ │ ├── Maximum_SubArray_BruteForce_Prefix-Sum.cpp │ │ ├── Maximum_SubArray_DivideAndConquer.cpp │ │ ├── Maximum_Subarray_on_circular_array.cpp │ │ └── Problems │ │ │ ├── Alphabet World.cpp │ │ │ └── MaximumSubArray_of_NonNegativeNumbers.cpp │ ├── Merge Sort │ │ └── MergeSort.cpp │ └── Min-Max Problem │ │ ├── MinMax_Using_Recursion.cpp │ │ ├── MinMax_algo_DnC.cpp │ │ └── Problems │ │ └── Sum_of_Min_Max.cpp ├── Dynamic Programming │ ├── Coin Change │ │ ├── CoinChange_BruteForce.cpp │ │ ├── CoinChange_DP_BottomUp.cpp │ │ ├── CoinChange_DP_TopDown.cpp │ │ └── Problem_in_CN.cpp │ ├── Fibonacci │ │ ├── Fibonacci_Bottom-UP_approach__Tabulation-Method.cpp │ │ ├── Fibonacci_Top-Down_approach__Overlapping_Subproblems__Memorization-Method.cpp │ │ └── Problems │ │ │ └── Tetranacci Number.cpp │ └── ZeroOne Knapsack │ │ ├── zeroOne_Knapsack_Bruteforce.cpp │ │ └── zeroOne_Knapsack_DP.cpp ├── Graph and Graph Algo │ ├── BFS │ │ └── BFS.cpp │ ├── DFS │ │ └── DFS.cpp │ ├── Graph_Algorithoms │ │ ├── Bellman Ford Algo │ │ │ └── Bellman_ford_GivenCode_.cpp │ │ ├── Dijkstra Algo │ │ │ ├── Dijkstra_Algo_UsingPriorityQueue.cpp │ │ │ ├── Dijkstra_Algo_UsingPriorityQueue__STL.cpp │ │ │ ├── Dijkstra_Algo_UsingSet.cpp │ │ │ └── GivenCode.cpp │ │ ├── Kruskal's Algo (MST) │ │ │ ├── KruskalsAlgo__GivenCode.cpp │ │ │ ├── Kruskals_Algo_.cpp │ │ │ └── prims.cpp │ │ └── Problems │ │ │ ├── Kruskal's Minimum Spanning Tree.cpp │ │ │ └── mySolve.cpp │ ├── Learned │ │ ├── BFS_using_adjList_.cpp │ │ ├── DFS.cpp │ │ ├── DFS_using_adjList_.cpp │ │ └── creat_Graph_using_adjList_.cpp │ ├── Problems_BFS-DFS │ │ ├── Backtracking__Rat_in_a_Maze.cpp │ │ ├── Lecture40_RatinaMazeProblem.cpp │ │ └── ShortestPath_using_BFS.cpp │ └── Sorting │ │ └── Topological_Sort │ │ ├── Using_BFS_kahnsAlgo_.cpp │ │ └── Using_DFS_.cpp ├── Greedy Algorithoms │ ├── Activity Selection │ │ ├── Activity_Selection__Maximization.cpp │ │ ├── Activity_Selection__UsingStructure.cpp │ │ └── Problems │ │ │ └── ReturnTicket.cpp │ ├── Coin Change - Minimixation │ │ ├── Coin_Change__BruteForce_Approach.cpp │ │ └── Coin_Change__Greedy_Approach__Mimimization.cpp │ └── Knapsack - Maximization │ │ ├── Fractional_Knapsack_usingStructure.cpp │ │ ├── Knapsack_Fractional.cpp │ │ ├── zeroOne_Knapsack_Greedy_&_Bruteforce__usingStructure.cpp │ │ └── zeroOne_Knapsack__Greedy.cpp └── String Algorithoms │ └── Knuth-Morris-Pratt__KMP_Algo__.cpp ├── LAB_Code ├── Class_1 (SPL Review Solution) │ ├── 00. SPL review.pdf │ ├── Avarage_of_n_NUMBERS_upto_2nd_Decimal.cpp │ ├── Char_Input_Untill_A.cpp │ ├── Number_Frequency_and_Distinct _Number.cpp │ ├── QN_09.cpp │ ├── QN_10.cpp │ ├── converts_the_sentence_into_lowercase_and_removes_all_whitespace_using_Recusion_.cpp │ ├── print_all_the_factorials_of_numbers_within__number_a__number_b___.cpp │ ├── prints_all_the_unique_elements_of_an_array__.cpp │ ├── prints_the_number_of_occurrences_of_each_unique_elements_of_an_array_.cpp │ └── take_as_input_a_string__and_toggle_cases_of_all_the_letters__.cpp ├── Class_2 STL │ └── Vector.cpp ├── Class_3_(Recursion) │ ├── Calculate_Power_of_a_Number_Using_recursion.cpp │ ├── RPS01_Prints_the_numbers_from___to_n__.cpp │ ├── RPS02_Calculate_the_sum_of_numbers_from___to_n___.cpp │ ├── RPS03_Calculate_the_factorial_of_n___.cpp │ ├── RPS04__Calculate_the_sum_of_digits_of_a_given_number_n__.cpp │ ├── RPS05.cpp │ ├── RPS06.cpp │ ├── RPS07.cpp │ ├── RPS08.cpp │ ├── RPS09.cpp │ ├── RPS10.cpp │ ├── RPS11.cpp │ ├── RPS_12.cpp │ ├── Reverse_a_Number_uning_recursion.cpp │ ├── [Template] Recursion Problem List.pdf │ └── converts_the_sentence_into_lowercase_and_removes_all_whitespace_using_Recusion_.cpp ├── Class_5 │ └── return_min_Max.cpp ├── Class_6 │ ├── Coin_Change_Greedy_GivenCode.cpp │ ├── Coin_change_BruteForce.cpp │ └── else.cpp └── Lab Final Fall23 Sec_B │ ├── Q1.cpp │ └── Q2.cpp ├── Lab Question + Assignment Solution ├── ASSG_3__Sec_J │ ├── QSN1_BruteForce.cpp │ └── QSN1_SolvedUsing__KrusKal_Algo.cpp ├── Assignment 01 (Greedy) Solve Sec J │ └── Noman's_ASSG.cpp ├── CT 01_TarekSir_Fall23_Sec_B │ ├── Q01_RecursionProblem.cpp │ └── Q02_StringProblem.cpp ├── CT 02 Tarek Sir Fall23 Sec B - On Greedy and DnC │ ├── Q1.cpp │ └── Q2.cpp └── CT2_Sec_J │ └── Q01.cpp ├── README.md └── Rename_File.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | RemoveexeFile.c 2 | RemoveexeFile.exe 3 | Rename_File.cpp 4 | else.cpp 5 | *.exe 6 | *.vscode 7 | *.cph 8 | *tempCodeRunnerFile.cpp -------------------------------------------------------------------------------- /DSA_Topics/Disjoint_Set/basic.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | /** Disjoint Set - With Heuristic **/ 11 | 12 | int parent[107]; /// To store parents 13 | int rank_[107]; /// store level 14 | 15 | void makeSet(int x) 16 | { 17 | parent[x] = x; 18 | rank_[x] = 0; 19 | } 20 | 21 | int findSet(int x) 22 | { 23 | if (parent[x] == x) 24 | return x; 25 | else 26 | { 27 | /* Path Comperssion (Heuristic PART) */ 28 | int friend_ = findSet(parent[x]); 29 | parent[x] = friend_; 30 | return friend_; 31 | } 32 | } 33 | 34 | bool Union(int u, int v) 35 | { 36 | int Ru = findSet(u); /// get parent 37 | int Rv = findSet(v); 38 | 39 | if (Ru == Rv) 40 | return false; /// already Connected 41 | else 42 | { 43 | /* Rank By Union (Heuristic Part) */ 44 | 45 | if (rank_[Ru] > rank_[Rv]) 46 | { 47 | parent[Rv] = Ru; 48 | } 49 | else if (rank_[Rv] > rank_[Ru]) 50 | { 51 | parent[Ru] = Rv; 52 | } 53 | else 54 | { 55 | parent[Rv] = Ru; 56 | rank_[Ru]++; 57 | } 58 | 59 | return true; 60 | } 61 | } 62 | 63 | int32_t main() 64 | { 65 | // main func 66 | } 67 | -------------------------------------------------------------------------------- /DSA_Topics/Divide and Conquer (DnC)/Maximum-SubArray/GivenCode_Maximum_SubArray_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Result 5 | { 6 | int sum; 7 | int leftIndex; 8 | int rightIndex; 9 | }; 10 | 11 | Result crossingSum(int arr[], int left, int mid, int right) 12 | { 13 | /// left side of crossing 14 | int sum = arr[mid]; 15 | int LS = arr[mid]; 16 | int LS_index = mid; 17 | for (int i = mid - 1; i >= left; i--) 18 | { 19 | sum = sum + arr[i]; 20 | if (sum > LS) 21 | { 22 | LS = sum; 23 | LS_index = i; 24 | } 25 | } 26 | /// right side of crossing 27 | sum = arr[mid + 1]; 28 | int RS = arr[mid + 1]; 29 | int RS_index = mid + 1; 30 | for (int i = mid + 2; i <= right; i++) 31 | { 32 | sum = sum + arr[i]; 33 | if (sum > RS) 34 | { 35 | RS = sum; 36 | RS_index = i; 37 | } 38 | } 39 | /// crossing 40 | Result CS; 41 | CS.sum = LS + RS; 42 | CS.leftIndex = LS_index; 43 | CS.rightIndex = RS_index; 44 | return CS; 45 | } 46 | 47 | Result maximumSumSubarray(int arr[], int left, int right) // main 48 | { 49 | if (left == right) /// one element 50 | { 51 | Result result; 52 | result.sum = arr[left]; 53 | result.leftIndex = result.rightIndex = right; 54 | 55 | return result; 56 | } 57 | else 58 | { 59 | /// int mid = (left+right)/2; 60 | int mid = left + ((right - left) / 2); 61 | 62 | Result leftSide = maximumSumSubarray(arr, left, mid); 63 | Result rightSide = maximumSumSubarray(arr, mid + 1, right); 64 | 65 | Result crossing = crossingSum(arr, left, mid, right); 66 | 67 | if (leftSide.sum >= rightSide.sum && leftSide.sum >= crossing.sum) 68 | return leftSide; 69 | else if (rightSide.sum >= leftSide.sum && rightSide.sum >= crossing.sum) 70 | return rightSide; 71 | else 72 | return crossing; 73 | } 74 | } 75 | 76 | int main() 77 | { 78 | int n = 8; 79 | int arr[8] = {-2, -5, 6, -2, -3, 1, 5, -6}; 80 | 81 | Result result = maximumSumSubarray(arr, 0, n - 1); 82 | 83 | cout << "Summation is: " << result.sum << endl; 84 | cout << "Index: " << result.leftIndex << " " << result.rightIndex << endl; 85 | } 86 | -------------------------------------------------------------------------------- /DSA_Topics/Divide and Conquer (DnC)/Maximum-SubArray/Maximum_SubArray_BruteForce_Prefix-Sum.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | int32_t main() 18 | { 19 | int n; 20 | 21 | // cin >> n; 22 | // int arr[n]; 23 | // for (int i = 0; i < n; i++) 24 | // { 25 | // cin >> arr[i]; 26 | // } 27 | 28 | // n = 8; 29 | // int arr[8] = {-2, -5, 6, -2, -3, 1, 5, -6}; 30 | 31 | n = 5; 32 | int arr[n] = {1, 2, 3, 4, 5}; 33 | 34 | int maxSum = INT_MIN; 35 | 36 | for (int i = 0; i < n; i++) 37 | { 38 | int sum = 0; 39 | 40 | for (int j = i; j < n; j++) 41 | { 42 | sum += arr[j]; 43 | if (sum > maxSum) 44 | maxSum = sum; 45 | } 46 | } 47 | 48 | cout << "Maximum Sum: " << maxSum << nl; 49 | 50 | CRACKED; 51 | } 52 | -------------------------------------------------------------------------------- /DSA_Topics/Divide and Conquer (DnC)/Maximum-SubArray/Maximum_SubArray_DivideAndConquer.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debT() cout << "Tashin" << nl; // debug code 15 | 16 | using namespace std; 17 | class Result 18 | { 19 | public: 20 | int maxSum; 21 | int leftindex; /// index 22 | int rightIndex; /// index 23 | }; 24 | 25 | Result crossingSum(int arr[], int left, int mid, int right) 26 | { 27 | ///-------------------- checking left side 28 | Result result; 29 | int sum = 0; 30 | int lSum = INT_MIN; 31 | int index = mid; 32 | while (index >= left) /// It's imp to go from mid to left, 33 | { /// bcz in Crossing sum we take left + right part consicutive value 34 | sum += arr[index]; 35 | if (sum > lSum) 36 | { 37 | lSum = sum; 38 | result.leftindex = index; 39 | } 40 | index--; 41 | } 42 | 43 | ///-------------------- checking right side 44 | sum = 0; 45 | int rSum = INT_MIN; 46 | index = mid + 1; 47 | while (index <= right) /// It's imp to go from mid+1 to right, 48 | { /// bcz in Crossing sum we take left + right part consicutive value 49 | sum += arr[index]; 50 | if (sum > rSum) 51 | { 52 | rSum = sum; 53 | result.rightIndex = index; 54 | } 55 | index++; 56 | } 57 | 58 | result.maxSum = lSum + rSum; 59 | 60 | return result; 61 | } 62 | 63 | Result maximumSubArray(int arr[], int left, int right) 64 | { 65 | if (left == right) 66 | { 67 | Result result; 68 | result.maxSum = arr[left]; 69 | result.leftindex = result.rightIndex = left; 70 | 71 | return result; 72 | } 73 | else 74 | { 75 | int mid = left + ((right - left) / 2); 76 | 77 | Result leftSum = maximumSubArray(arr, left, mid); 78 | Result rightSum = maximumSubArray(arr, mid + 1, right); 79 | Result crossSum = crossingSum(arr, left, mid, right); 80 | 81 | // ///--> Basic code 82 | // if (leftSum.maxSum >= rightSum.maxSum && leftSum.maxSum >= crossSum.maxSum) 83 | // return leftSum; 84 | // else if (rightSum.maxSum >= leftSum.maxSum && rightSum.maxSum >= crossSum.maxSum) 85 | // return rightSum; 86 | // else 87 | // return crossSum; 88 | 89 | ///--> More Mature code 90 | if (max({leftSum.maxSum, rightSum.maxSum, crossSum.maxSum}) == leftSum.maxSum) 91 | return leftSum; 92 | else if (max({leftSum.maxSum, rightSum.maxSum, crossSum.maxSum}) == rightSum.maxSum) 93 | return rightSum; 94 | else 95 | return crossSum; 96 | } 97 | } 98 | 99 | int32_t main() 100 | { 101 | int n; 102 | n = 8; 103 | 104 | // //--> for user inputs 105 | // cin >> n; 106 | // int arr[n]; 107 | // for (int i = 0; i < n; i++) 108 | // { 109 | // cin >> arr[i]; 110 | // } 111 | 112 | // n = 8; 113 | // int arr[n] = {-2, -5, 6, -2, -3, 1, 5, -6}; 114 | 115 | n = 5; 116 | int arr[n] = {1, 2, 3, 4, 5}; 117 | 118 | Result result = maximumSubArray(arr, 0, n - 1); 119 | 120 | cout << "Maximum Sum: " << result.maxSum << endl; 121 | cout << "Index: " << result.leftindex << " " << result.rightIndex << endl; 122 | 123 | CRACKED; 124 | } 125 | -------------------------------------------------------------------------------- /DSA_Topics/Divide and Conquer (DnC)/Maximum-SubArray/Maximum_Subarray_on_circular_array.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | int32_t main() 18 | { 19 | cout << "TASHIN" << nl; 20 | 21 | CRACKED; 22 | } 23 | -------------------------------------------------------------------------------- /DSA_Topics/Divide and Conquer (DnC)/Maximum-SubArray/Problems/Alphabet World.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debT() cout << "Tashin" << nl; // debug code 15 | 16 | using namespace std; 17 | class Result 18 | { 19 | public: 20 | int maxSum = 0; 21 | int leftindex; /// index 22 | int rightIndex; /// index 23 | }; 24 | 25 | Result crossingSum(string str, int left, int mid, int right) 26 | { 27 | Result result; 28 | result.maxSum = (int)str[mid] + (int)str[mid + 1]; 29 | result.leftindex = mid; 30 | result.rightIndex = mid + 1; 31 | 32 | return result; 33 | } 34 | 35 | Result maximumSubArray(string str, int left, int right) 36 | { 37 | if (left == right || right - left == 1) // one/two element 38 | { 39 | Result result; 40 | result.maxSum = (int)str[left]; 41 | result.leftindex = left; 42 | result.rightIndex = -1; 43 | if (left != right) 44 | { 45 | result.maxSum += (int)str[right]; 46 | result.rightIndex = right; 47 | } 48 | return result; 49 | } 50 | else 51 | { 52 | int mid = left + ((right - left) / 2); 53 | 54 | Result leftSum = maximumSubArray(str, left, mid); 55 | Result rightSum = maximumSubArray(str, mid + 1, right); 56 | Result crossSum = crossingSum(str, left, mid, right); 57 | 58 | if (max({leftSum.maxSum, rightSum.maxSum, crossSum.maxSum}) == leftSum.maxSum) 59 | return leftSum; 60 | else if (max({leftSum.maxSum, rightSum.maxSum, crossSum.maxSum}) == rightSum.maxSum) 61 | return rightSum; 62 | else 63 | return crossSum; 64 | } 65 | } 66 | 67 | int32_t main() 68 | { 69 | 70 | string str; 71 | cin >> str; 72 | 73 | Result result = maximumSubArray(str, 0, len(str)); 74 | 75 | cout << "Maximum Sum: " << result.maxSum << endl; 76 | 77 | cout << "Taken Alphabates: "; 78 | if (result.leftindex != -1) 79 | { 80 | cout << str[result.leftindex]; 81 | } 82 | if (result.rightIndex != -1) 83 | { 84 | cout << str[result.rightIndex]; 85 | } 86 | cout << nl; 87 | 88 | CRACKED; 89 | } 90 | 91 | 92 | /* 93 | 94 | abcdsafasxals 95 | 96 | aAbaABzAZBA 97 | 98 | ABSCVV 99 | ZZZZaa 100 | */ -------------------------------------------------------------------------------- /DSA_Topics/Divide and Conquer (DnC)/Maximum-SubArray/Problems/MaximumSubArray_of_NonNegativeNumbers.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | using namespace std; 8 | #define CRACKED return 0; 9 | 10 | #define len(s) s.length() 11 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 12 | #define debt cout << "Tashin " << endl; // debug code 13 | 14 | // } Driver Code Ends 15 | // User function template for C++ 16 | /**********************************************************************************************************************************************************************************/ 17 | 18 | class Result 19 | { 20 | public: 21 | int maxSum = 0; 22 | int len = 0; 23 | int leftindex; /// index 24 | int rightIndex; /// index 25 | vector ans; 26 | }; 27 | 28 | Result crossingSum(int arr[], int left, int mid, int right) 29 | { 30 | ///-------------------- checking left side 31 | Result result; 32 | int sum = 0; 33 | int lSum = 0; 34 | int index = mid; 35 | vector a; 36 | 37 | while (index >= left) /// It's imp to go from mid to left, 38 | { /// bcz in Crossing sum we take left + right part consicutive value 39 | if (arr[index] < 0) 40 | { 41 | break; 42 | } 43 | result.len++; 44 | sum += arr[index]; 45 | lSum = sum; 46 | result.leftindex = index; 47 | a.push_back(arr[index]); 48 | index--; 49 | } 50 | 51 | ///-------------------- checking right side 52 | sum = 0; 53 | int rSum = 0; 54 | index = mid + 1; 55 | vector b; 56 | 57 | while (index <= right) /// It's imp to go from mid+1 to right, 58 | { /// bcz in Crossing sum we take left + right part consicutive value 59 | if (arr[index] < 0) 60 | { 61 | break; 62 | } 63 | result.len++; 64 | sum += arr[index]; 65 | rSum = sum; 66 | result.rightIndex = index; 67 | b.push_back(arr[index]); 68 | index++; 69 | } 70 | 71 | result.maxSum = lSum + rSum; 72 | 73 | reverse(a.begin(), a.end()); 74 | 75 | for (auto i : a) 76 | { 77 | result.ans.push_back(i); 78 | } 79 | 80 | for (auto i : b) 81 | { 82 | result.ans.push_back(i); 83 | } 84 | 85 | return result; 86 | } 87 | 88 | Result returnResult(Result a, Result b, Result h) 89 | { 90 | if (a.maxSum == h.maxSum) 91 | { 92 | if (a.len > h.len) 93 | return a; 94 | else 95 | return h; 96 | } 97 | else if (b.maxSum == h.maxSum) 98 | { 99 | if (b.len > h.len) 100 | return a; 101 | else 102 | return h; 103 | } 104 | else 105 | return h; 106 | } 107 | 108 | Result maximumSubArray(int arr[], int left, int right) 109 | { 110 | if (left == right) 111 | { 112 | Result result; 113 | if (arr[left] >= 0) 114 | { 115 | result.len++; 116 | result.maxSum = arr[left]; 117 | result.leftindex = result.rightIndex = left; 118 | result.ans.push_back(arr[left]); 119 | } 120 | return result; 121 | } 122 | else 123 | { 124 | int mid = left + ((right - left) / 2); 125 | 126 | Result leftSum = maximumSubArray(arr, left, mid); 127 | Result rightSum = maximumSubArray(arr, mid + 1, right); 128 | Result crossSum = crossingSum(arr, left, mid, right); 129 | 130 | if (max({leftSum.maxSum, rightSum.maxSum, crossSum.maxSum}) == leftSum.maxSum) 131 | { 132 | return returnResult(rightSum, crossSum, leftSum); 133 | } 134 | else if (max({leftSum.maxSum, rightSum.maxSum, crossSum.maxSum}) == rightSum.maxSum) 135 | return returnResult(leftSum, crossSum, rightSum); 136 | else 137 | return returnResult(rightSum, leftSum, crossSum); 138 | } 139 | } 140 | 141 | class Solution 142 | { 143 | public: 144 | vector findSubarray(int a[], int n) 145 | { 146 | 147 | vector ans; 148 | 149 | int s = -1, e = -1, sum = -1; 150 | int currentSum = -1; 151 | for (int i = 0; i < n; i++) 152 | { 153 | if (a[i] >= 0) 154 | { 155 | if (currentSum == -1) 156 | { 157 | s = i; 158 | currentSum = 0; 159 | } 160 | currentSum += a[i]; 161 | } 162 | else 163 | { 164 | if (currentSum >= sum) 165 | { 166 | 167 | e = i - 1; 168 | currentSum = 0; 169 | } 170 | } 171 | } 172 | } 173 | }; 174 | 175 | /*************************************************************************************************************************************************************************************************/ 176 | 177 | //{ Driver Code Starts. 178 | 179 | void printAns(vector &ans) 180 | { 181 | for (auto &x : ans) 182 | { 183 | cout << x << " "; 184 | } 185 | cout << "\n"; 186 | } 187 | 188 | int main() 189 | { 190 | int t; 191 | cin >> t; 192 | while (t--) 193 | { 194 | int n, i; 195 | cin >> n; 196 | int a[n]; 197 | for (i = 0; i < n; i++) 198 | { 199 | cin >> a[i]; 200 | } 201 | Solution ob; 202 | auto ans = ob.findSubarray(a, n); 203 | printAns(ans); 204 | } 205 | return 0; 206 | } 207 | -------------------------------------------------------------------------------- /DSA_Topics/Divide and Conquer (DnC)/Merge Sort/MergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printArray(int arr[], int len) 5 | { 6 | for (int i = 0; i < len; i++) 7 | cout << arr[i] << " "; 8 | cout << endl; 9 | } 10 | 11 | void merge(int *arr, int s, int e) 12 | { 13 | 14 | int mid = s + (e - s) / 2; 15 | 16 | int len1 = mid - s + 1; 17 | int len2 = e - mid; 18 | 19 | int *first = new int[len1]; // dinamic memory declear 20 | int *second = new int[len2]; 21 | 22 | // copy values 23 | int mainArrayIndex = s; 24 | for (int i = 0; i < len1; i++) 25 | { 26 | first[i] = arr[mainArrayIndex++]; 27 | } 28 | 29 | mainArrayIndex = mid + 1; // without this line code works perfectly 30 | 31 | for (int i = 0; i < len2; i++) 32 | { 33 | second[i] = arr[mainArrayIndex++]; 34 | } 35 | 36 | // merge 2 sorted arrays 37 | int index1 = 0; 38 | int index2 = 0; 39 | mainArrayIndex = s; 40 | 41 | while (index1 < len1 && index2 < len2) 42 | { 43 | if (first[index1] < second[index2]) 44 | { 45 | arr[mainArrayIndex++] = first[index1++]; 46 | } 47 | else 48 | { 49 | arr[mainArrayIndex++] = second[index2++]; 50 | } 51 | } 52 | 53 | while (index1 < len1) 54 | { 55 | arr[mainArrayIndex++] = first[index1++]; 56 | } 57 | 58 | while (index2 < len2) 59 | { 60 | arr[mainArrayIndex++] = second[index2++]; 61 | } 62 | 63 | delete[] first; 64 | delete[] second; 65 | } 66 | 67 | /* This is the implementation of the merge sort algorithm. */ 68 | void mergeSort(int *arr, int s, int e) 69 | { 70 | 71 | 72 | // base case 73 | if (s >= e) 74 | return; 75 | 76 | int mid = s + (e - s) / 2; 77 | 78 | // left part sort 79 | mergeSort(arr, s, mid); 80 | 81 | // right part sort 82 | mergeSort(arr, mid + 1, e); 83 | 84 | // merge 85 | merge(arr, s, e); 86 | } 87 | 88 | int main() 89 | { 90 | // int arr[] = {3, 7, 0, 1, 5, 8, 3, 2, 34, 66, 87, 23, 12, 12, 12}; // odd 91 | int arr[] = {22, 32, 6, -2, 2, 3, 9, 10}; // even 92 | 93 | int len = sizeof(arr) / sizeof(arr[0]); 94 | 95 | cout << "Given Array : "; 96 | printArray(arr, len); 97 | 98 | mergeSort(arr, 0, len - 1); 99 | 100 | cout << "Sorted Array : "; 101 | printArray(arr, len); 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /DSA_Topics/Divide and Conquer (DnC)/Min-Max Problem/MinMax_Using_Recursion.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | class Result 18 | { 19 | public: 20 | int minimum; 21 | int maximum; 22 | }; 23 | 24 | Result minMax(int arr[], int index, int lastIndex) 25 | { 26 | if (index == lastIndex) // checking is it last index or not 27 | { 28 | Result result; 29 | result.minimum = arr[index]; 30 | result.maximum = arr[index]; 31 | return result; 32 | } 33 | else 34 | { 35 | Result friend_ = minMax(arr, index + 1, lastIndex); /// Calling a Friend 36 | 37 | Result result; 38 | result.minimum = min(arr[index], friend_.minimum); 39 | result.maximum = max(arr[index], friend_.maximum); 40 | return result; 41 | } 42 | } 43 | 44 | int32_t main() 45 | { 46 | int n; 47 | 48 | // //--> for user inputs 49 | // cin >> n; 50 | // int arr[n]; 51 | // for (int i = 0; i < n; i++) 52 | // { 53 | // cin >> arr[i]; 54 | // } 55 | 56 | n = 8; 57 | int arr[n] = {-2, -5, 6, -2, -3, 1, 5, -6}; 58 | 59 | // n = 10; 60 | // int arr[n] = {2, 5, 8, 3, 6, 9, 1, 4, 7, 0}; 61 | 62 | Result result = minMax(arr, 0, n - 1); 63 | 64 | cout << nl; 65 | cout << "Minimum value " << result.minimum << nl; 66 | cout << "Maximum value " << result.maximum << nl; 67 | cout << nl; 68 | 69 | CRACKED; 70 | } 71 | -------------------------------------------------------------------------------- /DSA_Topics/Divide and Conquer (DnC)/Min-Max Problem/MinMax_algo_DnC.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | class Result 17 | { 18 | public: 19 | int minimum; 20 | int maximum; 21 | }; 22 | 23 | Result minMax(int arr[], int s, int e) /// DnC 24 | { 25 | if (e - s == 1 || s == e) /// when array have 2 elements or one element 26 | { 27 | Result result; /// we can use pair also 28 | result.minimum = min(arr[s], arr[e]); 29 | result.maximum = max(arr[s], arr[e]); 30 | return result; 31 | } 32 | else 33 | { 34 | int mid = s + (e - s) / 2; 35 | 36 | Result leftSide = minMax(arr, s, mid); /// calling Friend 1 37 | Result rightSide = minMax(arr, mid + 1, e); /// calling Friend 2 38 | 39 | Result result; 40 | result.maximum = max(leftSide.maximum, rightSide.maximum); 41 | result.minimum = min(leftSide.minimum, rightSide.minimum); 42 | 43 | return result; 44 | } 45 | } 46 | 47 | int32_t main() 48 | { 49 | int n; 50 | 51 | // ///---> for user inputs 52 | // cin >> n; 53 | // int arr[n]; 54 | // for (int i = 0; i < n; i++) 55 | // { 56 | // cin >> arr[i]; 57 | // } 58 | 59 | n = 8; 60 | int arr[n] = {-2, -5, 6, -2, -3, 1, 5, -6}; /// TestCase 1 61 | 62 | // n = 10; 63 | // int arr[n] = {2, 5, 8, 3, 6, 9, 1, 4, 7, 0}; /// TestCase 2 64 | 65 | Result result = minMax(arr, 0, n - 1); 66 | 67 | cout << nl; 68 | cout << "Minimum value " << result.minimum << nl; 69 | cout << "Maximum value " << result.maximum << nl; 70 | cout << nl; 71 | 72 | CRACKED; 73 | } 74 | -------------------------------------------------------------------------------- /DSA_Topics/Divide and Conquer (DnC)/Min-Max Problem/Problems/Sum_of_Min_Max.cpp: -------------------------------------------------------------------------------- 1 | // https://www.geeksforgeeks.org/problems/max-min/1?itm_source=geeksforgeeks&itm_medium=article&itm_campaign=bottom_sticky_on_article 2 | 3 | // **************** Author : Tashin.Parvez **************** 4 | // ************* United International University ************* 5 | // **************** Updated: 27/10/23 **************** 6 | 7 | #include 8 | #define CRACKED return 0; 9 | #define nl endl // NewLine 10 | using namespace std; 11 | 12 | class Result 13 | { 14 | public: 15 | int mn; 16 | int mx; 17 | }; 18 | 19 | Result minMax(int arr[], int s, int e) 20 | { 21 | if (s == e || e - s == 1) 22 | { 23 | Result r; 24 | r.mn = min(arr[s], arr[e]); 25 | r.mx = max(arr[s], arr[e]); 26 | 27 | return r; 28 | } 29 | else 30 | { 31 | int mid = s + (e - s) / 2; 32 | Result f1 = minMax(arr, s, mid); 33 | Result f2 = minMax(arr, mid + 1, e); 34 | 35 | Result r; 36 | r.mn = min(f1.mn, f2.mn); 37 | r.mx = max(f1.mx, f2.mx); 38 | return r; 39 | } 40 | } 41 | 42 | class Solution 43 | { 44 | public: 45 | int findSum(int A[], int N) 46 | { 47 | Result result = minMax(A, 0, N - 1); 48 | return result.mn + result.mx; /// here is the thing 49 | } 50 | }; 51 | 52 | //{ Driver Code Starts. 53 | int main() 54 | { 55 | int t; 56 | cin >> t; 57 | while (t--) 58 | { 59 | int n; 60 | cin >> n; 61 | int arr[n]; 62 | for (int i = 0; i < n; i++) 63 | cin >> arr[i]; 64 | Solution ob; 65 | int ans = ob.findSum(arr, n); 66 | cout << ans; 67 | cout << "\n"; 68 | } 69 | return 0; 70 | } 71 | 72 | // } Driver Code Ends -------------------------------------------------------------------------------- /DSA_Topics/Dynamic Programming/Coin Change/CoinChange_BruteForce.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | int coinChangeNaive(vector coins, int amount) /// Bruteforce approach 18 | { 19 | if (amount == 0) 20 | return 0; 21 | 22 | int minCoins = -1; /// Not set Yet 23 | 24 | for (auto i : coins) 25 | { 26 | // cout << "Taken: " << amount / i << " Coin: " << i << nl; 27 | 28 | if (i > amount) /// Not Take 29 | { 30 | continue; 31 | } 32 | else 33 | { 34 | /// take Option Solve 35 | int remainAmount = amount - i; 36 | int friend_ = coinChangeNaive(coins, remainAmount); /// friend will give me left amount solution 37 | int totalAns = friend_ + 1; 38 | 39 | if (minCoins == -1) /// this will exicute when i able o take the needed amount 40 | { 41 | minCoins = totalAns; 42 | } 43 | else if (totalAns < minCoins) 44 | { 45 | minCoins = friend_ + 1; 46 | } 47 | } 48 | /* 49 | 50 | In every iteration of the for loop 51 | checking for the different coin take option 52 | means in iteration I'm checking if I need first given amount and I will pick the ith coin first 53 | and then call friend to solve for remaining ammount 54 | 55 | but but but the main amount is not canging in any loop iteraton cz in every iteration I'm picking the ith coin and always i need the same amount of money 56 | 57 | ///-------->> Tarek Sir 11 Nov Class 58 | 59 | */ 60 | } 61 | 62 | return minCoins; 63 | } 64 | 65 | int main() 66 | { 67 | int totalCoins; 68 | cout << "Enter numofCoins: "; 69 | cin >> totalCoins; 70 | 71 | vector coins; 72 | cout << "Enter all coins: "; 73 | for (auto i = 0; i < totalCoins; i++) 74 | { 75 | int x; 76 | cin >> x; 77 | coins.push_back(x); 78 | } 79 | 80 | sort(coins.begin(), coins.end(), greater()); /// decending order Sort 81 | 82 | cout << "Enter amount to create: "; 83 | int amount; 84 | cin >> amount; 85 | cout << nl; 86 | 87 | int pr2 = coinChangeNaive(coins, amount); 88 | 89 | cout << "Naive Approach: " << nl; 90 | cout << nl << "Total coins taken: " << pr2 << nl << nl; 91 | 92 | CRACKED; 93 | } 94 | 95 | /* 96 | 97 | 98 | 3 99 | 25 100 | 10 101 | 1 102 | 103 | 30 104 | 105 | 106 | */ -------------------------------------------------------------------------------- /DSA_Topics/Dynamic Programming/Coin Change/CoinChange_DP_BottomUp.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/72153120b926011b4bae85b1ec1cacbe176e3d3c/DSA_Topics/Dynamic Programming/Coin Change/CoinChange_DP_BottomUp.cpp -------------------------------------------------------------------------------- /DSA_Topics/Dynamic Programming/Coin Change/CoinChange_DP_TopDown.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | int dpTable[1000]; 18 | 19 | int coinChange_DP(vector &coins, int amount) /// Bruteforce approach 20 | { 21 | if (amount == 0) 22 | return 0; 23 | 24 | else if (dpTable[amount] != -1) 25 | return dpTable[amount]; 26 | 27 | int minCoins = -1; /// Not set Yet 28 | 29 | for (auto i : coins) 30 | { 31 | /// cout << "Taken: " << amount / i << " Coin: " << i << nl; 32 | 33 | if (i > amount) /// Can't able to take 34 | { 35 | continue; 36 | } 37 | else 38 | { 39 | /// take option 40 | int remainAmount = amount - i; /// take one piece coin 41 | int friend_ = coinChange_DP(coins, remainAmount); /// friend wwill give me left amount solution 42 | int totalAns = friend_ + 1; 43 | 44 | if (minCoins == -1) /// when value not set yet 45 | { 46 | minCoins = totalAns; 47 | } 48 | else if (totalAns < minCoins) /// here we are doing our theory take not take min part option 49 | { /// because in minCoins I have not take part 50 | minCoins = friend_ + 1; 51 | } 52 | } 53 | /* 54 | 55 | 56 | For every every amount we have options to take as many coin's We have 57 | --> Let say I have ( 5, 2, 1 ) coin's 58 | --> so if I want to create 20 59 | --> first I have 3 option [Needed 20] 60 | --> I have 3 option [Needed 15] 61 | 62 | 63 | In every iteration of the for loop 64 | Checking for the different coin take option [In for loop Iteration : Amount needed is not changing] 65 | we are checking the other options. 66 | 67 | means in iteration I'm checking if I need first given amount and I will pick the ith coin first 68 | and then call friend to solve for remaining ammount. 69 | in second itearation I have the same need ammount but I'm cchoosing the second coin first then asking friend to help for the rest 70 | 71 | 72 | ///-------->> Tarek Sir 11 Nov Class 73 | 74 | */ 75 | } 76 | 77 | dpTable[amount] = minCoins; 78 | return minCoins; 79 | } 80 | 81 | int main() 82 | { 83 | int totalCoins; 84 | cout << "Enter numofCoins: "; 85 | cin >> totalCoins; 86 | 87 | vector coins; 88 | cout << "Enter all coins: "; 89 | for (auto i = 0; i < totalCoins; i++) 90 | { 91 | int x; 92 | cin >> x; 93 | coins.push_back(x); 94 | } 95 | 96 | sort(coins.begin(), coins.end(), greater()); /// decending order Sort 97 | 98 | memset(dpTable, -1, sizeof(dpTable)); 99 | 100 | cout << "Enter amount to create: "; 101 | int amount; 102 | cin >> amount; 103 | cout << nl; 104 | 105 | int pr2 = coinChange_DP(coins, amount); 106 | cout << nl << "Total coins taken: " << pr2 << nl << nl; 107 | 108 | CRACKED; 109 | } 110 | 111 | /* 112 | 113 | 114 | 3 115 | 25 116 | 10 117 | 1 118 | 119 | 30 120 | 121 | 122 | */ -------------------------------------------------------------------------------- /DSA_Topics/Dynamic Programming/Coin Change/Problem_in_CN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int func(vector &num, int x, vector &dptable) 5 | { 6 | if (x == 0) 7 | return 0; 8 | 9 | else if (x < 0) 10 | return INT_MAX; 11 | 12 | else if (dptable[x] != -1) 13 | return dptable[x]; 14 | 15 | int mini = INT_MAX; 16 | 17 | for (auto i : num) 18 | { 19 | int friend_ = func(num, x - i, dptable); 20 | 21 | if (friend_ != INT_MAX) 22 | mini = min(mini, 1 + friend_); 23 | } 24 | 25 | dptable[x] = mini; 26 | return mini; 27 | } 28 | 29 | int minimumElements(vector &num, int x) 30 | { 31 | vector dptable(x + 1, -1); 32 | 33 | int ans = func(num, x, dptable); 34 | 35 | if (ans != INT_MAX) 36 | { 37 | return ans; 38 | } 39 | else 40 | return -1; 41 | } -------------------------------------------------------------------------------- /DSA_Topics/Dynamic Programming/Fibonacci/Fibonacci_Bottom-UP_approach__Tabulation-Method.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define int long long 15 | using namespace std; 16 | 17 | const int N = 1e5 + 2; 18 | int dpTable[N]; 19 | 20 | int fib(int n) /// Bottom Up (Iterative Method) 21 | { 22 | dpTable[1] = 0; ///--> base case 23 | dpTable[2] = 1; ///--> base case 24 | 25 | for (int i = 3; i <= n; i++) 26 | { 27 | dpTable[i] = dpTable[i - 1] + dpTable[i - 2]; 28 | } 29 | 30 | return dpTable[n]; 31 | } 32 | 33 | int32_t main() 34 | { 35 | int n; 36 | cin >> n; 37 | 38 | cout << "\nFibonacci series " << n << "th element is : " << fib(n) << nl << nl; 39 | 40 | CRACKED; 41 | } 42 | -------------------------------------------------------------------------------- /DSA_Topics/Dynamic Programming/Fibonacci/Fibonacci_Top-Down_approach__Overlapping_Subproblems__Memorization-Method.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define int long long 15 | using namespace std; 16 | 17 | const int N = 1e5 + 2; 18 | int dpTable[N]; 19 | 20 | int fib(int n) /// Top-Down Approach 21 | { 22 | if (dpTable[n] != -1) ///--> Memorization 23 | return dpTable[n]; 24 | else if (n == 2) /// --> Base Case 25 | { 26 | dpTable[n] = 1; 27 | return dpTable[n]; 28 | } 29 | else if (n == 1) /// --> Base Case 30 | { 31 | dpTable[n] = 0; 32 | return dpTable[n]; 33 | } 34 | else 35 | { 36 | int friend1 = fib(n - 1); 37 | int friend2 = fib(n - 2); 38 | dpTable[n] = friend1 + friend2; /// storing for reuse 39 | 40 | return dpTable[n]; 41 | } 42 | } 43 | 44 | int32_t main() 45 | { 46 | int n; 47 | cin >> n; 48 | 49 | for (int i = 0; i < N; i++) 50 | { 51 | dpTable[i] = -1; 52 | } 53 | 54 | memset(dpTable, -1, sizeof(dpTable)); /// set all value of dptable by -1 55 | 56 | int num = fib(n); 57 | 58 | cout << "\n Fibonacci series " << n << "th element is : " << num << nl << nl; 59 | 60 | CRACKED; 61 | } 62 | -------------------------------------------------------------------------------- /DSA_Topics/Dynamic Programming/Fibonacci/Problems/Tetranacci Number.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define int long long 10 | using namespace std; 11 | 12 | const int N = 1e5 + 2; 13 | int dpTable[N]; 14 | 15 | int tetranacci(int n) /// Bottom Up (Iterative Method) 16 | { 17 | for (int i = 5; i <= n; i++) 18 | { 19 | dpTable[i] = dpTable[i - 1] + dpTable[i - 2] + dpTable[i - 3] + dpTable[i - 4]; 20 | } 21 | 22 | return dpTable[n]; 23 | } 24 | 25 | int32_t main() 26 | { 27 | int n = 4; 28 | for (int i = 1; i <= n; i++) 29 | { 30 | cin >> dpTable[i]; 31 | } 32 | cin >> n; 33 | 34 | cout << tetranacci(n) << nl; 35 | 36 | CRACKED; 37 | } 38 | -------------------------------------------------------------------------------- /DSA_Topics/Dynamic Programming/ZeroOne Knapsack/zeroOne_Knapsack_Bruteforce.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | typedef struct Products 18 | { 19 | string productID; 20 | int weight; 21 | int value; 22 | } Product; 23 | 24 | bool compare(Product a, Product b) /// ascending order sort on basis of value/weight 25 | { 26 | // return (a.value / a.weight) > (b.value / b.weight); /// spmetimes it creat Precision error 27 | return (a.value * b.weight) > (b.value * a.weight); /// to avoide Precision error 28 | } 29 | 30 | int zeroOneKnapsack_BruteForce(vector products, int capacity, int index) 31 | { 32 | if (capacity == 0 || index == products.size()) /// no product remain or bag capacity = zero 33 | { 34 | return 0; /// returning Profit = 0 35 | } 36 | 37 | if (products[index].weight > capacity) /// Not Take Condition 38 | { 39 | /// Skip the current product 40 | int friend_ = zeroOneKnapsack_BruteForce(products, capacity, index + 1); /// Just Increment Index and Call Friend 41 | return friend_; 42 | } 43 | else 44 | { 45 | /// take the current product 46 | int take = products[index].value; 47 | int nibo_andFriend = zeroOneKnapsack_BruteForce(products, capacity - products[index].weight, index + 1); // calling friend 48 | take += nibo_andFriend; 49 | 50 | /// Skip the current product 51 | int notTake = 0; 52 | int niboNa_andFriend = zeroOneKnapsack_BruteForce(products, capacity, index + 1); // calling friend 53 | notTake += niboNa_andFriend; 54 | 55 | return max(take, notTake); 56 | } 57 | } 58 | 59 | void printSortedProducts(vector products) 60 | { 61 | cout << nl << "Sorted Products : "; 62 | for (Product p : products) 63 | { 64 | cout << p.productID << " "; 65 | } 66 | cout << nl; 67 | } 68 | 69 | int32_t main() 70 | { 71 | cout << "Enter num of products: "; 72 | int numOfProducts; 73 | cin >> numOfProducts; 74 | 75 | vector products(numOfProducts); 76 | 77 | cout << "Enter products Id, value and weight : "; 78 | 79 | for (int i = 0; i < numOfProducts; i++) 80 | { 81 | getchar(); 82 | cin >> products[i].productID; 83 | cin >> products[i].value; 84 | cin >> products[i].weight; 85 | } 86 | 87 | cout << "Enter bag size: "; 88 | int bagCapacity; 89 | cin >> bagCapacity; // bag size 90 | 91 | sort(products.begin(), products.end(), compare); /// Soring in decending order based on value / weight 92 | printSortedProducts(products); 93 | cout << nl; 94 | 95 | int ans; 96 | 97 | /// 98 | /// 99 | /// 100 | ///------------------------------------- Bruteforce ------------------------------------------------------ 101 | 102 | cout << "BruteForce Approach: " << nl; 103 | cout << "-----------------------" << nl; 104 | ans = zeroOneKnapsack_BruteForce(products, bagCapacity, 0); // value in bag 105 | cout << "Value of the bag: " << ans << nl << nl; 106 | 107 | CRACKED; 108 | } 109 | 110 | /* 111 | 112 | 4 113 | 114 | p1 1 1 115 | p2 4 3 116 | p3 5 4 117 | p4 7 5 118 | 119 | 7 120 | 121 | 122 | */ -------------------------------------------------------------------------------- /DSA_Topics/Dynamic Programming/ZeroOne Knapsack/zeroOne_Knapsack_DP.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | using namespace std; 15 | 16 | typedef struct Products 17 | { 18 | string productID; 19 | int weight; 20 | int value; 21 | } Product; 22 | 23 | int dpTable[100][100]; 24 | /// index capacity 25 | 26 | int zeroOneKnapsack_DP(vector products, int capacity, int index) //----> O(2^n) 27 | { 28 | if (dpTable[index][capacity] != -1) /// memorization 29 | return dpTable[index][capacity]; 30 | 31 | if (capacity == 0 || index == products.size()) /// no product remain or bag capacity = zero 32 | { 33 | dpTable[index][capacity] = 0; /// storing 34 | 35 | return dpTable[index][capacity]; /// can't able to take Products 36 | } 37 | 38 | if (products[index].weight > capacity) /// Not Take Condition 39 | { 40 | /// Skip the current product == Not Take 41 | int friend_ = zeroOneKnapsack_DP(products, capacity, index + 1); /// just increase the index 42 | dpTable[index][capacity] = friend_; /// storing 43 | 44 | return dpTable[index][capacity]; 45 | } 46 | else 47 | { 48 | // 49 | /// take the current product 50 | int take = products[index].value; 51 | int nibo_andFriend = zeroOneKnapsack_DP(products, capacity - products[index].weight, index + 1); // calling friend [increase index and capacity decrease] 52 | take += nibo_andFriend; 53 | 54 | // 55 | /// Skip the current product = Not Take 56 | int notTake = 0; 57 | int niboNa_andFriend = zeroOneKnapsack_DP(products, capacity, index + 1); // calling friend [just --> increase index] 58 | notTake += niboNa_andFriend; 59 | 60 | dpTable[index][capacity] = max(take, notTake); /// takeing the max profit from (take or notTake) /// storing 61 | 62 | return dpTable[index][capacity]; 63 | } 64 | } 65 | 66 | int32_t main() 67 | { 68 | cout << "Enter num of products: "; 69 | int numOfProducts; 70 | cin >> numOfProducts; 71 | 72 | vector products(numOfProducts); 73 | 74 | cout << "Enter products Id, value and weight : "; 75 | 76 | for (int i = 0; i < numOfProducts; i++) 77 | { 78 | getchar(); 79 | cin >> products[i].productID; 80 | cin >> products[i].value; 81 | cin >> products[i].weight; 82 | } 83 | 84 | cout << "Enter bag size: "; 85 | int bagCapacity; 86 | cin >> bagCapacity; // bag size 87 | 88 | int ans; 89 | 90 | /// 91 | /// 92 | /// 93 | 94 | memset(dpTable, -1, sizeof(dpTable)); 95 | 96 | ans = zeroOneKnapsack_DP(products, bagCapacity, 0); // value in bag 97 | cout << nl << "Value of the bag: " << ans << nl << nl; 98 | 99 | CRACKED; 100 | } 101 | 102 | /* 103 | 104 | 4 105 | 106 | p1 1 1 107 | p2 4 3 108 | p3 5 4 109 | p4 7 5 110 | 111 | 112 | 7 113 | 114 | 115 | */ 116 | 117 | /* 118 | 119 | 5 120 | p1 1 1 121 | p2 2 6 122 | p3 6 8 123 | p4 5 9 124 | p5 3 6 125 | 126 | 11 127 | 128 | */ 129 | 130 | /* 131 | 132 | 4 133 | 134 | p1 1 1 135 | p2 4 3 136 | p3 5 4 137 | p4 7 5 138 | 139 | 140 | 7 141 | 142 | 143 | */ 144 | 145 | /* 146 | 147 | 5 148 | p1 1 1 149 | p2 2 6 150 | p3 6 8 151 | p4 5 9 152 | p5 3 6 153 | 154 | 11 155 | 156 | */ 157 | 158 | /* 159 | 160 | 3 161 | p1 60 10 162 | p2 100 20 163 | p3 120 30 164 | 165 | 50 166 | 167 | */ 168 | /* 169 | 170 | 3 171 | P1 60 10 172 | P2 100 20 173 | P3 120 30 174 | 175 | 50 176 | 177 | */ -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/BFS/BFS.cpp: -------------------------------------------------------------------------------- 1 | // Breadth first search 2 | #include 3 | #include 4 | #define null NULL 5 | #define nl endl 6 | using namespace std; 7 | // 0 1 2 3 4 8 | int graph[5][5] = {0, 1, 1, 1, 0, 9 | 1, 0, 0, 0, 1, 10 | 1, 0, 0, 1, 0, 11 | 1, 0, 1, 0, 1, 12 | 0, 1, 0, 1, 0}; 13 | // 0, 0, 0, 0, 0}; // not work 14 | 15 | void bfs(int source) 16 | { 17 | int color[5] = {0}; // not visited 18 | int d[5]; // distance 19 | int p[5] = {-1}; // parent 20 | 21 | /* 22 | color: 23 | 0 = white // not visited 24 | 1 = Gray // visited [in queue] 25 | 2 = Black // printed 26 | */ 27 | 28 | color[source] = 1; // visited [in queue] 29 | d[source] = 0; 30 | 31 | queue Q; 32 | Q.push(source); 33 | 34 | while (!Q.empty()) 35 | { 36 | 37 | int s = Q.front(); 38 | Q.pop(); 39 | 40 | cout << s << " "; // ans 41 | 42 | for (int i = 0; i < 5; i++) // traverse that particular row 43 | { 44 | if (graph[s][i] == 1 && color[i] == 0) 45 | // graph = 1 means connected 46 | // color = 0 means not in queue 47 | { 48 | Q.push(i); 49 | color[i] = 1; // visited [in queue] 50 | d[i] = d[s] + 1; // perticular node distance [kinda level] 51 | p[i] = s; // giving parent [ int s = Q.front() ] 52 | } 53 | } 54 | color[s] = 2; // parent color black 55 | } 56 | 57 | cout << endl; 58 | 59 | for (int i = 0; i < 5; i++) // printing all nodes distance from root 60 | { 61 | cout << "distance = " << i << " is = " << d[i] << endl; 62 | } 63 | 64 | cout << endl; 65 | 66 | for (int i = 0; i < 5; i++) // printing all nodes parent 67 | { 68 | cout << "parent = " << i << " is = " << p[i] << endl; 69 | } 70 | 71 | cout << endl; 72 | } 73 | int main() 74 | { 75 | cout << nl; 76 | bfs(4); // sending one node as a root 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/DFS/DFS.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | vector graph[100]; 11 | int visited[100]; 12 | 13 | void dfs(int source) 14 | { 15 | visited[source] = 1; 16 | for (int i = 0; i < graph[source].size(); i++) 17 | { 18 | if (visited[graph[source][i]] == 0) 19 | { 20 | dfs(visited[graph[source][i]]); 21 | } 22 | } 23 | } 24 | 25 | int32_t main() 26 | { 27 | int nodes, edges; 28 | cout << "Enter nodes and edge number: "; 29 | cin >> nodes >> edges; 30 | 31 | cout << "Enter edges from u to v: "; 32 | for (int i = 0; i < edges; i++) 33 | { 34 | int u, v; 35 | cin >> u >> v; 36 | graph[u].push_back(v); 37 | } 38 | 39 | for (int i = 0; i <= nodes; i++) 40 | { 41 | if (visited[i] == 0) 42 | { 43 | dfs(i); 44 | } 45 | } 46 | 47 | CRACKED; 48 | } 49 | -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Graph_Algorithoms/Bellman Ford Algo/Bellman_ford_GivenCode_.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | 3 | Online C++ Compiler. 4 | Code, Compile, Run and Debug C++ program online. 5 | Write your code in this editor and press "Run" button to compile and execute it. 6 | 7 | *******************************************************************************/ 8 | 9 | #include 10 | using namespace std; 11 | 12 | struct Edge 13 | { 14 | int u, v, w; 15 | }; 16 | 17 | const int INF = 0x7f7f7f7f; 18 | 19 | void pathPrint(int node, int parent[]) 20 | { 21 | if (parent[node] == node) 22 | { 23 | cout << node; 24 | return; 25 | } 26 | else 27 | { 28 | pathPrint(parent[node], parent); 29 | cout << " -> " << node; 30 | } 31 | } 32 | 33 | void printArray(int arr[], int n, int parent[]) 34 | { 35 | for (int i = 0; i < n; i++) 36 | { 37 | cout << i << " --> " << arr[i] << " Path: "; 38 | pathPrint(i, parent); 39 | cout << endl; 40 | } 41 | cout << endl; 42 | } 43 | 44 | void bellmanFord(vector edges, int V, int source) 45 | { 46 | int distance[V]; 47 | memset(distance, INF, sizeof(distance)); 48 | distance[source] = 0; 49 | int parent[V]; 50 | for (int i = 0; i < V; i++) 51 | parent[i] = i; 52 | 53 | for (int i = 1; i <= V - 1; i++) 54 | { 55 | bool updated = false; 56 | for (Edge edge : edges) 57 | { 58 | if (distance[edge.v] > distance[edge.u] + edge.w) 59 | { 60 | distance[edge.v] = distance[edge.u] + edge.w; 61 | parent[edge.v] = edge.u; 62 | updated = true; 63 | } 64 | } 65 | if (updated == false) 66 | { 67 | printArray(distance, V, parent); 68 | return; 69 | } 70 | } 71 | 72 | for (Edge edge : edges) 73 | { 74 | if (distance[edge.v] > distance[edge.u] + edge.w) 75 | { 76 | distance[edge.v] = distance[edge.u] + edge.w; 77 | cout << "Negative Cycle Exist\n"; 78 | return; 79 | } 80 | } 81 | 82 | printArray(distance, V, parent); 83 | } 84 | 85 | int main() 86 | { 87 | 88 | int V, E; 89 | cin >> V >> E; 90 | vector edges(E); 91 | for (int i = 0; i < E; i++) 92 | { 93 | cin >> edges[i].u >> edges[i].v >> edges[i].w; 94 | } 95 | int source; 96 | cin >> source; 97 | bellmanFord(edges, V, source); 98 | return 0; 99 | } -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Graph_Algorithoms/Dijkstra Algo/Dijkstra_Algo_UsingPriorityQueue.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | // https://www.youtube.com/watch?v=F3PNsWE6_hM 7 | 8 | #include 9 | #define CRACKED return 0; 10 | #define nl endl // NewLine 11 | #define null NULL 12 | #define sp " " // space 13 | #define setDec(x) fixed << setprecision(x) 14 | #define len(s) s.length() 15 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 16 | #define debt cout << "Tashin " << endl; // debug code 17 | 18 | using namespace std; 19 | 20 | /* 21 | /// Dirrected Weighted Connected non-Negative Graph 22 | Single Source shortest Path 23 | Weighted (Non-Negative) 24 | Does not work on Negative Cycle 25 | O(E*log(V)) 26 | */ 27 | 28 | struct Edge 29 | { 30 | int v; 31 | int w; 32 | }; 33 | 34 | struct Ndistance 35 | { 36 | int node; 37 | int distance; 38 | }; 39 | 40 | const int INF = 0x7F7F7F7F; 41 | 42 | void pathPrint(int currentNode, int parent[]) 43 | { 44 | if (parent[currentNode] == currentNode) 45 | { 46 | cout << currentNode << "->"; 47 | } 48 | else 49 | { 50 | pathPrint(parent[currentNode], parent); 51 | cout << currentNode << "->"; 52 | } 53 | } 54 | 55 | void Dijkstra(vector Graph[], int V, int source) 56 | { 57 | int distance[V]; 58 | 59 | for (int i = 0; i < V; i++) /// making all unreachable 60 | distance[i] = INF; 61 | 62 | distance[source] = 0; /// Source to source distance = 0 63 | 64 | int parent[V]; 65 | 66 | for (int i = 0; i < V; i++) /// make set func 67 | parent[i] = i; 68 | 69 | auto compare = [&](Ndistance a, Ndistance b) /// lamda func for compare 70 | { 71 | if (a.distance <= b.distance) /// check the return type 72 | /// priority queue returen type is diff from other's normal logic 73 | return false; 74 | else 75 | return true; 76 | }; 77 | 78 | priority_queue, decltype(compare)> PQ(compare); 79 | 80 | PQ.push({source, distance[source]}); /// source and source to source distance 81 | 82 | while (PQ.empty() == false) 83 | { 84 | Ndistance temp = PQ.top(); /// take the lowest disteance element from the current pos 85 | PQ.pop(); 86 | 87 | int currentNode = temp.node; /// reached node become Current Node 88 | int distanceFormSource = temp.distance; /// (source to u) 89 | 90 | for (Edge neighbor : Graph[currentNode]) /// node - weight ( (to & Cost ) 91 | { 92 | /// in Record > (source to u) + (u to v) 93 | if (distance[neighbor.v] > distanceFormSource + neighbor.w) /// If i can minimize the value to reach then it works 94 | { 95 | distance[neighbor.v] = distanceFormSource + neighbor.w; /// Update Record 96 | 97 | PQ.push({neighbor.v, distance[neighbor.v]}); /// push in queue 98 | 99 | parent[neighbor.v] = currentNode; /// Update parent 100 | } 101 | } 102 | } 103 | 104 | /// Algo done 105 | 106 | /**************** Print To See Path's and Weights ***********************/ 107 | 108 | cout << "\nV : W : Path" << endl; 109 | cout << "------------------" << endl; 110 | 111 | for (int i = 0; i < V; i++) 112 | { 113 | cout << i << " : "; 114 | cout << distance[i] << " : "; 115 | pathPrint(i, parent); 116 | cout << endl; 117 | } 118 | cout << endl; 119 | } 120 | 121 | int main() 122 | { 123 | /// Dirrected Weighted Connected non-Negative Graph 124 | int V, E; 125 | cout << "\nEnter total vertex and total edges: "; 126 | cin >> V >> E; 127 | 128 | vector Graph[V]; /// node & distance 129 | 130 | cout << "Enter all edges form u to v and weight: " << nl; 131 | 132 | for (int i = 0; i < E; i++) 133 | { 134 | int from, to, cost; 135 | cin >> from >> to >> cost; 136 | 137 | Graph[from].push_back({to, cost}); /// from er index e push back (to & Cost) 138 | } 139 | 140 | int source; 141 | cout << "Enter Source: "; 142 | cin >> source; 143 | 144 | Dijkstra(Graph, V, source); 145 | 146 | return 0; 147 | } 148 | 149 | /* 150 | 151 | 152 | 6 8 153 | 154 | 0 1 10 155 | 0 2 1 156 | 1 3 3 157 | 2 1 2 158 | 2 3 7 159 | 3 4 2 160 | 4 5 2 161 | 3 5 3 162 | 163 | 0 164 | 165 | */ -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Graph_Algorithoms/Dijkstra Algo/Dijkstra_Algo_UsingPriorityQueue__STL.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debt cout << "Tashin " << endl; // debug code 15 | using namespace std; 16 | 17 | /*************************************************************************************************************************/ 18 | 19 | class Solution 20 | { 21 | public: 22 | vector dijkstra(int v, vector> adj[], int s) 23 | { 24 | vector distance(v); 25 | vector visited(v); 26 | vector parent(v); 27 | 28 | for (int i = 0; i < v; i++) 29 | { 30 | visited[i] = 0; 31 | parent[i] = i; 32 | distance[i] = INT_MAX; /// not reachable 33 | } 34 | distance[s] = 0; 35 | 36 | auto cmp = [&](pair a, pair b) 37 | { 38 | return a.second > b.second; 39 | }; 40 | 41 | /// edge , distance from a perticular node 42 | priority_queue, vector>, decltype(cmp)> Q(cmp); 43 | Q.push({s, distance[s]}); 44 | 45 | while (Q.empty() == false) 46 | { 47 | pair temp = Q.top(); 48 | Q.pop(); 49 | 50 | int u = temp.first; 51 | int dis = temp.second; 52 | 53 | if (visited[u] == 1) 54 | continue; 55 | else 56 | visited[u] = 1; 57 | 58 | for (auto i : adj[u]) 59 | { 60 | int v = i[0]; 61 | int v_dis = i[1]; 62 | 63 | if (distance[v] > dis + v_dis) 64 | { 65 | distance[v] = dis + v_dis; 66 | Q.push({v, distance[v]}); 67 | parent[v] = u; 68 | } 69 | } 70 | } 71 | 72 | return distance; 73 | } 74 | }; 75 | -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Graph_Algorithoms/Dijkstra Algo/Dijkstra_Algo_UsingSet.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | // BABAR: https://www.youtube.com/watch?v=dVUR3Rm6biE 7 | 8 | #include 9 | #define CRACKED return 0; 10 | #define nl endl // NewLine 11 | #define null NULL 12 | #define sp " " // space 13 | #define setDec(x) fixed << setprecision(x) 14 | #define len(s) s.length() 15 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 16 | #define debt cout << "Tashin " << endl; // debug code 17 | 18 | using namespace std; 19 | 20 | vector dijkstraAlgo_UsingSet(map>> adj, int totalVertices, int totalEdges, int source) 21 | { 22 | vector distance(totalVertices); /// (Output Vector) store all distance from the source to particular node 23 | for (auto i : distance) 24 | { 25 | i = INT_MAX; /// can't reach 26 | } 27 | distance[source] = 0; /// source to source distance = 0 28 | 29 | set> st; 30 | // 31 | 32 | st.insert({0, source}); 33 | 34 | while (!st.empty()) 35 | { 36 | auto top = *(st.begin()); 37 | 38 | int dis = top.first; /// distance from source to u 39 | int u = top.second; /// New Node I will reach, So now I will go somewhere from this node 40 | 41 | st.erase(st.begin()); /// remove top cz this edge is picked 42 | 43 | for (auto i : adj[u]) 44 | { 45 | int v = i.first; /// want to go next 46 | if (distance[v] > dis + i.second) /// distance[v] = ?? 47 | /// dis + i.second = (distance of source to u + dis of u to v) 48 | { 49 | auto record = st.find({distance[v], v}); 50 | if (record != st.end()) 51 | { 52 | st.erase(record); 53 | } 54 | 55 | /// distance update 56 | distance[v] = dis + i.second; 57 | 58 | /// push new record 59 | st.insert({distance[v], v}); /// From u --> v 60 | } 61 | } 62 | } 63 | 64 | return distance; 65 | } 66 | 67 | int32_t main() 68 | { 69 | 70 | int totalVertices, totalEdges, source; 71 | cout << nl << "Enter total vertexs & total Edges : "; 72 | cin >> totalVertices >> totalEdges; 73 | 74 | map>> adj; ///----> adjacency List 75 | // u-vertex < v-vertex edge-weight > 76 | 77 | cout << "Enter (u to v) edge & weight as u, v , w" << nl; /// (u to v) edge weight w 78 | for (int i = 0; i < totalEdges; i++) 79 | { 80 | int u, v, w; 81 | cin >> u >> v >> w; 82 | 83 | adj[u].push_back({v, w}); /// u --> v 84 | adj[v].push_back({u, w}); /// v --> u 85 | } 86 | 87 | cout << nl << "Enter source: "; /// taking starting pont 88 | cin >> source; 89 | 90 | vector distance = dijkstraAlgo_UsingSet(adj, totalVertices, totalEdges, source); 91 | 92 | cout << "Press -1 to exit" << nl << nl; 93 | while (true) 94 | { 95 | cout << "Enter which nodes SSSP you want to know from source(u)= " << source << " : "; 96 | int v; 97 | cin >> v; 98 | 99 | if (v == -1) 100 | break; 101 | cout << "Distance form (" << source << " to " << v << ") is " << distance[v] << nl << nl; 102 | } 103 | 104 | CRACKED; 105 | } 106 | 107 | /* 108 | 4 5 109 | 110 | 0 1 5 111 | 0 2 8 112 | 1 2 9 113 | 1 3 2 114 | 2 3 6 115 | 116 | 0 117 | 118 | */ 119 | 120 | /* 121 | 122 | 6 8 123 | 124 | 0 1 10 125 | 0 2 1 126 | 2 3 7 127 | 2 1 2 128 | 1 3 3 129 | 3 5 3 130 | 3 4 2 131 | 4 5 2 132 | 133 | 0 134 | 135 | */ -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Graph_Algorithoms/Dijkstra Algo/GivenCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Edge 4 | { 5 | int v; 6 | int w; 7 | }; 8 | 9 | struct Ndistance 10 | { 11 | int node; 12 | int distance; 13 | }; 14 | 15 | const int INF = 0x7F7F7F7F; 16 | 17 | void pathPrint(int currentNode, int parent[]) 18 | { 19 | if (parent[currentNode] == currentNode) 20 | { 21 | cout << currentNode << " "; 22 | } 23 | else 24 | { 25 | pathPrint(parent[currentNode], parent); 26 | cout << currentNode << " "; 27 | } 28 | } 29 | 30 | void Dijkstra(vector Graph[], int V, int source) 31 | { 32 | int distance[V]; 33 | 34 | for (int i = 0; i < V; i++) /// making unreachable 35 | distance[i] = INF; 36 | 37 | distance[source] = 0; 38 | 39 | int parent[V]; 40 | 41 | for (int i = 0; i < V; i++) /// make set func 42 | parent[i] = i; 43 | 44 | auto compare = [&](Ndistance a, Ndistance b) 45 | { 46 | if (a.distance <= b.distance) /// check the return type 47 | /// priority queue returen type is diff from other's normal logic 48 | return false; 49 | else 50 | return true; 51 | }; 52 | 53 | priority_queue, decltype(compare)> PQ(compare); 54 | 55 | PQ.push({source, distance[source]}); /// source and source to source distance 56 | 57 | while (PQ.empty() == false) 58 | { 59 | Ndistance temp = PQ.top(); /// take the lowest disteance element from the current pos 60 | 61 | PQ.pop(); /// already reached the temp's node and now temp node is the current pos 62 | 63 | int currentNode = temp.node; /// current node (temp's Node) 64 | 65 | for (Edge neighbor : Graph[currentNode]) 66 | { 67 | if (distance[neighbor.v] > distance[currentNode] + neighbor.w) 68 | { 69 | distance[neighbor.v] = distance[currentNode] + neighbor.w; 70 | 71 | PQ.push({neighbor.v, distance[neighbor.v]}); 72 | parent[neighbor.v] = currentNode; 73 | } 74 | } 75 | } 76 | 77 | for (int i = 0; i < V; i++) 78 | { 79 | cout << i << " : "; 80 | cout << distance[i] << " @"; 81 | pathPrint(i, parent); 82 | cout << endl; 83 | } 84 | cout << endl; 85 | } 86 | 87 | int main() 88 | { 89 | 90 | /// Dirrected Weighted Connected non-Negative Graph 91 | int V, E; 92 | cin >> V >> E; 93 | 94 | vector Graph[V]; /// node & distance 95 | 96 | for (int i = 0; i < E; i++) 97 | { 98 | int from, to, cost; 99 | cin >> from >> to >> cost; 100 | 101 | Graph[from].push_back({to, cost}); /// from er index e push back (to & Cost) 102 | } 103 | 104 | int source; 105 | cin >> source; 106 | 107 | Dijkstra(Graph, V, source); 108 | 109 | return 0; 110 | } -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Graph_Algorithoms/Kruskal's Algo (MST)/KruskalsAlgo__GivenCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Edge 4 | { 5 | int u, v, w; 6 | }; 7 | 8 | /** Disjoint Set Union - Start **/ 9 | 10 | int parent[107]; 11 | int rank_[107]; 12 | 13 | void makeSet(int x) 14 | { 15 | parent[x] = x; 16 | rank_[x] = 0; 17 | } 18 | 19 | int findSet(int x) 20 | { 21 | if (parent[x] == x) 22 | return x; 23 | else 24 | { 25 | int friend_ = findSet(parent[x]); 26 | parent[x] = friend_; 27 | return friend_; 28 | } 29 | } 30 | 31 | bool Union(int u, int v) 32 | { 33 | int Ru = findSet(u); 34 | int Rv = findSet(v); 35 | 36 | if (Ru == Rv) 37 | return false; 38 | else 39 | { 40 | if (rank_[Ru] > rank_[Rv]) 41 | { 42 | parent[Rv] = Ru; 43 | } 44 | else if (rank_[Rv] > rank_[Ru]) 45 | { 46 | parent[Ru] = Rv; 47 | } 48 | else if (rank_[Ru] == rank_[Rv]) 49 | { 50 | parent[Rv] = Ru; 51 | rank_[Ru]++; 52 | } 53 | return true; 54 | } 55 | } 56 | 57 | /** Disjoint Set Union - End **/ 58 | 59 | bool compare(Edge a, Edge b) 60 | { 61 | if (a.w <= b.w) 62 | return true; 63 | else 64 | return false; 65 | } 66 | 67 | int kruskalMST(vector &edges, int V) 68 | { 69 | sort(edges.begin(), edges.end(), compare); 70 | 71 | for (int i = 0; i < V; i++) /// 72 | makeSet(i); 73 | 74 | int mstCost = 0; 75 | 76 | for (Edge e : edges) 77 | { 78 | bool unionDone = Union(e.u, e.v); 79 | if (unionDone == true) 80 | { 81 | cout << e.u << " " << e.v << endl; 82 | mstCost = mstCost + e.w; 83 | } 84 | } 85 | 86 | return mstCost; 87 | } 88 | 89 | 90 | int main() 91 | { 92 | int V, E; 93 | cin >> V >> E; 94 | vector edges(E); 95 | 96 | for (int i = 0; i < E; i++) 97 | { 98 | cin >> edges[i].u >> edges[i].v >> edges[i].w; 99 | } 100 | 101 | int mstCost = kruskalMST(edges, V); 102 | cout << mstCost << endl; 103 | 104 | return 0; 105 | } -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Graph_Algorithoms/Kruskal's Algo (MST)/Kruskals_Algo_.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debt cout << "Tashin " << endl; // debug code 15 | 16 | using namespace std; 17 | 18 | struct EdgeInfo 19 | { 20 | int u; 21 | int v; 22 | int weight; 23 | }; 24 | 25 | bool compare(EdgeInfo a, EdgeInfo b) 26 | { 27 | return a.weight <= b.weight; 28 | } 29 | 30 | /** Disjoint Set Parts **/ 31 | 32 | int parent[1000]; 33 | int rankOfThistree[1000]; 34 | 35 | void makeSet(int i) 36 | { 37 | parent[i] = i; 38 | rankOfThistree[i] = 0; 39 | } 40 | 41 | int findparent(int a) /// finding grand parent 42 | { 43 | if (parent[a] == a) 44 | return a; 45 | else 46 | { 47 | /// This is the Heuristic ( Path Compression [Heuristic] ) of Find Parent 48 | 49 | int friend_ = findparent(parent[a]); 50 | parent[a] = friend_; /// this is doing to minimize the complexity for the same case in future 51 | return parent[a]; 52 | } 53 | } 54 | 55 | bool unionSet(int a, int b) 56 | { 57 | int parentof_A = findparent(a); /// geitting parent of a 58 | int parentof_B = findparent(b); /// getting parent of b 59 | 60 | if (parentof_A == parentof_B) 61 | { 62 | return true; /// I can't take them 63 | } 64 | else 65 | { 66 | /// Union heuristic (Union Rank) 67 | 68 | if (rankOfThistree[parentof_A] == rankOfThistree[parentof_B]) 69 | { 70 | parent[parentof_A] = parentof_B; 71 | rankOfThistree[parentof_B]++; 72 | } 73 | else if (rankOfThistree[parentof_A] > rankOfThistree[parentof_B]) 74 | { 75 | parent[parentof_B] = parentof_A; 76 | } 77 | else if (rankOfThistree[parentof_A] < rankOfThistree[parentof_B]) 78 | { 79 | parent[parentof_A] = parentof_B; 80 | } 81 | 82 | return false; 83 | } 84 | } 85 | 86 | /** END **/ 87 | 88 | int kruskalMST(vector edges, int vertices) 89 | { 90 | sort(edges.begin(), edges.end(), compare); 91 | 92 | for (int i = 0; i < vertices; i++) 93 | { 94 | makeSet(i); 95 | } 96 | 97 | int mstCost = 0; 98 | 99 | cout << nl << "Taken Edges : " << nl; 100 | for (EdgeInfo edge : edges) 101 | { 102 | /// if their parent are not same then i have to take this edge 103 | bool sameParent = unionSet(edge.u, edge.v); /// Checking parent same or not (Its is checking that is these virtices are taken previous or not if taken then their parent's are same) 104 | 105 | /// parent same means i cant take them 106 | 107 | if (sameParent == false) 108 | { 109 | cout << edge.u << " --> " << edge.v << nl; 110 | 111 | mstCost += edge.weight; 112 | } 113 | } 114 | 115 | return mstCost; 116 | } 117 | int32_t main() 118 | { 119 | int numOfVertices, numofEdge; 120 | cin >> numOfVertices >> numofEdge; /// input 121 | 122 | vector edges; 123 | 124 | for (int i = 0; i < numofEdge; i++) /// taking u v and w 125 | { 126 | EdgeInfo edge; 127 | cin >> edge.u >> edge.v >> edge.weight; 128 | edges.push_back(edge); 129 | } 130 | 131 | int mstCost = kruskalMST(edges, numOfVertices); 132 | 133 | cout << nl << "MSTCost " << mstCost << nl << nl; 134 | 135 | CRACKED; 136 | } 137 | 138 | /* 139 | 140 | 5 7 141 | 0 1 2 142 | 0 3 6 143 | 1 2 3 144 | 1 3 8 145 | 1 4 5 146 | 2 4 7 147 | 3 4 9 148 | 149 | 150 | */ 151 | -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Graph_Algorithoms/Kruskal's Algo (MST)/prims.cpp: -------------------------------------------------------------------------------- 1 | // limon 2 | 3 | #include 4 | using namespace std; 5 | 6 | int minSpanningTree(v){ 7 | 8 | } 9 | 10 | int main(){ 11 | 12 | } -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Graph_Algorithoms/Problems/Kruskal's Minimum Spanning Tree.cpp: -------------------------------------------------------------------------------- 1 | 2 | //{ Driver Code Starts 3 | #include 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int spanningTree(int N, vector> adj[]) 10 | { 11 | int parent[N + 1]; 12 | 13 | int key[N + 1]; 14 | 15 | bool mstSet[N + 1]; 16 | 17 | for (int i = 0; i <= N; i++) 18 | key[i] = INT_MAX, mstSet[i] = false; 19 | 20 | priority_queue, vector>, greater>> pq; 21 | 22 | key[0] = 0; 23 | parent[0] = -1; 24 | pq.push({0, 0}); 25 | 26 | while (!pq.empty()) 27 | { 28 | int u = pq.top().second; 29 | pq.pop(); 30 | 31 | mstSet[u] = true; 32 | 33 | for (auto it : adj[u]) 34 | { 35 | int v = it[0]; 36 | int weight = it[1]; 37 | if (mstSet[v] == false && weight < key[v]) 38 | { 39 | parent[v] = u; 40 | key[v] = weight; 41 | pq.push({key[v], v}); 42 | } 43 | } 44 | } 45 | 46 | int mstWeight = 0; 47 | for (int i = 1; i <= N; i++) 48 | { 49 | if (key[i] != INT_MAX) 50 | mstWeight += key[i]; 51 | } 52 | return mstWeight; 53 | } 54 | }; 55 | /******************************************************************************/ 56 | 57 | class Solution 58 | { 59 | public: 60 | //Function to find sum of weights of edges of the Minimum Spanning Tree. 61 | int spanningTree(int V, vector> adj[]) 62 | { 63 | // code here 64 | vector key(V,INT_MAX); 65 | vector visited(V,false); 66 | vector parent(V,-1); 67 | 68 | key[0]=0; 69 | parent[0]=-1; 70 | 71 | for(int i=0;ikey[j]){ 76 | mini=key[j]; 77 | u=j; 78 | } 79 | } 80 | visited[u]=true; 81 | for(auto v:adj[u]){ 82 | if(!visited[v[0]] && key[v[0]]>v[1]){ 83 | key[v[0]]=v[1]; 84 | parent[v[0]]=u; 85 | } 86 | } 87 | } 88 | int ans=0; 89 | 90 | for(int i=1;i> t; 103 | while (t--) 104 | { 105 | int V, E; 106 | cin >> V >> E; 107 | vector> adj[V]; 108 | int i = 0; 109 | while (i++ < E) 110 | { 111 | int u, v, w; 112 | cin >> u >> v >> w; 113 | vector t1, t2; 114 | t1.push_back(v); 115 | t1.push_back(w); 116 | adj[u].push_back(t1); 117 | t2.push_back(u); 118 | t2.push_back(w); 119 | adj[v].push_back(t2); 120 | } 121 | 122 | Solution obj; 123 | cout << obj.spanningTree(V, adj) << "\n"; 124 | } 125 | 126 | return 0; 127 | } 128 | 129 | // } Driver Code Ends -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Graph_Algorithoms/Problems/mySolve.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution 7 | { 8 | 9 | public: 10 | struct EdgeInfo 11 | { 12 | int u; 13 | int v; 14 | int weight; 15 | }; 16 | 17 | static bool compare(EdgeInfo a, EdgeInfo b) 18 | { 19 | return a.weight < b.weight; 20 | } 21 | 22 | // Function to find sum of weights of edges of the Minimum Spanning Tree. 23 | int spanningTree(int V, vector> adj[]) 24 | { 25 | // code here 26 | vector visited(V, 0); 27 | vector edges; 28 | 29 | int i = 0; 30 | while (i < V) 31 | { 32 | int cost = INT_MAX; 33 | EdgeInfo edg; 34 | 35 | for (auto j : adj[i]) 36 | { 37 | if (cost > j[1]) 38 | { 39 | cost = j[1]; 40 | 41 | edg.u = i; 42 | edg.v = j[0]; 43 | edg.weight = j[1]; 44 | } 45 | } 46 | 47 | edges.push_back(edg); 48 | i++; 49 | } 50 | 51 | sort(edges.begin(), edges.end(), compare); /// sort based on their weight 52 | int MSTcost = 0; 53 | for (auto i : edges) 54 | { 55 | if (visited[i.u] == 0 || visited[i.v] == 0) 56 | { 57 | 58 | // cout << i.u << " ---> " << i.v << endl; 59 | 60 | MSTcost += i.weight; 61 | visited[i.u] = 1; 62 | visited[i.v] = 1; 63 | } 64 | } 65 | 66 | return MSTcost; 67 | } 68 | }; 69 | 70 | //{ Driver Code Starts. 71 | 72 | int main() 73 | { 74 | int t; 75 | cin >> t; 76 | while (t--) 77 | { 78 | int V, E; 79 | cin >> V >> E; 80 | vector> adj[V]; 81 | int i = 0; 82 | while (i++ < E) 83 | { 84 | int u, v, w; 85 | cin >> u >> v >> w; 86 | vector t1, t2; 87 | t1.push_back(v); 88 | t1.push_back(w); 89 | adj[u].push_back(t1); 90 | 91 | t2.push_back(u); 92 | t2.push_back(w); 93 | adj[v].push_back(t2); 94 | } 95 | 96 | Solution obj; 97 | cout << obj.spanningTree(V, adj) << "\n"; 98 | } 99 | 100 | return 0; 101 | } 102 | 103 | // } Driver Code Ends -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Learned/BFS_using_adjList_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | void prepareAdjList(unordered_map> &adjList, vector> &edge) 10 | { 11 | for (auto i : edge) 12 | { 13 | adjList[i.first].push_back(i.second); 14 | adjList[i.second].push_back(i.first); 15 | } 16 | } 17 | void bfs(unordered_map> &adjList, unordered_map &visited, vector &ans, int node) 18 | { 19 | queue q; 20 | q.push(node); 21 | visited[node] = 1; 22 | while (!q.empty()) 23 | { 24 | int frontNode = q.front(); 25 | // store ans 26 | ans.push_back(frontNode); 27 | q.pop(); 28 | for (auto i : adjList[frontNode]) 29 | { 30 | if (!visited[i]) 31 | { 32 | q.push(i); 33 | visited[i] = 1; 34 | } 35 | } 36 | } 37 | } 38 | 39 | vector BFS(int vertex, vector> edge) 40 | { 41 | unordered_map> adjList; // make adj list 42 | vector ans; // store bfs 43 | unordered_map visited; // coloring 44 | prepareAdjList(adjList, edge); 45 | for (int i = 0; i < vertex; i++) 46 | { 47 | if (!visited[i]) 48 | { 49 | bfs(adjList, visited, ans, i); 50 | } 51 | } 52 | } 53 | 54 | int32_t main() 55 | { 56 | CRACKED; 57 | } 58 | -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Learned/DFS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define null NULL 4 | #define nl endl 5 | using namespace std; 6 | 7 | int visited[4] = {0}; 8 | int nodes = 4; 9 | int adjMatrix[4][4] = {1, 0, 0, 1, 10 | 0, 1, 1, 0, 11 | 0, 1, 1, 0, 12 | 1, 0, 0, 1}; 13 | 14 | void dfs(int start) // comming 2 15 | { 16 | visited[start] = 1; 17 | printf("%d ", start); 18 | 19 | for (int i = 0; i < nodes; i++) 20 | { 21 | if (adjMatrix[start][i] && !visited[i]) 22 | { 23 | dfs(i); 24 | } 25 | } 26 | } 27 | 28 | int main() 29 | { 30 | int start = 2; 31 | 32 | printf("\nDFS Traversal: "); 33 | dfs(start); 34 | for (start = 0; start < nodes; start++) 35 | dfs(start); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Learned/DFS_using_adjList_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | void prepareAdjList(unordered_map> &adjList, vector> &edge) 10 | { 11 | for (auto i : edge) 12 | { 13 | adjList[i.first].push_back(i.second); 14 | adjList[i.second].push_back(i.first); 15 | } 16 | } 17 | void dfs(int node, unordered_map &visited, unordered_map> &adjList, vector &component) 18 | { 19 | component.push_back(node); 20 | visited[node] = 1; 21 | 22 | for (auto i : adjList[node]) 23 | { 24 | if (!visited[i]) 25 | dfs(i, visited, adjList, component); 26 | } 27 | } 28 | 29 | vector DFS(int vertex, int e, vector> edge) 30 | { 31 | unordered_map> adjList; // make adj list 32 | prepareAdjList(adjList, edge); 33 | 34 | vector> ans; // store dfs 35 | 36 | unordered_map visited; // coloring 37 | 38 | // for all nodes 39 | for (int i = 0; i < vertex; i++) 40 | { 41 | if (!visited[i]) 42 | { 43 | vector component; 44 | dfs(i, visited, adjList, component); 45 | ans.push_back(component); 46 | } 47 | } 48 | } 49 | 50 | int32_t main() 51 | { 52 | CRACKED; 53 | } 54 | -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Learned/creat_Graph_using_adjList_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class graph 11 | { 12 | public: 13 | unordered_map> adj; 14 | void addEdge(int u, int v, int direction) 15 | { 16 | // dirrection == 1 -> directed 17 | // dirrection == 0 -> Undirected 18 | adj[u].push_back(v); 19 | if (direction) 20 | { 21 | adj[v].push_back(u); 22 | } 23 | } 24 | 25 | void printAdjList() 26 | { 27 | for (auto i : adj) 28 | { 29 | cout << i.first << " -> "; 30 | for (auto j : i.second) 31 | { 32 | cout << j << ", "; 33 | } 34 | cout << nl; 35 | } 36 | } 37 | }; 38 | 39 | int32_t main() 40 | { 41 | int n; 42 | cout << "Enter number of nodes: "; 43 | cin >> n; 44 | 45 | int m; 46 | cout << "Enter number of edges: "; 47 | cin >> m; 48 | graph g; 49 | for (int i = 0; i < m; i++) 50 | { 51 | int u, v; 52 | cin >> u >> v; 53 | // creating undirected egde 54 | g.addEdge(u, v, 0); 55 | } 56 | 57 | g.printAdjList(); 58 | 59 | CRACKED; 60 | } 61 | -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Problems_BFS-DFS/Backtracking__Rat_in_a_Maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define CRACKED return 0; 3 | #define nl endl // NewLine 4 | #define null NULL 5 | using namespace std; 6 | int maze[5][5] = {0, 1, 0, 0, 2, 7 | 0, 0, 0, 1, 0, 8 | 0, 3, 1, 0, 0, 9 | 0, 1, 0, 1, 0, 10 | 0, 0, 0, 0, 0}; 11 | // int visited[5][5] = {0}; 12 | int solution[5][5] = {0}; 13 | int n = 5; 14 | bool isSafe(int r, int c) 15 | { 16 | if (r < n && c << n && maze[r][c] == 0) 17 | { 18 | return true; 19 | } 20 | else 21 | return false; 22 | } 23 | bool ratinMaze(int row, int col) 24 | { 25 | if (maze[row][col] == 2) 26 | { 27 | solution[row][col] = 1; 28 | return true; 29 | } 30 | 31 | if (isSafe(row, col)) 32 | { 33 | solution[row][col] = 1; 34 | if (ratinMaze(row + 1, col)) 35 | { 36 | return true; 37 | } 38 | if (ratinMaze(row, col + 1)) 39 | { 40 | return true; 41 | } 42 | solution[row][col] = 0; 43 | return false; 44 | } 45 | return false; 46 | } 47 | 48 | int32_t main() 49 | { 50 | 51 | if (ratinMaze(1, 0)) 52 | { 53 | cout << "YES" << nl; 54 | } 55 | else 56 | cout << "No" << nl; 57 | CRACKED; 58 | } 59 | -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Problems_BFS-DFS/Lecture40_RatinaMazeProblem.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | bool isSafe(int row, int col, int n, vector> &visited, vector> &m) 11 | { 12 | if (row >= 0 && col < n && col >= 0 && col < n && visited[row][col] == 0 && m[row][col == 0]) 13 | { 14 | return true; 15 | } 16 | else 17 | return false; 18 | } 19 | 20 | void solve(vector> &m, int n, vector &ans, int row, int col, vector> &visited, string path) 21 | { 22 | if (row == n - 1 && col == n - 1) 23 | { 24 | ans.push_back(path); 25 | return; 26 | } 27 | visited[row][col] = 1; 28 | 29 | if (isSafe(row - 1, col, n, visited, m)) 30 | { 31 | path.push_back('U'); 32 | solve(m, n, ans, row - 1, col, visited, path); 33 | path.pop_back(); 34 | } 35 | 36 | if (isSafe(row + 1, col, n, visited, m)) 37 | { 38 | path.push_back('D'); 39 | solve(m, n, ans, row + 1, col, visited, path); 40 | path.pop_back(); 41 | } 42 | 43 | if (isSafe(row, col + 1, n, visited, m)) 44 | { 45 | path.push_back('R'); 46 | solve(m, n, ans, row, col + 1, visited, path); 47 | path.pop_back(); 48 | } 49 | if (isSafe(row, col - 1, n, visited, m)) 50 | { 51 | path.push_back('L'); 52 | solve(m, n, ans, row, col - 1, visited, path); 53 | path.pop_back(); 54 | } 55 | 56 | visited[row][col] = 0; 57 | } 58 | 59 | vector findpath(vector> &m, int row, int col, int n) 60 | { 61 | vector ans; 62 | if (m[row][col] == 0) 63 | { 64 | return ans; 65 | } 66 | vector> visited; 67 | for (int i = 0; i < n; i++) 68 | { 69 | for (int j = 0; j < n; j++) 70 | { 71 | visited[i][j] = 0; 72 | } 73 | } 74 | string path = ""; 75 | solve(m, n, ans, row, col, visited, path); 76 | } 77 | 78 | int32_t main() 79 | { 80 | 81 | CRACKED; 82 | } 83 | -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Problems_BFS-DFS/ShortestPath_using_BFS.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class Graph 11 | { 12 | int V; // vertices 13 | unordered_map> adjList; // adj.List 14 | 15 | public: 16 | Graph(int vertices) // constructor 17 | { 18 | this->V = vertices; 19 | } 20 | 21 | void addEdge(int u, int v) 22 | { 23 | adjList[u].push_back(v); 24 | adjList[v].push_back(u); 25 | } 26 | 27 | void findShortestPath(int source, int dest) 28 | { 29 | int parent[V]; 30 | parent[source] = -1; 31 | bool visited[V] = {0}; 32 | queue q; 33 | q.push(source); 34 | visited[source] = 1; 35 | 36 | while (!q.empty()) 37 | { 38 | int x = q.front(); 39 | q.pop(); 40 | for (auto i : adjList[x]) 41 | { 42 | if (!visited[i]) 43 | { 44 | visited[i] = 1; 45 | parent[i] = x; 46 | q.push(i); 47 | } 48 | } 49 | } 50 | // finding the shortest path 51 | int currentNode = dest; 52 | // we will go from dest to source 53 | int cnt = 0; 54 | stack s; 55 | s.push(currentNode); 56 | while (currentNode != source) 57 | { 58 | currentNode = parent[currentNode]; 59 | s.push(currentNode); 60 | cnt++; 61 | } 62 | while (!s.empty()) 63 | { 64 | cout << s.top() << " "; 65 | s.pop(); 66 | } 67 | cout << nl; 68 | } 69 | }; 70 | 71 | int main() 72 | { 73 | cout << "Enter number of vertices: "; 74 | int vertices, edges; 75 | cin >> vertices; 76 | cout << "Enter number of edges : "; 77 | cin >> edges; 78 | 79 | Graph g(vertices); 80 | for (int i = 0; i < edges; i++) 81 | { 82 | cout << "Enter edges from (u to v): "; 83 | int u, v; 84 | cin >> u >> v; 85 | g.addEdge(u, v); 86 | } 87 | 88 | cout << "enter source and destination " << nl; 89 | int s, d; 90 | cin >> s >> d; 91 | g.findShortestPath(s, d); 92 | 93 | return 0; 94 | } 95 | // question 2 96 | /* 97 | 6 8 98 | 99 | 0 1 100 | 0 5 101 | 1 2 102 | 2 3 103 | 2 6 104 | 3 4 105 | 4 6 106 | 5 6 107 | 108 | 109 | 110 | */ 111 | 112 | // question 1 113 | /* 114 | 115 | 5 4 116 | 117 | 0 2 118 | 2 4 119 | 0 1 120 | 1 3 121 | 122 | */ -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Sorting/Topological_Sort/Using_BFS_kahnsAlgo_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | class Graph 11 | { 12 | int V; // vertices 13 | unordered_map> adjList; // adj.List 14 | 15 | public: 16 | Graph(int vertices) // constructor 17 | { 18 | this->V = vertices; 19 | } 20 | 21 | void addEdge(int u, int v) 22 | { 23 | adjList[u].push_back(v); 24 | } 25 | 26 | /* 27 | Rules is : 28 | push only if its indegree is zero 29 | after every push decrease its child indegree and check its degree becomes 0 or not 30 | */ 31 | 32 | void topSort() 33 | { 34 | queue q; 35 | vector indegree(V); // we are taking nodes values from 0 to n. 36 | vector result; 37 | int visitedCount = 0; // is to check there is any circle or not 38 | 39 | // Calculate indegree 40 | for (auto p : adjList) 41 | { 42 | // int u = p.first; // comming from u 43 | for (auto v : p.second) 44 | { 45 | indegree[v]++; 46 | } 47 | } 48 | 49 | // insert vertices with 0 indegree in queue 50 | for (int i = 0; i < indegree.size(); i++) 51 | { 52 | if (indegree[i] == 0) 53 | q.push(i); 54 | } 55 | 56 | // BFS 57 | while (!q.empty()) 58 | { 59 | int x = q.front(); 60 | q.pop(); 61 | visitedCount++; 62 | result.push_back(x); 63 | 64 | // Reduce indegree of x's child 65 | for (auto dest : adjList[x]) 66 | { 67 | indegree[dest]--; 68 | if (indegree[dest] == 0) // checking any of child have 0 indegree or not 69 | q.push(dest); 70 | } 71 | } 72 | 73 | if (visitedCount != V) 74 | { 75 | cout << "There exists a cycle in the graph"; 76 | return; 77 | } 78 | else 79 | { 80 | for (int x : result) 81 | { 82 | cout << x << "\t"; 83 | } 84 | cout << endl; 85 | } 86 | } 87 | }; 88 | 89 | int main() 90 | { 91 | Graph g(6); 92 | g.addEdge(5, 2); 93 | g.addEdge(5, 0); 94 | g.addEdge(4, 0); 95 | g.addEdge(4, 1); 96 | g.addEdge(2, 3); 97 | g.addEdge(3, 1); 98 | 99 | g.topSort(); 100 | return 0; 101 | } -------------------------------------------------------------------------------- /DSA_Topics/Graph and Graph Algo/Sorting/Topological_Sort/Using_DFS_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | vector graph[100]; 11 | int visited[100] = {0}; 12 | stack result; 13 | int cnt = 1; 14 | 15 | void dfs(int source) 16 | { 17 | visited[source] = 1; 18 | 19 | for (int i = 0; i < graph[source].size(); i++) 20 | { 21 | if (visited[graph[source][i]] == 0) 22 | { 23 | dfs(graph[source][i]); 24 | } 25 | } 26 | 27 | // all nodes are vsiited 28 | 29 | cout << "PUSH " << cnt << " " << source << nl; // debug 30 | // pushing when doing backtracking 31 | result.push(source); 32 | cnt++; 33 | } 34 | /* 35 | 36 | --------------- Logic of TopSort ----------------- 37 | if from 1 no edge i can go o 4, 5 ,7 edges 38 | then 1 should be come first then these edges in sorting order 39 | 40 | */ 41 | int32_t main() 42 | { 43 | int nodes, edges; 44 | cout << "Enter nodes and edge number: "; 45 | cin >> nodes >> edges; 46 | 47 | // input edges 48 | for (int i = 0; i < edges; i++) 49 | { 50 | cout << "Enter edges from u to v: "; 51 | int u, v; 52 | cin >> u >> v; 53 | graph[u].push_back(v); 54 | } 55 | 56 | for (int i = 1; i <= nodes; i++) 57 | { 58 | // calling DFS 59 | if (visited[i] == 0) 60 | dfs(i); 61 | } 62 | 63 | cout << nl << "Topological Order: "; 64 | while (!result.empty()) 65 | { 66 | cout << result.top() << " "; 67 | result.pop(); 68 | } 69 | cout << nl; 70 | CRACKED; 71 | } 72 | /* 73 | 6 7 74 | 75 | 1 2 76 | 2 4 77 | 1 3 78 | 3 4 79 | 4 6 80 | 4 5 81 | 5 6 82 | 83 | */ 84 | 85 | /* 86 | 87 | 5 4 88 | 89 | 3 2 90 | 1 2 91 | 2 4 92 | 2 5 93 | 94 | 95 | */ -------------------------------------------------------------------------------- /DSA_Topics/Greedy Algorithoms/Activity Selection/Activity_Selection__Maximization.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | bool comperator(vector &v1, vector &v2) 18 | { 19 | ///-----> Basic Code 20 | // if (v1[1] < v2[1]) 21 | // return true; 22 | // else 23 | // return false; 24 | 25 | ///-----> Optimized 26 | return v1[1] < v2[1]; 27 | } 28 | 29 | int32_t main() 30 | { 31 | cout << "Enter total tasks: " << nl; 32 | int n; 33 | cin >> n; 34 | 35 | vector> vii(n); /// 2D vector = each index represent another vector(start time, end time) 36 | 37 | cout << "Enter n tasks start and end times: " << nl; 38 | for (int i = 0; i < n; i++) 39 | { 40 | int start, end; 41 | cin >> start >> end; 42 | vii.push_back({start, end}); 43 | } 44 | 45 | sort(vii.begin(), vii.end(), comperator); // ascending order based on end time 46 | 47 | // lamda function [custome comperator] 48 | // sort(vii.begin(), vii.end(), [&](vector a, vector b) 49 | // { return a[1] < b[1]; }); 50 | 51 | int taskTaken = 1; 52 | int endTime = vii[0][1]; 53 | for (int i = 1; i < n; i++) 54 | { 55 | if (vii[i][0] >= endTime) 56 | { 57 | taskTaken++; 58 | endTime = vii[i][1]; 59 | } 60 | } 61 | 62 | cout << "Task Taken " << taskTaken << nl; 63 | 64 | CRACKED; 65 | } 66 | -------------------------------------------------------------------------------- /DSA_Topics/Greedy Algorithoms/Activity Selection/Activity_Selection__UsingStructure.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | typedef struct Activities /// Container 18 | { 19 | string actNo; 20 | int startTime; 21 | int endTime; 22 | } Activity; 23 | 24 | bool compare(Activity a, Activity b) /// Comperator 25 | { 26 | return a.endTime < b.endTime; 27 | } 28 | 29 | int activitySelection(vector activity) 30 | { 31 | 32 | if (activity.size() == 0) 33 | return 0; 34 | 35 | sort(activity.begin(), activity.end(), compare); 36 | 37 | int taskTaken_Counter = 1; /// always picked first task 38 | int freeTime = activity[0].endTime; 39 | 40 | cout << nl << "Activity Picked : " << activity[0].actNo << " "; 41 | 42 | for (int i = 1; i < activity.size(); i++) 43 | { 44 | if (activity[i].startTime >= freeTime) 45 | { 46 | cout << activity[i].actNo << " "; 47 | taskTaken_Counter++; 48 | freeTime = activity[i].endTime; 49 | } 50 | } 51 | return taskTaken_Counter; 52 | } 53 | 54 | int32_t main() 55 | { 56 | int n; 57 | cin >> n; 58 | vector activity(n); 59 | for (int i = 0; i < n; i++) 60 | { 61 | getchar(); 62 | // cin.ignore(); /// similar as getchar(); 63 | 64 | cin >> activity[i].actNo; 65 | cin >> activity[i].startTime; 66 | cin >> activity[i].endTime; 67 | } 68 | 69 | int numberOftask = activitySelection(activity); 70 | 71 | cout << nl << "Num of tasks: " << numberOftask << nl; 72 | 73 | CRACKED; 74 | } 75 | 76 | /* 77 | 8 78 | 79 | a1 1 3 80 | a2 0 4 81 | a3 1 2 82 | a4 4 6 83 | a5 2 9 84 | a6 5 8 85 | a7 3 5 86 | a8 4 5 87 | 88 | 89 | ans: 3 90 | 91 | */ -------------------------------------------------------------------------------- /DSA_Topics/Greedy Algorithoms/Activity Selection/Problems/ReturnTicket.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | using namespace std; 10 | 11 | bool activitySelection(vector start, vector end, int n, int amountNeed) 12 | { 13 | if (start.size() == 0) 14 | { 15 | if (amountNeed == 0) 16 | return true; 17 | else 18 | return false; 19 | } 20 | 21 | vector> vii; 22 | 23 | for (auto i = 0; i < n; i++) 24 | { 25 | vii.push_back({start[i], end[i]}); 26 | } 27 | 28 | sort(vii.begin(), vii.end(), [&](pair a, pair b) 29 | { return a.second < b.second; }); 30 | 31 | int taskpicked = 1; 32 | int money = 10; 33 | int availability = vii[0].second; 34 | 35 | for (int i = 1; i < n; i++) 36 | { 37 | if (vii[i].first > availability) 38 | { 39 | taskpicked++; 40 | money += 10; 41 | availability = vii[i].second; 42 | if (amountNeed <= money) 43 | break; 44 | } 45 | } 46 | 47 | if (amountNeed <= money) 48 | return true; 49 | else 50 | return false; 51 | } 52 | 53 | int32_t main() 54 | { 55 | 56 | int n; 57 | cin >> n; 58 | vector start(n), end(n); 59 | 60 | for (int i = 0; i < n; i++) 61 | cin >> start[i]; 62 | for (int i = 0; i < n; i++) 63 | cin >> end[i]; 64 | 65 | int numofABCurrencyYouNeed; 66 | cin >> numofABCurrencyYouNeed; 67 | 68 | if (activitySelection(start, end, n, numofABCurrencyYouNeed)) 69 | cout << "YES" << nl; 70 | else 71 | cout << "NO" << nl; 72 | } 73 | 74 | 75 | /* 76 | 77 | 4 78 | 1 3 2 5 79 | 2 4 3 6 80 | 40 81 | 82 | */ 83 | 84 | /* 85 | 86 | 4 87 | 1 2 3 5 88 | 2 3 4 6 89 | 30 90 | 91 | */ -------------------------------------------------------------------------------- /DSA_Topics/Greedy Algorithoms/Coin Change - Minimixation/Coin_Change__BruteForce_Approach.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | int coinChangeNaive(vector coins, int amount) /// Bruteforce approach 18 | { 19 | if (amount == 0) 20 | return 0; 21 | 22 | int minCoins = -1; 23 | 24 | for (auto i : coins) 25 | { 26 | // cout << "Taken: " << amount / i << " Coin: " << i << nl; 27 | 28 | if (i > amount) /// Not Take 29 | { 30 | continue; 31 | } 32 | else 33 | { 34 | /// take Option Solve 35 | int remainAmount = amount - i; /// take onely one pice 36 | int friend_ = coinChangeNaive(coins, remainAmount); /// friend will give me left amount solution 37 | int totalAns = friend_ + 1; 38 | 39 | if (minCoins == -1) /// this will exicute when i able o take the needed amount 40 | { 41 | minCoins = totalAns; 42 | } 43 | else if (totalAns < minCoins) 44 | { 45 | minCoins = friend_ + 1; 46 | } 47 | } 48 | /* 49 | In every iteration of the for loop 50 | checking for the different coin take option 51 | means in iteration I'm checking if I need first given amount and I will pick the ith coin first 52 | and then call friend to solve for remaining ammount 53 | 54 | ///-------->> Tarek Sir 11 Nov Class 55 | */ 56 | } /// for loop end 57 | 58 | return minCoins; 59 | } 60 | 61 | int main() 62 | { 63 | int totalCoins; 64 | cout << "Enter numofCoins: "; 65 | cin >> totalCoins; 66 | 67 | vector coins; 68 | cout << "Enter all coins: "; 69 | for (auto i = 0; i < totalCoins; i++) 70 | { 71 | int x; 72 | cin >> x; 73 | coins.push_back(x); 74 | } 75 | 76 | sort(coins.begin(), coins.end(), greater()); /// decending order Sort 77 | 78 | cout << "Enter amount to create: "; 79 | int amount; 80 | cin >> amount; 81 | cout << nl; 82 | 83 | int pr2 = coinChangeNaive(coins, amount); 84 | cout << "Naive Approach: " << nl; 85 | cout << nl << "Total coins taken: " << pr2 << nl << nl; 86 | 87 | CRACKED; 88 | } 89 | 90 | /* 91 | 92 | 93 | 3 94 | 25 95 | 10 96 | 1 97 | 98 | 30 99 | 100 | 101 | */ -------------------------------------------------------------------------------- /DSA_Topics/Greedy Algorithoms/Coin Change - Minimixation/Coin_Change__Greedy_Approach__Mimimization.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | int coinChangeNaive(vector coins, int amount) /// Bruteforce approach 18 | { 19 | if (amount == 0) 20 | return 0; 21 | 22 | int minCoins = -1; 23 | 24 | for (auto i : coins) 25 | { 26 | // cout << "Taken: " << amount / i << " Coin: " << i << nl; 27 | 28 | if (i > amount) /// Not take 29 | { 30 | continue; 31 | } 32 | else 33 | { 34 | int remainAmount = amount - i; 35 | int friend_ = coinChangeNaive(coins, remainAmount); /// take Option Running 36 | 37 | if (minCoins == -1) 38 | { 39 | minCoins = friend_ + 1; 40 | } 41 | else if (friend_ + 1 < minCoins) 42 | { 43 | minCoins = friend_ + 1; 44 | } 45 | } 46 | } 47 | 48 | return minCoins; 49 | } 50 | 51 | pair coinChangeGreedy(vector coins, int amount) /// Greedy Approach 52 | { 53 | int count = 0; 54 | 55 | for (auto i : coins) 56 | { 57 | cout << "Taken: " << amount / i << " Coin: " << i << nl; 58 | 59 | count += amount / i; 60 | amount -= (amount / i) * i; 61 | } 62 | 63 | return {amount, count}; /// returning left amount and total taken coins 64 | } 65 | 66 | int main() 67 | { 68 | int totalCoins; 69 | cout << "Enter numofCoins: "; 70 | cin >> totalCoins; 71 | 72 | vector coins(totalCoins); 73 | cout << "Enter all coins: "; 74 | for (auto i = 0; i < totalCoins; i++) 75 | { 76 | int x; 77 | cin >> x; 78 | coins.push_back(x); 79 | } 80 | 81 | sort(coins.begin(), coins.end(), greater()); /// decending order Sort 82 | 83 | cout << "Enter amount to create: "; 84 | int amount; 85 | cin >> amount; 86 | 87 | /// 88 | /// 89 | /// 90 | 91 | cout << nl; 92 | cout << "Greedy Approach: " << nl; 93 | cout << "----------------------------" << nl; 94 | pair pr = coinChangeGreedy(coins, amount); 95 | 96 | cout << nl << "Remaining Amounts: " << pr.first << nl; 97 | cout << "Total coins taken: " << pr.second << nl << nl; 98 | 99 | /// 100 | /// 101 | /// 102 | 103 | // cout << nl << nl; 104 | // cout << "Naive Approach: " << nl; /// Bruteforce Approach 105 | // cout << "----------------------------" << nl; 106 | // 107 | // int pr2 = coinChangeNaive(coins, amount); 108 | // cout << "Total coins taken: " << pr2 << nl << nl; 109 | 110 | CRACKED; 111 | } 112 | 113 | /* 114 | ------------------------- >> Case : 1 115 | 116 | 10 117 | 1000 118 | 500 119 | 200 120 | 100 121 | 50 122 | 20 123 | 10 124 | 5 125 | 2 126 | 1 127 | 128 | 52727 129 | 130 | */ 131 | 132 | /* 133 | ------------------------- >> Case : 2 134 | 135 | 3 136 | 25 137 | 10 138 | 1 139 | 140 | 30 141 | 142 | 143 | */ -------------------------------------------------------------------------------- /DSA_Topics/Greedy Algorithoms/Knapsack - Maximization/Fractional_Knapsack_usingStructure.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | typedef struct Products 18 | { 19 | string productID; 20 | int weight; 21 | int value; 22 | } Product; 23 | 24 | bool compare(Product a, Product b) /// decending order sort on basis of value/weight 25 | { 26 | // return (a.value / a.weight) > (b.value / b.weight); /// sometimes it creat Precision error 27 | 28 | return (a.value * b.weight) > (b.value * a.weight); /// to avoide Precision error 29 | } 30 | 31 | double fractionalKnapsack_Greedy(vector products, int capacity) 32 | { 33 | double ans = 0; 34 | for (int i = 0; i < products.size(); i++) 35 | { 36 | if (products[i].weight <= capacity) 37 | { 38 | cout << products[i].productID << " "; /// picked product 39 | 40 | ans += products[i].value; 41 | capacity -= products[i].weight; 42 | } 43 | else 44 | { 45 | cout << products[i].productID << " "; /// picked product 46 | 47 | ans += capacity * ((1.0 * products[i].value) / products[i].weight); 48 | capacity = 0; 49 | break; /// capacity is zero so I can't anything more 50 | } 51 | } 52 | 53 | return ans; 54 | } 55 | 56 | void printSortedProducts(vector products) 57 | { 58 | cout << nl << "Sorted Products : "; 59 | for (Product p : products) 60 | { 61 | cout << p.productID << " "; 62 | } 63 | cout << nl; 64 | } 65 | 66 | int32_t main() 67 | { 68 | cout << "Enter num of products: "; 69 | int numOfProducts; 70 | cin >> numOfProducts; 71 | 72 | vector products(numOfProducts); 73 | 74 | cout << "Enter products Id, value and weight : "; 75 | 76 | for (int i = 0; i < numOfProducts; i++) 77 | { 78 | getchar(); 79 | cin >> products[i].productID; 80 | cin >> products[i].value; 81 | cin >> products[i].weight; 82 | } 83 | 84 | cout << "Enter bag size: "; 85 | int bagCapacity; 86 | cin >> bagCapacity; ///----> bag/Knapsack size 87 | 88 | sort(products.begin(), products.end(), compare); /// Sorting based on ther Value / weight 89 | 90 | printSortedProducts(products); 91 | cout << nl; 92 | 93 | double ans = fractionalKnapsack_Greedy(products, bagCapacity); // value in bag 94 | cout << "Value of the bag: " << ans << nl << nl; 95 | 96 | CRACKED; 97 | } 98 | 99 | /*---------> Test Case 1 100 | 101 | 4 102 | 103 | p1 1 1 104 | p2 4 3 105 | p3 5 4 106 | p4 7 5 107 | 108 | 7 109 | 110 | 111 | */ 112 | 113 | /*---------> Test Case 2 114 | 115 | 116 | 5 117 | 118 | P1 21 7 119 | P2 24 4 120 | P3 12 6 121 | P4 40 5 122 | P5 30 6 123 | 124 | 20 125 | 126 | 127 | */ -------------------------------------------------------------------------------- /DSA_Topics/Greedy Algorithoms/Knapsack - Maximization/Knapsack_Fractional.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | bool compare(pair a, pair b) 18 | { 19 | double v1 = (double)a.first / a.second; // value/weight 20 | double v2 = (double)b.first / b.second; // value/weight 21 | 22 | return v1 > v2; 23 | } 24 | 25 | int32_t main() 26 | { 27 | cout << "Enter num of products: "; 28 | int numOfProducts; 29 | cin >> numOfProducts; 30 | 31 | vector> vii; // value weight 32 | 33 | cout << "Enter products value and weight: "; 34 | 35 | for (int i = 0; i < numOfProducts; i++) 36 | { 37 | int a, b; 38 | cin >> a >> b; // value weight 39 | vii.push_back({a, b}); 40 | } 41 | 42 | sort(vii.begin(), vii.end(), compare); 43 | 44 | // for sort check 45 | // for (int i = 0; i < numOfProducts; i++) 46 | // { 47 | // cout << vii[i].first << " " << vii[i].second << nl; 48 | // } 49 | // cout << nl; 50 | 51 | cout << "Enter bag size: "; 52 | int weight; 53 | cin >> weight; // bag size 54 | 55 | int ans = 0; // value in bag 56 | 57 | for (int i = 0; i < numOfProducts; i++) 58 | { 59 | if (vii[i].second <= weight) 60 | { 61 | ans += vii[i].first; 62 | weight -= vii[i].second; 63 | continue; 64 | } 65 | else 66 | { 67 | double value = (double)vii[i].first / vii[i].second; 68 | ans += value * weight; 69 | weight = 0; 70 | break; 71 | } 72 | } 73 | 74 | cout << nl << nl; 75 | cout << "Space remain: " << weight << nl; 76 | cout << "value of the bag: " << ans << nl; 77 | 78 | CRACKED; 79 | } 80 | 81 | 82 | /* 83 | 84 | 5 85 | 86 | 21 7 87 | 24 4 88 | 12 6 89 | 40 5 90 | 30 6 91 | 92 | 20 93 | 94 | 95 | ans: 109 96 | 97 | */ -------------------------------------------------------------------------------- /DSA_Topics/Greedy Algorithoms/Knapsack - Maximization/zeroOne_Knapsack_Greedy_&_Bruteforce__usingStructure.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | typedef struct Products 18 | { 19 | string productID; 20 | int weight; 21 | int value; 22 | } Product; 23 | 24 | bool compare(Product a, Product b) /// ascending order sort on basis of value/weight 25 | { 26 | // return (a.value / a.weight) > (b.value / b.weight); /// spmetimes it creat Precision error 27 | return (a.value * b.weight) > (b.value * a.weight); /// to avoide Precision error 28 | } 29 | 30 | int zeroOneKnapsack_Greedy(vector products, int capacity) 31 | { 32 | int ans = 0; 33 | for (int i = 0; i < products.size(); i++) 34 | { 35 | if (products[i].weight <= capacity) 36 | { 37 | cout << products[i].productID << " "; 38 | ans += products[i].value; 39 | capacity -= products[i].weight; 40 | } 41 | } 42 | 43 | return ans; /// returning the profits 44 | } 45 | 46 | int zeroOneKnapsack_BruteForce(vector products, int capacity, int index) 47 | { 48 | if (capacity == 0 || index == products.size()) /// no product remain or bag capacity = zero 49 | { 50 | return 0; /// returning Profit = 0 51 | } 52 | 53 | if (products[index].weight > capacity) /// Not Take Condition 54 | { 55 | /// Skip the current product 56 | int friend_ = zeroOneKnapsack_BruteForce(products, capacity, index + 1); /// Just Increment Index and Call Friend 57 | return friend_; 58 | } 59 | else 60 | { 61 | /// take the current product 62 | int take = products[index].value; 63 | int nibo_andFriend = zeroOneKnapsack_BruteForce(products, capacity - products[index].weight, index + 1); // calling friend 64 | take += nibo_andFriend; 65 | 66 | /// Skip the current product 67 | int notTake = 0; 68 | int niboNa_andFriend = zeroOneKnapsack_BruteForce(products, capacity, index + 1); // calling friend 69 | notTake += niboNa_andFriend; 70 | 71 | return max(take, notTake); 72 | } 73 | } 74 | 75 | void printSortedProducts(vector products) 76 | { 77 | cout << nl << "Sorted Products : "; 78 | for (Product p : products) 79 | { 80 | cout << p.productID << " "; 81 | } 82 | cout << nl; 83 | } 84 | 85 | int32_t main() 86 | { 87 | cout << "Enter num of products: "; 88 | int numOfProducts; 89 | cin >> numOfProducts; 90 | 91 | vector products(numOfProducts); 92 | 93 | cout << "Enter products Id, value and weight : "; 94 | 95 | for (int i = 0; i < numOfProducts; i++) 96 | { 97 | getchar(); 98 | cin >> products[i].productID; 99 | cin >> products[i].value; 100 | cin >> products[i].weight; 101 | } 102 | 103 | cout << "Enter bag size: "; 104 | int bagCapacity; 105 | cin >> bagCapacity; // bag size 106 | 107 | sort(products.begin(), products.end(), compare); /// Soring in decending order based on value / weight 108 | printSortedProducts(products); 109 | cout << nl; 110 | 111 | int ans; 112 | 113 | /// 114 | /// 115 | /// 116 | 117 | ///------------------------------------ Greedy 118 | cout << "Greedy Approach: " << nl; 119 | cout << "-----------------------" << nl; 120 | ans = zeroOneKnapsack_Greedy(products, bagCapacity); // value in bag 121 | cout << nl; 122 | cout << "Value of the bag: " << ans << nl << nl << nl; 123 | 124 | /// 125 | /// 126 | /// 127 | 128 | ///------------------------------------- Bruteforce 129 | 130 | cout << "BruteForce Approach: " << nl; 131 | cout << "-----------------------" << nl; 132 | ans = zeroOneKnapsack_BruteForce(products, bagCapacity, 0); // value in bag 133 | cout << "Value of the bag: " << ans << nl << nl; 134 | 135 | CRACKED; 136 | } 137 | 138 | /* 139 | 140 | 4 141 | 142 | p1 1 1 143 | p2 4 3 144 | p3 5 4 145 | p4 7 5 146 | 147 | 7 148 | 149 | 150 | */ -------------------------------------------------------------------------------- /DSA_Topics/Greedy Algorithoms/Knapsack - Maximization/zeroOne_Knapsack__Greedy.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | 15 | using namespace std; 16 | 17 | bool compare(pair a, pair b) 18 | { 19 | double v1 = (double)a.first / a.second; // value/weight 20 | double v2 = (double)b.first / b.second; // value/weight 21 | 22 | return v1 > v2; 23 | } 24 | 25 | pair zeroOneKnapsack(vector> vii, int bagCapacity) 26 | { 27 | sort(vii.begin(), vii.end(), compare); 28 | 29 | //// for sort check 30 | // for (int i = 0; i < numOfProducts; i++) 31 | // cout << vii[i].first << " " << vii[i].second << nl; 32 | // cout << nl; 33 | 34 | pair solve; /// just for return Type 35 | 36 | int profit = 0; 37 | 38 | for (int i = 0; i < vii.size(); i++) 39 | { 40 | if (vii[i].second > bagCapacity) 41 | { 42 | continue; 43 | } 44 | else if (vii[i].second <= bagCapacity) 45 | { 46 | profit += vii[i].first; 47 | bagCapacity -= vii[i].second; 48 | } 49 | } 50 | 51 | solve.first = bagCapacity; /// Bag remain capacity 52 | solve.second = profit; /// total Profit 53 | 54 | return solve; 55 | } 56 | 57 | int32_t main() 58 | { 59 | cout << "Enter num of products: "; 60 | int numOfProducts; 61 | cin >> numOfProducts; 62 | 63 | vector> vii; // value & weight 64 | 65 | cout << "Enter products value and weight: "; 66 | 67 | for (int i = 0; i < numOfProducts; i++) 68 | { 69 | int a, b; 70 | cin >> a >> b; // value & weight 71 | vii.push_back({a, b}); 72 | } 73 | 74 | cout << "Enter bag size: "; 75 | int bagCapacity; 76 | cin >> bagCapacity; // bag size 77 | 78 | pair solve = zeroOneKnapsack(vii, bagCapacity); /// call zeroOne Knapsack Func 79 | 80 | cout << nl << nl; 81 | cout << "Space remain: " << solve.first << nl; 82 | cout << "value of the bag: " << solve.second << nl; 83 | 84 | CRACKED; 85 | } 86 | 87 | /* 88 | 89 | 4 90 | 91 | 1 1 92 | 4 3 93 | 5 4 94 | 7 5 95 | 96 | 7 97 | 98 | 99 | 100 | */ -------------------------------------------------------------------------------- /DSA_Topics/String Algorithoms/Knuth-Morris-Pratt__KMP_Algo__.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debt cout << "Tashin " << endl; // debug code 15 | 16 | using namespace std; 17 | 18 | vector createLPS(string pattern) /// Working correctly 19 | { 20 | /// acacabacacab 21 | vector lps(pattern.length(), 0); /// Storing the position of the matched char 22 | 23 | int len = 0; /// Length of the previous longest prefix suffix 24 | int i = 1; /// iterator 25 | 26 | while (i < pattern.length()) 27 | { 28 | if (pattern[i] == pattern[len]) 29 | { 30 | lps[i] = (len + 1); 31 | len++; 32 | i++; 33 | } 34 | else 35 | { 36 | if (len != 0) 37 | { 38 | /* 39 | index = index-1; 40 | index = lps[index] +1; 41 | */ 42 | 43 | len = lps[len - 1]; 44 | } 45 | else 46 | { 47 | lps[i] = (0); 48 | i++; 49 | } 50 | } 51 | } 52 | 53 | return lps; 54 | } 55 | 56 | void KMP_algo(string text, string pattern) 57 | { 58 | vector lps = createLPS(pattern); 59 | 60 | int i = 0; /// iterate --> text 61 | int j = 0; /// iterate --> pattern 62 | 63 | while (i < text.length()) 64 | { 65 | if (text[i] == pattern[j]) 66 | { 67 | i++; 68 | j++; 69 | } 70 | else 71 | { 72 | if (j == 0) 73 | i++; 74 | else 75 | j = lps[j - 1]; 76 | } 77 | 78 | if (j == pattern.length()) 79 | { 80 | cout << "Find Match" << nl; 81 | break; 82 | } 83 | } 84 | } 85 | 86 | int32_t main() 87 | { 88 | string text, pattern; 89 | getline(cin, text); 90 | getline(cin, pattern); 91 | 92 | KMP_algo(text, pattern); 93 | 94 | CRACKED; 95 | } 96 | 97 | /* 98 | 99 | Pattern: acacabacacabacacac /// note's LPS Logic 100 | 101 | 102 | Pattern: abaabcabsascsaabaaabcabba 103 | 104 | */ -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/00. SPL review.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/72153120b926011b4bae85b1ec1cacbe176e3d3c/LAB_Code/Class_1 (SPL Review Solution)/00. SPL review.pdf -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/Avarage_of_n_NUMBERS_upto_2nd_Decimal.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define setDec(x) fixed << setprecision(x) 8 | using namespace std; 9 | 10 | int32_t main() 11 | { 12 | 13 | int n; 14 | cin >> n; 15 | int sum = 0; 16 | for (int i = 0; i < n; i++) 17 | { 18 | int a; 19 | cin >> a; 20 | sum += a; 21 | } 22 | 23 | cout << setDec(2) << sum / (n * 1.0) << nl; 24 | 25 | CRACKED; 26 | } 27 | -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/Char_Input_Untill_A.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define setDec(x) fixed << setprecision(x) 8 | using namespace std; 9 | 10 | int32_t main() 11 | { 12 | 13 | char c; 14 | while (cin >> c) 15 | { 16 | 17 | if (c == 'A') 18 | break; 19 | else 20 | cout << c << nl; 21 | } 22 | CRACKED; 23 | } 24 | -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/Number_Frequency_and_Distinct _Number.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | int32_t main() 11 | { 12 | int arr[10000] = {0}; 13 | int n; 14 | cout << "Enter Total number: "; 15 | cin >> n; 16 | int a; 17 | cout << "Enter Numbers: "; 18 | for (int i = 0; i < n; i++) 19 | { 20 | cin >> a; 21 | arr[a]++; 22 | } 23 | 24 | for (int i = 0; i < 10000; i++) 25 | { 26 | if (arr[i] > 0) 27 | cout << i << ","; 28 | } 29 | 30 | cout << nl; 31 | cout << nl; 32 | for (int i = 0; i < 10000; i++) 33 | { 34 | if (arr[i] > 0) 35 | cout << i << " " << arr[i] << nl; 36 | } 37 | 38 | CRACKED; 39 | } 40 | -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/QN_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Employee 5 | { 6 | public: 7 | string name; 8 | int height; 9 | int age; 10 | 11 | Employee(string name, int height, int age) 12 | { 13 | this->name = name; 14 | this->height = height; 15 | this->age = age; 16 | } 17 | 18 | public: 19 | Employee() 20 | { 21 | } 22 | }; 23 | 24 | bool comparator(Employee a, Employee b) 25 | { 26 | if (a.height < b.height) 27 | return false; 28 | else if (a.height > b.height) 29 | return true; 30 | else 31 | { 32 | if (a.age < b.age) 33 | return false; 34 | else 35 | return true; 36 | } 37 | } 38 | 39 | void printVector(vector employees) 40 | { 41 | for (Employee e : employees) 42 | { 43 | cout << e.name << " " << e.height << " " << e.age; 44 | cout << endl; 45 | } 46 | cout << endl; 47 | } 48 | 49 | int main() 50 | { 51 | 52 | int n; 53 | cin >> n; 54 | 55 | vector employees(n); 56 | 57 | for (int i = 0; i < n; i++) 58 | { 59 | getchar(); 60 | getline(cin, employees[i].name); 61 | cin >> employees[i].height >> employees[i].age; 62 | } 63 | 64 | sort(employees.begin(), employees.end(), comparator); 65 | printVector(employees); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/QN_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Employee 4 | { 5 | public: 6 | string name; 7 | int id; 8 | int salary; 9 | 10 | Employee(string name, int id, int salary) 11 | { 12 | this->name = name; 13 | this->id = id; 14 | this->salary = salary; 15 | } 16 | 17 | Employee() 18 | { 19 | } 20 | }; 21 | 22 | bool comparator(Employee a, Employee b) 23 | { 24 | if (a.salary < b.salary) 25 | return true; 26 | else if (a.salary > b.salary) 27 | return false; 28 | else 29 | { 30 | if (a.id < b.id) 31 | return true; 32 | else 33 | return false; 34 | } 35 | } 36 | 37 | void printVector(vector employees) 38 | { 39 | for (Employee e : employees) 40 | { 41 | cout << e.name << " " << e.salary << " " << e.id; 42 | cout << endl; 43 | } 44 | cout << endl; 45 | } 46 | 47 | int main() 48 | { 49 | freopen("input.txt", "r", stdin); 50 | 51 | int n; 52 | cin >> n; 53 | getchar(); 54 | 55 | vector employees(n); 56 | 57 | for (int i = 0; i < n; i++) 58 | { 59 | getline(cin, employees[i].name); 60 | } 61 | 62 | for (int i = 0; i < n; i++) 63 | { 64 | cin >> employees[i].id; 65 | } 66 | 67 | for (int i = 0; i < n; i++) 68 | { 69 | cin >> employees[i].salary; 70 | } 71 | 72 | printVector(employees); 73 | 74 | sort(employees.begin(), employees.end(), comparator); 75 | 76 | printVector(employees); 77 | return 0; 78 | } -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/converts_the_sentence_into_lowercase_and_removes_all_whitespace_using_Recusion_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | string func(string s, int i, int strlen) 11 | { 12 | if (i == strlen) 13 | return ""; 14 | else if (s[i] == ' ') 15 | return "" + func(s, i + 1, strlen); 16 | else if (s[i] >= 'A' && s[i] <= 'Z') 17 | return (char)('a' + (s[i] - 'A')) + func(s, i + 1, strlen); 18 | else 19 | return s[i] + func(s, i + 1, strlen); 20 | } 21 | 22 | int32_t main() 23 | { 24 | string s; 25 | getline(cin, s); 26 | 27 | cout << func(s, 0, s.length()) << nl; 28 | 29 | CRACKED; 30 | } 31 | -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/print_all_the_factorials_of_numbers_within__number_a__number_b___.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | void factorial(int n) 11 | { 12 | cout << n << "! = "; 13 | int multiple = 1; 14 | for (int i = n; i >= 1; i--) 15 | { 16 | multiple *= i; 17 | if (i == 1) 18 | cout << i << " "; 19 | else 20 | cout << i << " X "; 21 | } 22 | cout << " = " << multiple << nl; 23 | 24 | return; 25 | } 26 | 27 | int32_t main() 28 | { 29 | int a, b; 30 | cin >> a >> b; 31 | for (int i = a; i <= b; i++) 32 | { 33 | factorial(i); 34 | } 35 | 36 | CRACKED; 37 | } 38 | -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/prints_all_the_unique_elements_of_an_array__.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | int n; 13 | cin >> n; 14 | int arr[n]; 15 | for (int i = 0; i < n; i++) 16 | { 17 | cin >> arr[i]; 18 | } 19 | 20 | for (int i = 0; i < n; i++) 21 | { 22 | int currentElement = arr[i]; 23 | bool previouslyFound = false; 24 | 25 | for (int j = 0; j <= (i - 1); j++) 26 | { 27 | if (arr[j] == currentElement) 28 | { 29 | previouslyFound = true; 30 | break; 31 | } 32 | } 33 | 34 | if (previouslyFound == false) 35 | cout << currentElement << " "; 36 | } 37 | cout << nl; 38 | CRACKED; 39 | } 40 | -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/prints_the_number_of_occurrences_of_each_unique_elements_of_an_array_.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin >> n; 7 | int arr[n]; 8 | 9 | for (int i = 0; i < n; i++) 10 | cin >> arr[i]; 11 | 12 | for (int i = 0; i < n; i++) 13 | { 14 | int currentElement = arr[i]; 15 | bool previouslyFound = false; 16 | 17 | for (int j = 0; j <= i - 1; j++) 18 | { 19 | if (arr[j] == currentElement) 20 | { 21 | previouslyFound = true; 22 | break; 23 | } 24 | } 25 | 26 | if (previouslyFound == false) 27 | { 28 | int counter = 0; 29 | for (int j = i; j < n; j++) 30 | { 31 | if (arr[j] == currentElement) 32 | counter++; 33 | } 34 | cout << currentElement << " occurs " << counter << " times\n"; 35 | } 36 | } 37 | cout << "\n"; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /LAB_Code/Class_1 (SPL Review Solution)/take_as_input_a_string__and_toggle_cases_of_all_the_letters__.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | string toggle(string s, int i, int strlen) 11 | { 12 | if (i == strlen) 13 | return ""; 14 | else if (s[i] >= 'A' && s[i] <= 'Z') 15 | return (char)('a' + (s[i] - 'A')) + toggle(s, i + 1, strlen); 16 | else if (s[i] >= 'a' && s[i] <= 'z') 17 | return (char)('A' + (s[i] - 'a')) + toggle(s, i + 1, strlen); 18 | else 19 | return s[i] + toggle(s, i + 1, strlen); 20 | } 21 | 22 | int32_t main() 23 | { 24 | string s; 25 | getline(cin, s); 26 | 27 | cout << toggle(s, 0, s.length()) << nl; 28 | 29 | CRACKED; 30 | } 31 | -------------------------------------------------------------------------------- /LAB_Code/Class_2 STL/Vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bool comparator(int a, int b) 8 | { 9 | if (a >= b) 10 | return true; 11 | else 12 | return false; 13 | } 14 | 15 | int main() 16 | { 17 | 18 | vector myVector; 19 | 20 | /// insert Last 21 | myVector.push_back(10); 22 | myVector.push_back(15); 23 | myVector.push_back(12); 24 | myVector.push_back(17); 25 | myVector.push_back(22); 26 | myVector.push_back(5); 27 | myVector.push_back(8); 28 | myVector.push_back(2); 29 | myVector.push_back(150); 30 | 31 | /// size of the vector 32 | int SIZE = myVector.size(); 33 | 34 | /// printing the vector 35 | for (int i = 0; i < SIZE; i++) 36 | cout << myVector[i] << " "; 37 | cout << endl; 38 | 39 | /// sorting the vector 40 | 41 | // sort(myVector.begin(), myVector.begin() + 3); // sort only first 3 elements 42 | 43 | // for (int i = 0; i < SIZE; i++) // printing the vector 44 | // cout << myVector[i] << " "; 45 | // cout << endl; 46 | 47 | /// sort(myVector.begin(), myVector.end()); // ascending order sort 48 | 49 | // for (int i = 0; i < SIZE; i++) // printing the vector 50 | // cout << myVector[i] << " "; 51 | // cout << endl; 52 | 53 | sort(myVector.begin(), myVector.end(), comparator); // decending order 54 | 55 | /// printing the vector 56 | for (int i = 0; i < SIZE; i++) 57 | cout << myVector[i] << " "; 58 | cout << endl; 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/Calculate_Power_of_a_Number_Using_recursion.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | double calculatePower(int a, int b) 10 | { 11 | if (b == 0) 12 | return 1; 13 | else if (b < 0) 14 | { 15 | b*=-1; 16 | return 1/(calculatePower(a, b)); 17 | } 18 | else 19 | return a * calculatePower(a, b - 1); 20 | } 21 | 22 | int32_t main() 23 | { 24 | int a, b; 25 | cin >> a >> b; 26 | 27 | double ans = calculatePower(a, b); 28 | cout << "a to the power b = " << ans << nl; 29 | 30 | CRACKED; 31 | } 32 | -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS01_Prints_the_numbers_from___to_n__.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | void print1toN(int i, int n) 10 | { 11 | if (i > n) 12 | return; 13 | else 14 | { 15 | cout << i << nl; 16 | return print1toN(i + 1, n); 17 | } 18 | } 19 | 20 | void UsingOnlyN(int n) 21 | { 22 | if (n == 0) 23 | return; 24 | else 25 | { 26 | UsingOnlyN(n - 1); 27 | cout << n << nl; 28 | } 29 | } 30 | 31 | int32_t main() 32 | { 33 | int n; 34 | cin >> n; 35 | 36 | print1toN(1, n); 37 | cout << nl; 38 | cout << nl; 39 | UsingOnlyN(n); 40 | 41 | CRACKED; 42 | } -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS02_Calculate_the_sum_of_numbers_from___to_n___.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int Sum(int n) 10 | { 11 | if (n == 0) 12 | return 0; 13 | else 14 | return n + Sum(n - 1); 15 | } 16 | 17 | int32_t main() 18 | { 19 | int n; 20 | cin >> n; 21 | 22 | int ans = Sum(n); 23 | cout << "Ans " << ans << nl; 24 | 25 | CRACKED; 26 | } -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS03_Calculate_the_factorial_of_n___.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int factorial(int n) 10 | { 11 | if (n == 0) 12 | return 1; 13 | else 14 | return n * factorial(n - 1); 15 | } 16 | 17 | int32_t main() 18 | { 19 | int n; 20 | cin >> n; 21 | 22 | int ans = factorial(n); 23 | cout << "Ans " << ans << nl; 24 | 25 | CRACKED; 26 | } -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS04__Calculate_the_sum_of_digits_of_a_given_number_n__.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int SumOfDigits(int n) 10 | { 11 | if (n / 10 == 0) 12 | return n; 13 | else 14 | { 15 | return (n % 10) + SumOfDigits(n / 10); 16 | } 17 | } 18 | 19 | int32_t main() 20 | { 21 | int n; 22 | cin >> n; 23 | 24 | int ans = SumOfDigits(n); 25 | cout << "SumOfDigits = " << ans << nl; 26 | 27 | CRACKED; 28 | } 29 | -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS05.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int CountDigits(int n) 10 | { 11 | if (n / 10 == 0) 12 | return 1; 13 | else 14 | { 15 | return 1 + CountDigits(n / 10); 16 | } 17 | } 18 | 19 | int32_t main() 20 | { 21 | int n; 22 | cin >> n; 23 | 24 | int ans = CountDigits(n); 25 | cout << "CountDigits = " << ans << nl; 26 | 27 | CRACKED; 28 | } 29 | -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS06.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int Fibonacci(int n) 10 | { 11 | if (n == 1) 12 | { 13 | return 0; 14 | } 15 | if (n == 2) 16 | return 1; 17 | 18 | return Fibonacci(n - 1) + Fibonacci(n - 2); 19 | } 20 | 21 | int32_t main() 22 | { 23 | int n; 24 | cin >> n; 25 | 26 | cout << "Fibonacci = " << Fibonacci(n) << nl; 27 | 28 | CRACKED; 29 | } 30 | -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS07.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int calculatePower(int a, int b) 10 | { 11 | if (b == 0) 12 | return 1; 13 | else 14 | return a * calculatePower(a, b - 1); 15 | } 16 | 17 | int32_t main() 18 | { 19 | int a, b; 20 | cin >> a >> b; 21 | 22 | int ans = calculatePower(a, b); 23 | cout << "a to the power b = " << ans << nl; 24 | 25 | CRACKED; 26 | } 27 | -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS08.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | void printArray(int i, int n, int arr[]) 10 | { 11 | if (i == n) 12 | return; 13 | else 14 | { 15 | cout << arr[i] << " "; 16 | printArray(i + 1, n, arr); 17 | } 18 | } 19 | 20 | int32_t main() 21 | { 22 | int n; 23 | cin >> n; 24 | int arr[n]; 25 | for (int i = 0; i < n; i++) 26 | cin >> arr[i]; 27 | 28 | printArray(0, sizeof(arr) / sizeof(arr[0]), arr); 29 | 30 | CRACKED; 31 | } 32 | -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int largestElement(int arr[], int currentIndex) 5 | { 6 | if (currentIndex == 0) 7 | return arr[currentIndex]; 8 | else 9 | { 10 | int currentValue = arr[currentIndex]; 11 | int friend_ = largestElement(arr, currentIndex - 1); 12 | return max(currentValue, friend_); 13 | } 14 | } 15 | 16 | int main() 17 | { 18 | int arr[10] = {6, 9, 8, 4, 5, 1, 2, 3, 4, 5}; 19 | int n = 10; 20 | 21 | int result = largestElement(arr, n - 1); 22 | 23 | cout << result << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int smallestElement(int arr[], int currentIndex) 5 | { 6 | if (currentIndex == 0) 7 | return arr[currentIndex]; 8 | else 9 | { 10 | int currentValue = arr[currentIndex]; 11 | int friend_ = smallestElement(arr, currentIndex - 1); 12 | return min(currentValue, friend_); 13 | } 14 | } 15 | 16 | int main() 17 | { 18 | int arr[10] = {6, 9, 8, 4, 5, 1, 2, 3, 4, 5}; 19 | int n = 10; 20 | int result = smallestElement(arr, n - 1); 21 | cout << result << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct MinMax 4 | { 5 | int minimum; 6 | int maximum; 7 | }; 8 | 9 | MinMax minMaxElement(int arr[], int currentIndex) 10 | { 11 | if (currentIndex == 0) 12 | return {arr[currentIndex], arr[currentIndex]}; 13 | else 14 | { 15 | int currentValue = arr[currentIndex]; 16 | MinMax friend_ = minMaxElement(arr, currentIndex - 1); 17 | MinMax result = friend_; 18 | 19 | if (currentValue < result.minimum) 20 | result.minimum = currentValue; 21 | else if (currentValue > result.maximum) 22 | result.maximum = currentValue; 23 | return result; 24 | } 25 | } 26 | 27 | int main() 28 | { 29 | int arr[10] = {6, 9, 8, 4, 5, 1, 2, 3, 4, 5}; 30 | int n = 10; 31 | 32 | MinMax result = minMaxElement(arr, n - 1); 33 | 34 | cout << result.minimum << " " << result.maximum << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/RPS_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPalindrome(string str, int left, int right) 5 | { 6 | if (left >= right) 7 | return true; 8 | else 9 | { 10 | if (str[left] == str[right]) 11 | { 12 | bool friend_ = isPalindrome(str, left + 1, right - 1); 13 | return friend_; 14 | } 15 | else 16 | return false; 17 | } 18 | } 19 | 20 | int main() 21 | { 22 | string str = "madam"; 23 | bool result = isPalindrome(str, 0, str.size() - 1); 24 | if (result == true) 25 | cout << "Palindrome\n"; 26 | else 27 | cout << "Not a Palindrome\n"; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/Reverse_a_Number_uning_recursion.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | #define setDec(x) fixed << setprecision(x) 8 | 9 | using namespace std; 10 | int reverse(int n, int ans) 11 | { 12 | int temp = n % 10; 13 | ans *= 10; 14 | ans += temp; 15 | n = n / 10; 16 | if (n == 0) 17 | return ans; 18 | else 19 | reverse(n, ans); 20 | } 21 | 22 | int32_t main() 23 | { 24 | int n; 25 | cout << "Enter a Number: "; 26 | cin >> n; 27 | cout << "Reverse Number: "; 28 | cout << reverse(n, 0) << nl; 29 | 30 | CRACKED; 31 | } 32 | -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/[Template] Recursion Problem List.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/72153120b926011b4bae85b1ec1cacbe176e3d3c/LAB_Code/Class_3_(Recursion)/[Template] Recursion Problem List.pdf -------------------------------------------------------------------------------- /LAB_Code/Class_3_(Recursion)/converts_the_sentence_into_lowercase_and_removes_all_whitespace_using_Recusion_.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 18-08-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl // NewLine 7 | #define null NULL 8 | using namespace std; 9 | 10 | string func(string s, int i, int strlen) 11 | { 12 | if (i == strlen) 13 | return ""; 14 | else if (s[i] == ' ') 15 | return "" + func(s, i + 1, strlen); 16 | else if (s[i] >= 'A' && s[i] <= 'Z') 17 | return (char)('a' + (s[i] - 'A')) + func(s, i + 1, strlen); 18 | else 19 | return s[i] + func(s, i + 1, strlen); 20 | } 21 | 22 | int32_t main() 23 | { 24 | string s; 25 | getline(cin, s); 26 | 27 | cout << func(s, 0, s.length()) << nl; 28 | 29 | CRACKED; 30 | } 31 | -------------------------------------------------------------------------------- /LAB_Code/Class_5/return_min_Max.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | pair minMax(vector &arr, int i) 10 | { 11 | int mn = arr[i]; 12 | int mx = arr[i]; 13 | 14 | if (i == arr.size() - 1) 15 | return {mx, mn}; 16 | else 17 | { 18 | return {max(mx, minMax(arr, i + 1).first), min(mn, minMax(arr, i + 1).second)}; 19 | } 20 | } 21 | 22 | int32_t main() 23 | { 24 | int n; 25 | cin >> n; 26 | vector arr; 27 | 28 | for (int i = 0; i < n; i++) 29 | { 30 | int a; 31 | cin >> a; 32 | arr.push_back(a); 33 | } 34 | 35 | pair ans = minMax(arr, 0); 36 | 37 | cout << "max " << ans.first << nl; 38 | cout << "min " << ans.second << nl; 39 | 40 | CRACKED; 41 | } -------------------------------------------------------------------------------- /LAB_Code/Class_6/Coin_Change_Greedy_GivenCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool compare(int a, int b) 5 | { 6 | if (a > b) 7 | return true; 8 | else 9 | return false; 10 | } 11 | 12 | void printVector(vector coins) 13 | { 14 | for (int x : coins) 15 | { 16 | cout << x << " "; 17 | } 18 | cout << endl; 19 | } 20 | int coinChange(vector coins, int amount) 21 | { 22 | sort(coins.begin(), coins.end(), compare); 23 | 24 | int remainingAmount = amount; 25 | int minimumNumberOfCoins = 0; 26 | 27 | for (int i = 0; i < coins.size(); i++) 28 | { 29 | int currentCoin = coins[i]; 30 | int howManyOfCurrentCoin = remainingAmount / currentCoin; 31 | remainingAmount = remainingAmount % currentCoin; 32 | minimumNumberOfCoins += howManyOfCurrentCoin; 33 | 34 | cout << currentCoin << " -> " << howManyOfCurrentCoin << endl; 35 | } 36 | 37 | return minimumNumberOfCoins; 38 | } 39 | int main() 40 | { 41 | vector coins{5, 2, 1, 20, 50}; 42 | int result = coinChange(coins, 157); 43 | cout << result << endl; 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /LAB_Code/Class_6/Coin_change_BruteForce.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez **************** 2 | // ************* United International University ************* 3 | // **************** Updated: 27/10/23 **************** 4 | 5 | #include 6 | #define CRACKED return 0; 7 | #define nl endl // NewLine 8 | #define null NULL 9 | #define sp " " // space 10 | #define setDec(x) fixed << setprecision(x) 11 | #define len(s) s.length() 12 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 13 | 14 | using namespace std; 15 | 16 | // int coinChange(vector coins, int amount) 17 | // { 18 | // if (amount == 0) 19 | // return 0; 20 | 21 | // int numberOfCoins = INT_MAX; 22 | // for (int i = 0; i < coins.size(); i++) 23 | // { 24 | // int currentCoin = coins[i]; 25 | // if (currentCoin > amount) // for example: coin = 25, amount = 20 26 | // continue; 27 | // else 28 | // { 29 | // int remainingAmount = amount - currentCoin; 30 | // int friend_ = 1 + coinChange(coins, remainingAmount); 31 | 32 | // if (numberOfCoins > friend_) 33 | // numberOfCoins = friend_; 34 | // } 35 | // } 36 | // return numberOfCoins; 37 | // } 38 | 39 | int coinChange(int it, vector coins, int amount) 40 | { 41 | 42 | cout << "RUNNING" << endl; 43 | 44 | if (amount == 0 || it == coins.size()) 45 | { 46 | cout << "it = " << it << " "; 47 | deb(amount); 48 | return 0; 49 | } 50 | else 51 | { 52 | int currentCoin = coins[it]; 53 | 54 | if (currentCoin <= amount) /// have 2 case 55 | { 56 | cout << nl << "TASHIN ----------->> SMALL CASE" << endl; 57 | int friend1 = 1 + coinChange(it, coins, amount - currentCoin); // take 58 | int friend2 = coinChange(it + 1, coins, amount); // not take 59 | 60 | cout << "Friend1 = " << friend1 << " Friend2 = " << friend2 << nl; 61 | cout << "Friend_ = " << min(friend1, friend2) << " <<---------------------------------- this will return " << nl; 62 | cout<> BIG CASE" << endl; 68 | int friend1 = coinChange(it + 1, coins, amount); // take 69 | cout << "Friend1 = " << friend1 << " <<------------------------------------------------ this will return " << nl; 70 | cout< amount) // for example: coin = 25, amount = 20 80 | // continue; 81 | // else 82 | // { 83 | // int remainingAmount = amount - currentCoin; 84 | // int friend_ = 1 + coinChange(coins, remainingAmount); 85 | 86 | // if (numberOfCoins > friend_) 87 | // numberOfCoins = friend_; 88 | // } 89 | // } 90 | // return numberOfCoins; 91 | } 92 | 93 | int main() 94 | { 95 | vector coins{25, 10, 1}; 96 | // int result = coinChange(coins, 157); 97 | 98 | int result = coinChange(0, coins, 30); 99 | cout << result << endl; 100 | return 0; 101 | } -------------------------------------------------------------------------------- /LAB_Code/Class_6/else.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int coinChange(vector coins, int amount) 5 | { 6 | // cout << "TASHIN" << endl; 7 | if (amount == 0) 8 | return 0; 9 | 10 | int numberOfCoins = INT_MAX; 11 | 12 | for (int i = 0; i < coins.size(); i++) 13 | { 14 | int currentCoin = coins[i]; 15 | if (currentCoin > amount) // for example: coin = 25, amount = 20 16 | continue; 17 | else 18 | { 19 | int remainingAmount = amount - currentCoin; 20 | int friend_ = 1 + coinChange(coins, remainingAmount); 21 | 22 | if (numberOfCoins > friend_) 23 | numberOfCoins = friend_; 24 | } 25 | } 26 | return numberOfCoins; 27 | } 28 | 29 | int main() 30 | { 31 | vector coins{50, 30, 10, 5, 2}; 32 | int result = coinChange(coins, 10); 33 | cout << result << endl; 34 | return 0; 35 | } -------------------------------------------------------------------------------- /LAB_Code/Lab Final Fall23 Sec_B/Q1.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debt cout << "Tashin " << endl; // debug code 15 | 16 | using namespace std; 17 | 18 | bool cmp(pair, int> a, pair, int> b) 19 | { 20 | return a.second < b.second; 21 | } 22 | 23 | int parent[10000]; 24 | int rank__[10000]; 25 | 26 | void makeset(int v) 27 | { 28 | for (int i = 0; i < v; i++) 29 | { 30 | parent[i] = i; 31 | rank__[i] = 0; 32 | } 33 | } 34 | 35 | int findParent(int n) 36 | { 37 | if (parent[n] == n) 38 | return n; 39 | else 40 | { 41 | int frie = findParent(parent[n]); 42 | parent[n] = frie; 43 | return frie; 44 | } 45 | } 46 | 47 | bool unionSet(int u, int v) 48 | { 49 | u = findParent(u); 50 | v = findParent(v); 51 | 52 | if (u == v) 53 | return true; /// cant take 54 | else 55 | { 56 | if (rank__[u] > rank__[v]) 57 | { 58 | parent[v] = u; 59 | } 60 | else if (rank__[v] > rank__[u]) 61 | { 62 | parent[u] = v; 63 | } 64 | else 65 | { 66 | rank__[u]++; 67 | parent[v] = u; 68 | } 69 | return false; /// take this 70 | } 71 | } 72 | 73 | bool kruskalsAlgo(vector, int>> &edges, int v, int amount) 74 | { 75 | sort(edges.begin(), edges.end(), cmp); 76 | 77 | makeset(v); 78 | 79 | int gopalCost = 0; 80 | 81 | for (auto i : edges) 82 | { 83 | int u = i.first.first; 84 | int v = i.first.second; 85 | 86 | bool take = unionSet(u, v); 87 | 88 | if (take == false) 89 | { 90 | // cout << u << "--> " << v << nl; // debug 91 | gopalCost += i.second; 92 | } 93 | } 94 | 95 | if (gopalCost <= amount) 96 | { 97 | // cout << gopalCost << nl; // debug 98 | return true; 99 | } 100 | else 101 | return false; 102 | } 103 | 104 | int32_t main() 105 | { 106 | 107 | int v, e; 108 | cin >> v >> e; 109 | vector, int>> edges; 110 | 111 | for (int i = 0; i < e; i++) 112 | { 113 | int x, y, w; 114 | cin >> x >> y >> w; 115 | edges.push_back({{x, y}, w}); 116 | } 117 | 118 | int amount; 119 | cin >> amount; 120 | 121 | bool output_ = kruskalsAlgo(edges, v, amount); 122 | 123 | if (output_ == true) 124 | { 125 | cout << "YES" << nl; 126 | } 127 | else 128 | { 129 | cout << "NO" << nl; 130 | } 131 | 132 | CRACKED; 133 | } 134 | 135 | /* 136 | 137 | 4 5 138 | 139 | 0 1 200 140 | 1 2 700 141 | 0 3 50 142 | 1 2 800 143 | 1 3 100 144 | 145 | 900 146 | 147 | 148 | */ 149 | 150 | /* 151 | 152 | 4 5 153 | 0 1 200 154 | 0 2 700 155 | 0 3 50 156 | 1 2 800 157 | 1 3 100 158 | 159 | 500 160 | 161 | */ -------------------------------------------------------------------------------- /LAB_Code/Lab Final Fall23 Sec_B/Q2.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debt cout << "Tashin " << endl; // debug code 15 | 16 | using namespace std; 17 | 18 | int dpTable[1000][1000]; /// just use for total_use_Coin 19 | 20 | #define notset -1 21 | 22 | pair coinChange(vector coins, int amount_needTo_make, int coinsItake, int totalCoinsCanTake) 23 | { 24 | 25 | pair ans; 26 | 27 | if (dpTable[amount_needTo_make][coinsItake] != notset) 28 | { 29 | return {0, dpTable[amount_needTo_make][coinsItake]}; 30 | } 31 | 32 | int createamount = -1; 33 | int usedCoins = coinsItake; 34 | 35 | ans.first = createamount; 36 | ans.second = usedCoins; 37 | 38 | if (amount_needTo_make == 0) 39 | { 40 | 41 | pair ans; 42 | return {0, coinsItake}; 43 | 44 | /// amount_needTo_make , tatal_used_coin 45 | /// 0 , 0 46 | 47 | // return 1; /// normal coin change 48 | } 49 | 50 | if (coinsItake > totalCoinsCanTake) 51 | { 52 | dpTable[amount_needTo_make][coinsItake] = 100000; 53 | 54 | return {0, dpTable[amount_needTo_make][coinsItake]}; 55 | } 56 | 57 | for (auto i : coins) 58 | { 59 | if (i > amount_needTo_make) 60 | { 61 | continue; 62 | } 63 | else 64 | { 65 | // int remainAmount = amount_needTo_make - i; 66 | // int callfriend_ = coinChange(coins, remainAmount, totalCoinsCanTake); 67 | // int takecoins = 1+ callfriend_; 68 | 69 | int remainAmount = amount_needTo_make - i; 70 | 71 | pair callfriend_ = coinChange(coins, remainAmount, coinsItake + 1, totalCoinsCanTake); 72 | 73 | if (callfriend_.first != 0) 74 | { 75 | continue; 76 | } 77 | else /// able to make needed amount 78 | { 79 | if (callfriend_.second <= totalCoinsCanTake) 80 | { 81 | ans.second = callfriend_.second; 82 | 83 | dpTable[amount_needTo_make][coinsItake] = ans.second; 84 | 85 | break; 86 | } 87 | } 88 | } 89 | } 90 | 91 | return ans; 92 | } 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | bool cmp(int a, int b) 105 | { 106 | return a > b; 107 | } 108 | 109 | int32_t main() 110 | { 111 | int m, n; 112 | cin >> m >> n; 113 | 114 | vector coins; 115 | for (int i = 0; i < n; i++) 116 | { 117 | int a; 118 | cin >> a; 119 | coins.push_back(a); 120 | } 121 | int d; 122 | cin >> d; 123 | 124 | sort(coins.begin(), coins.end(), cmp); 125 | 126 | memset(dpTable, notset, sizeof(dpTable)); 127 | 128 | pair ans = coinChange(coins, m, 0, d); 129 | 130 | if (ans.first == 0 && ans.second <= d) 131 | { 132 | cout << "YES" << nl; 133 | } 134 | else 135 | cout << "NO" << nl; 136 | 137 | CRACKED; 138 | } 139 | 140 | /* 141 | 100 5 142 | 25 25 25 25 50 143 | 2 144 | */ 145 | 146 | /* 147 | 148 | 100 5 149 | 25 25 25 25 50 150 | 4 151 | 152 | 153 | */ -------------------------------------------------------------------------------- /Lab Question + Assignment Solution/ASSG_3__Sec_J/QSN1_BruteForce.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debt cout << "Tashin " << endl; // debug code 15 | 16 | using namespace std; 17 | 18 | struct EdgeInfo 19 | { 20 | int u; 21 | int v; 22 | int weight; 23 | }; 24 | bool compare(EdgeInfo a, EdgeInfo b) 25 | { 26 | return a.weight < b.weight; 27 | } 28 | 29 | int32_t main() 30 | { 31 | int numOfVertices, numofEdge; 32 | cin >> numOfVertices >> numofEdge; /// input 33 | 34 | vector edges; 35 | 36 | for (int i = 0; i < numofEdge; i++) /// taking u v and w 37 | { 38 | EdgeInfo edge; 39 | cin >> edge.u >> edge.v >> edge.weight; 40 | edges.push_back(edge); 41 | } 42 | 43 | sort(edges.begin(), edges.end(), compare); /// sort based on their weight 44 | 45 | /// print to check sort func 46 | // for (auto i : edges) 47 | // { 48 | // cout << i.weight << nl; 49 | // } 50 | 51 | cout << nl << "Edges: " << nl; 52 | vector visited(numOfVertices, 0); 53 | 54 | int cnt = 0; 55 | int i = 0; /// to iterate edges 56 | int cost = 1; /// cost 57 | 58 | while (cnt < numOfVertices) 59 | { 60 | if (visited[edges[i].u] == 0 || visited[edges[i].v] == 0) 61 | { 62 | cout << edges[i].u << "--" << edges[i].v << nl; 63 | cost *= edges[i].weight; 64 | 65 | if (visited[edges[i].u] == 0) 66 | { 67 | visited[edges[i].u] = 1; 68 | cnt++; 69 | } 70 | 71 | if (visited[edges[i].v] == 0) 72 | { 73 | visited[edges[i].v] = 1; 74 | cnt++; 75 | } 76 | } 77 | 78 | i++; 79 | } 80 | 81 | cout << "Minimum product: " << cost << nl << nl; 82 | 83 | CRACKED; 84 | } 85 | 86 | /* 87 | 88 | 5 7 89 | 0 1 2 90 | 0 3 6 91 | 1 2 3 92 | 1 3 8 93 | 1 4 5 94 | 2 4 7 95 | 3 4 9 96 | 97 | 98 | */ 99 | 100 | /* 101 | 102 | 7 7 103 | 0 1 3 104 | 1 3 3 105 | 1 5 10 106 | 2 4 6 107 | 2 6 9 108 | 3 6 8 109 | 4 5 6 110 | 111 | 112 | */ 113 | -------------------------------------------------------------------------------- /Lab Question + Assignment Solution/ASSG_3__Sec_J/QSN1_SolvedUsing__KrusKal_Algo.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debt cout << "Tashin " << endl; // debug code 15 | 16 | using namespace std; 17 | 18 | struct EdgeInfo 19 | { 20 | int u; 21 | int v; 22 | int weight; 23 | }; 24 | 25 | bool compare(EdgeInfo a, EdgeInfo b) 26 | { 27 | return a.weight <= b.weight; 28 | } 29 | 30 | /** Disjoint Set Parts **/ 31 | 32 | int parent[1000]; 33 | int rankOfThistree[1000]; 34 | 35 | void makeSet(int i) 36 | { 37 | parent[i] = i; 38 | rankOfThistree[i] = 0; 39 | } 40 | 41 | int findparent(int a) /// finding grand parent 42 | { 43 | if (parent[a] == a) 44 | return a; 45 | else 46 | { 47 | /// This is the Heuristic ( Pa}th Compression ) of Find Parent 48 | 49 | int friend_ = findparent(parent[a]); 50 | parent[a] = friend_; /// this is doing to minimize the complexity for the same case in future 51 | return parent[a]; 52 | } 53 | } 54 | 55 | bool unionSet(int a, int b) 56 | { 57 | int parentof_A = findparent(a); 58 | int parentof_B = findparent(b); 59 | 60 | if (parentof_A == parentof_B) 61 | { 62 | return true; 63 | } 64 | else 65 | { 66 | /// Union heuristic (Union Rank) 67 | 68 | if (rankOfThistree[parentof_A] == rankOfThistree[parentof_B]) 69 | { 70 | parent[parentof_A] = parentof_B; 71 | rankOfThistree[parentof_B]++; 72 | } 73 | else if (rankOfThistree[parentof_A] > rankOfThistree[parentof_B]) 74 | { 75 | parent[parentof_B] = parentof_A; 76 | } 77 | else if (rankOfThistree[parentof_A] < rankOfThistree[parentof_B]) 78 | { 79 | parent[parentof_A] = parentof_B; 80 | } 81 | 82 | return false; 83 | } 84 | } 85 | 86 | /** END **/ 87 | 88 | int kruskalMST(vector edges, int vertices) 89 | { 90 | sort(edges.begin(), edges.end(), compare); 91 | 92 | for (int i = 0; i < vertices; i++) 93 | { 94 | makeSet(i); 95 | } 96 | 97 | int mstCost = 1; 98 | 99 | cout << nl << "Taken Edges : " << nl; 100 | for (EdgeInfo edge : edges) 101 | { 102 | /// if their parent are not same then i have to take this edge 103 | bool sameParent = unionSet(edge.u, edge.v); /// Checking parent same or not 104 | /// parent same means i cant take them 105 | 106 | if (sameParent == false) 107 | { 108 | cout << edge.u << " --> " << edge.v << nl; 109 | 110 | mstCost *= edge.weight; 111 | } 112 | } 113 | 114 | return mstCost; 115 | } 116 | 117 | int32_t main() 118 | { 119 | int numOfVertices, numofEdge; 120 | cin >> numOfVertices >> numofEdge; /// input 121 | 122 | vector edges; 123 | 124 | for (int i = 0; i < numofEdge; i++) /// taking u v and w 125 | { 126 | EdgeInfo edge; 127 | cin >> edge.u >> edge.v >> edge.weight; 128 | edges.push_back(edge); 129 | } 130 | 131 | int mstCost = kruskalMST(edges, numOfVertices); 132 | 133 | cout << nl << "MSTCost " << mstCost << nl << nl; 134 | 135 | CRACKED; 136 | } 137 | 138 | /* 139 | 140 | 5 7 141 | 0 1 2 142 | 0 3 6 143 | 1 2 3 144 | 1 3 8 145 | 1 4 5 146 | 2 4 7 147 | 3 4 9 148 | 149 | 150 | */ 151 | -------------------------------------------------------------------------------- /Lab Question + Assignment Solution/Assignment 01 (Greedy) Solve Sec J/Noman's_ASSG.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 29/10/23 **************** 5 | 6 | #include 7 | #define faster \ 8 | ios_base::sync_with_stdio(false); \ 9 | cin.tie(0); \ 10 | cout.tie(0); 11 | 12 | #define Read(x) freopen(x, "r", stdin) 13 | #define Write(x) freopen(x, "w", stdout) 14 | #define CRACKED return 0; 15 | #define nl "\n" 16 | #define newLine cout << nl; 17 | 18 | // data_type_compressions : 19 | #define int long long 20 | #define ull unsigned long long 21 | #define ld long double 22 | 23 | #define minusone -1 24 | #define PI 3.14159265358979323846 25 | 26 | // I/O : 27 | #define sp " " // space 28 | #define si(n) cin >> n; // input [ si = scan input] 29 | #define output(x) cout << x << nl // output 30 | 31 | #define square(x) ((x) * (x)) // x^2 square 32 | 33 | #define setDec(x) fixed << setprecision(x) 34 | 35 | #define len(s) s.length() 36 | #define tolower(s) transform(s.begin(), s.end(), s.begin(), ::tolower) // make string lowercase 37 | 38 | #define YES cout << "YES" << endl; 39 | #define Yes cout << "Yes" << endl; 40 | #define NO cout << "NO" << endl; 41 | #define No cout << "No" << endl; 42 | 43 | #define min3(a, b, c) min(min(a, b), c) 44 | #define mid3(a, b, c) (a + b + c) - max3(a, b, c) - min3(a, b, c) 45 | #define max3(a, b, c) max(max(a, b), c) 46 | 47 | #define SumInRange(a, b) ((b * (b + 1)) / 2) - (((a - 1) * (a)) / 2) 48 | 49 | using namespace std; 50 | 51 | //--------------------------------- Debug -------------------------------- 52 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 53 | #define debt cout << " Tashin " << endl; 54 | 55 | //--------------------------------- FOR -------------------------------- 56 | #define FOR0(i, n) for (int i = 0; i < (int)(n); i++) //___ 0 to < N 57 | #define FOR1(i, n) for (int i = 1; i <= (int)(n); i++) //___ 1 to <= N 58 | 59 | #define FOR2(a, n) for (int i = a; i < n; i++) //___ A to < N 60 | 61 | //-------------------------------- Vector ------------------------------- 62 | #define vi vector 63 | #define vc vector 64 | #define vs vector 65 | #define vb vector 66 | #define vii vector> 67 | #define vsi vector> 68 | 69 | #define pb push_back 70 | #define pop pop_back 71 | 72 | #define vsort(v) sort(v.begin(), v.end()) // Vector asc 73 | #define vSortRev(v) sort(v.begin(), v.end(), greater()) // Vector dec 74 | 75 | #define arrSort(a) sort(a, a + n) // array asc 76 | #define arrSortRev(a) sort(a, a + n, greater()) // array dec 77 | 78 | //-------------------------------- pair ------------------------------- 79 | 80 | typedef pair pii; 81 | typedef pair pis; 82 | typedef pair psi; 83 | 84 | #define mp make_pair 85 | #define ff first 86 | #define ss second 87 | 88 | //-------------------------------- Map ------------------------------- 89 | #define mapii map 90 | #define mapsi map 91 | #define mapci map 92 | 93 | /******************************* Some Func ********************************************/ 94 | int getASCII(char c) 95 | { 96 | return c; 97 | } 98 | 99 | bool evenOdd(int n) // Odd = 0 && Even = 1 100 | { 101 | if (!(n & 1)) // Formula: EVEN (for even --->> i&1 == 0) 102 | return 1; 103 | else 104 | return 0; 105 | } 106 | 107 | void printarray(int arr[], int len) 108 | { 109 | for (int i = 0; i < len; i++) 110 | (i + 1 == len) ? cout << arr[i] << nl : cout << arr[i] << " "; 111 | } 112 | 113 | /********************************* Get used to *************************************************/ 114 | /* 115 | Use matrix = while 2D array 116 | */ 117 | bool compare(vector a, vector b) 118 | { 119 | return a[a.size() - 2] < b[b.size() - 2]; 120 | } 121 | 122 | // 0 1 2 3 4 5 == 6 123 | 124 | void solution() 125 | { 126 | int i, j, k, l, m, n, a, b, c, d, w, x, y, z, t, cnt = 0, index; 127 | 128 | cin >> n >> m; 129 | 130 | vector> matrix; 131 | 132 | int rowMax = INT_MIN; 133 | for (i = 0; i < m; i++) 134 | { 135 | vector temp; 136 | for (j = 0; j < n; j++) 137 | { 138 | cin >> a; 139 | 140 | temp.push_back(a); 141 | 142 | if (j != n - 1) 143 | rowMax = max(a, rowMax); 144 | } 145 | 146 | temp.push_back((a * 1.0) / rowMax); // 2nd last for comp 147 | temp.push_back(i); // last value for index 148 | 149 | matrix.push_back(temp); 150 | } 151 | 152 | // ///---> debug code 153 | // for (auto i : matrix) 154 | // { 155 | // for (auto j : i) 156 | // { 157 | // cout << j << " "; 158 | // } 159 | // newLine; 160 | // } 161 | // newLine; 162 | // newLine; 163 | 164 | sort(matrix.begin(), matrix.end(), compare); // sort 165 | 166 | // /---> debug code 167 | // for (auto i : matrix) 168 | // { 169 | // for (auto j : i) 170 | // { 171 | // cout << j << " "; 172 | // } 173 | // newLine; 174 | // } 175 | // newLine; 176 | // newLine; 177 | 178 | //----------------------------------------- sort done; 179 | 180 | int arr[n] = {0}; /// total vont count of envery vote kendro 181 | // n-1 = noman 182 | // left all me 183 | 184 | int count = 0; 185 | for (auto i : matrix) // kendro dhrotechi 186 | { 187 | rowMax = INT_MIN; // for amr dol 188 | 189 | for (int j = 0; j < n; j++) //// add. 190 | { 191 | arr[j] += i[j]; 192 | 193 | if (arr[j] > rowMax && j != n - 1) 194 | { 195 | rowMax = arr[j]; 196 | } 197 | } 198 | 199 | if (arr[n - 1] > rowMax) 200 | { 201 | break; 202 | } 203 | 204 | count++; // counting how many kendor i can approved 205 | } 206 | 207 | cout << m - count << nl; 208 | 209 | for (auto i : matrix) // kendro dhrotechi 210 | { 211 | count--; // counting how many kendor i can approved 212 | 213 | if (count < 0) 214 | { 215 | cout << i[n + 1] +1 << " "; 216 | } 217 | } 218 | 219 | cout << nl; 220 | 221 | } 222 | 223 | int32_t main() 224 | { 225 | faster; 226 | 227 | int t = 1; 228 | // cin >> t; 229 | int c = 1; 230 | 231 | while (t--) 232 | { 233 | // cout << "Case " << c++ << ": "; 234 | solution(); 235 | } 236 | 237 | CRACKED; 238 | } 239 | -------------------------------------------------------------------------------- /Lab Question + Assignment Solution/CT 01_TarekSir_Fall23_Sec_B/Q01_RecursionProblem.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int taskTakeTimes(int n) 10 | { 11 | if (n == 1 || n == 2) 12 | return 1; 13 | else 14 | return taskTakeTimes(n - 1) + taskTakeTimes(n - 2); 15 | } 16 | 17 | int32_t main() 18 | { 19 | int n; 20 | cin >> n; 21 | 22 | cout << taskTakeTimes(n) << nl; 23 | 24 | CRACKED; 25 | } -------------------------------------------------------------------------------- /Lab Question + Assignment Solution/CT 01_TarekSir_Fall23_Sec_B/Q02_StringProblem.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | int countt = 0; 9 | 10 | string password(int n, int cnt) 11 | { 12 | if (n == 0) 13 | { 14 | return ""; 15 | } 16 | else 17 | { 18 | int lastDigit = n % 10; 19 | if (lastDigit % 2 == 0) // even 20 | { 21 | cnt++; 22 | countt++; 23 | return to_string(lastDigit) + password(n / 10, 0); 24 | } 25 | else 26 | { 27 | return password(n / 10, 0); 28 | } 29 | } 30 | } 31 | 32 | int32_t main() 33 | { 34 | int n; 35 | cin >> n; 36 | 37 | string s = password(n, 0); 38 | for (int i = s.length(); i >= 0; i--) 39 | { 40 | cout << s[i]; 41 | } 42 | cout << "#" << countt << nl; 43 | CRACKED; 44 | } -------------------------------------------------------------------------------- /Lab Question + Assignment Solution/CT 02 Tarek Sir Fall23 Sec B - On Greedy and DnC/Q1.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debt cout << "Tashin " << endl; // debug code 15 | 16 | using namespace std; 17 | 18 | int tryingForLogN(vector arr, int totalDistance, int maxGo) 19 | { 20 | 21 | } 22 | 23 | int32_t main() 24 | { 25 | 26 | int totalDistance; 27 | cin >> totalDistance; 28 | 29 | int maxGo; 30 | cin >> maxGo; 31 | 32 | int s; 33 | cin >> s; 34 | vector stops(s); 35 | 36 | for (int i = 0; i < s; i++) 37 | { 38 | cin >> stops[i]; 39 | } 40 | 41 | /**************************/ 42 | int currentDistance = maxGo; 43 | int cnt = 0; 44 | 45 | for (int i = 0; i < s; i++) 46 | { 47 | if (stops[i] == currentDistance) 48 | { 49 | if (i == s - 1 && currentDistance >= totalDistance) 50 | { 51 | break; 52 | } 53 | cnt++; 54 | currentDistance = stops[i] + maxGo; 55 | if (i == s - 1 && currentDistance < totalDistance) 56 | { 57 | cout << "Cant reach The position" << nl; 58 | return 0; 59 | } 60 | } 61 | else if (stops[i] > currentDistance) 62 | { 63 | cnt++; 64 | currentDistance = stops[i - 1] + maxGo; 65 | } 66 | } 67 | 68 | cout << "Total stops " << cnt << nl; 69 | 70 | CRACKED; 71 | } 72 | 73 | /* 74 | 75 | 20 76 | 10 77 | 8 78 | 2 4 5 8 12 14 16 19 79 | 80 | 81 | */ -------------------------------------------------------------------------------- /Lab Question + Assignment Solution/CT 02 Tarek Sir Fall23 Sec B - On Greedy and DnC/Q2.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debt cout << "Tashin " << endl; // debug code 15 | 16 | using namespace std; 17 | 18 | int crossingSumResult(vector arr, int s, int e, int mid) 19 | { 20 | int output; 21 | 22 | //------------------ left side 23 | int lSumOutput = arr[mid]; 24 | int currentLsum = arr[mid]; 25 | 26 | int index = mid - 1; // mid to start 27 | 28 | while (index >= s) 29 | { 30 | currentLsum += arr[index]; 31 | if (currentLsum > lSumOutput) 32 | { 33 | lSumOutput = currentLsum; 34 | } 35 | index--; 36 | } 37 | 38 | //------------------ right side 39 | 40 | int rSumOutput = arr[mid + 1]; 41 | int currentRsum = arr[mid + 1]; 42 | 43 | index = (mid + 1) + 1; // mid to start 44 | 45 | while (index <= e) 46 | { 47 | currentRsum += arr[index]; 48 | if (currentRsum > rSumOutput) 49 | { 50 | rSumOutput = currentRsum; 51 | } 52 | index++; 53 | } 54 | 55 | return rSumOutput + lSumOutput; 56 | } 57 | 58 | int maximumSubArray(vector arr, int s, int e) 59 | { 60 | if (s == e) // base case 61 | { 62 | return arr[s]; 63 | } 64 | 65 | int midIndex = s + (e - s) / 2; 66 | 67 | int leftSum = maximumSubArray(arr, s, midIndex); 68 | 69 | int crossingSum = crossingSumResult(arr, s, e, midIndex); 70 | 71 | int rightSum = maximumSubArray(arr, midIndex + 1, e); 72 | 73 | return max({leftSum, rightSum, crossingSum}); 74 | } 75 | 76 | int32_t main() 77 | { 78 | 79 | int n; 80 | cin >> n; 81 | vector arr(n); 82 | for (int i = 0; i < n; i++) 83 | { 84 | cin >> arr[i]; 85 | } 86 | 87 | int maxiSum = maximumSubArray(arr, 0, n - 1); 88 | 89 | cout << maxiSum << nl; 90 | 91 | CRACKED; 92 | } 93 | 94 | /* 95 | 96 | 9 97 | -2 1 -3 4 -1 2 1 -5 4 98 | 99 | */ -------------------------------------------------------------------------------- /Lab Question + Assignment Solution/CT2_Sec_J/Q01.cpp: -------------------------------------------------------------------------------- 1 | 2 | // **************** Author : Tashin.Parvez **************** 3 | // ************* United International University ************* 4 | // **************** Updated: 27/10/23 **************** 5 | 6 | #include 7 | #define CRACKED return 0; 8 | #define nl endl // NewLine 9 | #define null NULL 10 | #define sp " " // space 11 | #define setDec(x) fixed << setprecision(x) 12 | #define len(s) s.length() 13 | #define deb(n) cout << "Tashin " << #n << " = " << n << endl; // debug code 14 | #define debt cout << "Tashin " << endl; // debug code 15 | 16 | using namespace std; 17 | 18 | void DNC(vector &arr, int s, int e) 19 | { 20 | if (s == e || e - s == 1) 21 | { 22 | return; 23 | } 24 | else 25 | { 26 | int mid = s + (e - s) / 2; 27 | if (arr[mid - 1] + arr[mid + 1] == 2 * arr[mid]) 28 | { 29 | swap(arr[mid - 1], arr[mid]); 30 | DNC(arr, s, mid - 1); 31 | DNC(arr, mid + 1, e); 32 | } 33 | } 34 | } 35 | 36 | int32_t main() 37 | { 38 | while (1) 39 | { 40 | cout << "Input N : "; 41 | int n; 42 | cin >> n; 43 | if (n == 0) 44 | break; 45 | 46 | vector arr; 47 | for (size_t i = 1; i <= n; i++) 48 | { 49 | arr.push_back(i); 50 | } 51 | 52 | DNC(arr, 0, n - 1); 53 | 54 | for (size_t i = 0; i < n; i++) 55 | { 56 | cout << arr[i] << " "; 57 | } 58 | cout << nl; 59 | } 60 | 61 | CRACKED; 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data_Structure_and_Algorithms_2_UIU 2 | 3 | ![GitHub repo size](https://img.shields.io/github/repo-size/TashinParvez/Data_Structure_and_Algorithms_2_UIU) 4 | ![GitHub contributors](https://img.shields.io/github/contributors/TashinParvez/Data_Structure_and_Algorithms_2_UIU) 5 | ![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU) 6 | 7 | Hey, folks! 8 | 9 | This repository will be immensely helpful if you are taking a **Data Structure and Algorithm-II** course or practicing **DSA-2 topics**. This brief article is here to help you become familiar with its contents. 10 | 11 | If you are a student of DSA-1, you can check my [DSA-1 Repo](https://github.com/TashinParvez/DSA_1_UIU). 12 | 13 | 14 | ## How This Repository Can Help You 15 | 16 | - **Quick Reference:** The repository can serve as a quick reference guide. You can easily find relevant code for DSA topics, saving time on online resource searches. 17 | - You will know which problems I solved before any of my exams; this will help you get an idea about the exam. 18 | - **You are getting more understandable code.** 19 | - **Coding Practice:** You can use the code for coding practice. You can modify and experiment with the code to reinforce your understanding of DSA topics. 20 | 21 | 22 | ## Table of Contents 23 | First of all, you have seen 3 folders(DSA_Topics, LAB_Code, Lab Question + Assignment Solution). 24 | 25 | - **DSA_Topics** 26 | - [Disjoint_Set](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Disjoint_Set) 27 | 28 | - [Divide and Conquer (DnC)](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Divide%20and%20Conquer%20(DnC)) 29 | - [Maximum-SubArray](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Divide%20and%20Conquer%20(DnC)/Maximum-SubArray) 30 | - [Merge Sort](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Divide%20and%20Conquer%20(DnC)/Merge%20Sort) 31 | - [Min-Max Problem](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Divide%20and%20Conquer%20(DnC)/Min-Max%20Problem) 32 | 33 | - [Dynamic Programming](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Dynamic%20Programming) 34 | - [Coin Change](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Dynamic%20Programming/Coin%20Change) 35 | - [Fibonacci](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Dynamic%20Programming/Fibonacci) 36 | - [ZeroOne Knapsack](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Dynamic%20Programming/ZeroOne%20Knapsack) 37 | 38 | - [Graph and Graph Algo](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo) 39 | - [BFS](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo/BFS) 40 | - [DFS](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo/DFS) 41 | - [Graph_Algorithoms](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo/Graph_Algorithoms) 42 | - [Bellman Ford Algo](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo/Graph_Algorithoms/Bellman%20Ford%20Algo) 43 | - [Dijkstra Algo](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo/Graph_Algorithoms/Dijkstra%20Algo) 44 | - [Kruskal's Algo (MST)](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo/Graph_Algorithoms/Kruskal's%20Algo%20(MST)) 45 | - [Learned](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo/Learned) 46 | - [Problems_BFS-DFS](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo/Problems_BFS-DFS) 47 | - [Sorting](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo/Sorting) 48 | - [Topological_Sort](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Graph%20and%20Graph%20Algo/Sorting/Topological_Sort) 49 | 50 | - [Greedy Algorithoms](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Greedy%20Algorithoms) 51 | - [Activity Selection](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Greedy%20Algorithoms/Activity%20Selection) 52 | - [Coin Change - Minimixation](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Greedy%20Algorithoms/Coin%20Change%20-%20Minimixation) 53 | - [Knapsack - Maximization](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/Greedy%20Algorithoms/Knapsack%20-%20Maximization) 54 | 55 | - [String Algorithoms](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/String%20Algorithoms) 56 | - [Knuth-Morris-Pratt__KMP_Algo](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/DSA_Topics/String%20Algorithoms/Knuth-Morris-Pratt__KMP_Algo) 57 | 58 | ___ 59 | 60 | - **Lab Code** 61 | 62 | - [Class_1 (SPL Review Solution)](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/LAB_Code/Class_1%20(SPL%20Review%20Solution)) 63 | - [Class_2 STL](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/LAB_Code/Class_2%20STL) 64 | - [Class_3_(Recursion)](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/LAB_Code/Class_3_(Recursion)) 65 | - [Class_5](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/LAB_Code/Class_5) 66 | - [Class_6](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/LAB_Code/Class_6) 67 | - [Lab Final Fall23 Sec_B](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/LAB_Code/Lab%20Final%20Fall23%20Sec_B) 68 | 69 | *** 70 | 71 | - **Lab Question + Assignment Solution** 72 | - [ASSG_3__Sec_J](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/Lab%20Question%20%2B%20Assignment%20Solution/ASSG_3__Sec_J) 73 | - [Assignment 01 (Greedy) Solve Sec J](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/Lab%20Question%20%2B%20Assignment%20Solution/Assignment%2001%20(Greedy)%20Solve%20Sec%20J) 74 | - [CT 01_TarekSir_Fall23_Sec_B](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/Lab%20Question%20%2B%20Assignment%20Solution/CT%2001_TarekSir_Fall23_Sec_B) 75 | - [CT 02 Tarek Sir Fall23 Sec B - On Greedy and DnC](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/Lab%20Question%20%2B%20Assignment%20Solution/CT%2002%20Tarek%20Sir%20Fall23%20Sec%20B%20-%20On%20Greedy%20and%20DnC) 76 | - [CT2_Sec_J](https://github.com/TashinParvez/Data_Structure_and_Algorithms_2_UIU/tree/main/Lab%20Question%20%2B%20Assignment%20Solution/CT2_Sec_J) 77 | 78 | 79 | 80 | ## Used Shortcuts 81 | If you are reading my code for the first time maybe you will find some unknown little shortcuts (Predefined), I use in my every code, so to get similar with it you just need to see the below codes. It's very simple, you can also use these tricks on your code to write code faster. All codes are written in C++. Don't worry if you don't know this language if you know C Programming that will be enough for you to understand every topic. 82 | 83 | - sometimes you will find these 2 lines at the beginning of my code 84 | ```bash 85 | // **************** Author: Tashin.Parvez *************************\ 86 | // **************** Updated: 18-08-23 *************************\ 87 | ``` 88 | this line came when I used my code template which is saved on my device. 89 | - The `include bits/stdc++.h` is a header file that we use in our code to include all the standard libraries[C++]. 90 | - **CRACKED** I use this instead of `return 0;` 91 | 92 | [Nothing but this my code different from others, you can say it's my signature] 93 | - `using namespace std;` 94 | In **C++**, we use it to make our code shorter and easier to read. It lets us use standard C++ functions and objects without having to type `std::` before each one. It's like a shortcut that saves us time and makes our code cleaner. 95 | 96 | - I write **null** instead of writing `NULL`. [Why? Well, when writing code quickly, I sometimes forget to use all capital letters for "NULL" To avoid segmentation errors I use null and it's predefined.] 97 | 98 | 99 | ## How to save this repository? 100 | 101 | - In the top-right corner of the page, click **Fork**. 102 | 103 | ![starring-a-repository](https://docs.github.com/assets/cb-40742/mw-1440/images/help/repository/fork-button.webp) 104 | 105 | ## Viewing this repository on your profile 106 | - In the top-right corner of the page, click **Star**. 107 | 108 | ![Web capture_16-10-2023_195122_docs github com](https://docs.github.com/assets/cb-8608/mw-1440/images/help/stars/starring-a-repository.webp) 109 | 110 | 111 | 112 | 113 | ## Author 114 | - [@TashinParvez](https://github.com/TashinParvez) 115 | 116 | [![linkedin](https://img.shields.io/badge/linkedin-0A66C2?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/tashinparvez/) 117 | 118 | -------------------------------------------------------------------------------- /Rename_File.cpp: -------------------------------------------------------------------------------- 1 | // **************** Author : Tashin.Parvez *************************\ 2 | // **************** Updated: 22-06-23 *************************\ 3 | 4 | #include 5 | #define CRACKED return 0; 6 | #define nl endl; // NewLine 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | string s; 12 | getline(cin, s); // take line 13 | 14 | for (int i = 0; i < s.length(); i++) 15 | { 16 | if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) 17 | cout << s[i]; 18 | else 19 | cout << '_'; 20 | } 21 | cout << '_'; 22 | cout << '_'; 23 | 24 | cout << nl; 25 | 26 | CRACKED; 27 | } 28 | --------------------------------------------------------------------------------