diff --git a/DP - LEETCODE/unique_paths.cpp b/DP - LEETCODE/unique_paths.cpp new file mode 100644 index 00000000..a11cef4f --- /dev/null +++ b/DP - LEETCODE/unique_paths.cpp @@ -0,0 +1,27 @@ +#include + + + +int uniquePaths(int m, int n) { + vector< vector>dp(m,vector(n)); + for(int i = 0;i=0;i--){ + for(int j = n-2;j>=0;j--){ + dp[i][j]= dp[i+1][j] + dp[i][j+1]; + } + } + + + return dp[0][0]; +} + + +int main(){ + + return 0; +} \ No newline at end of file diff --git a/Subset_sum_problem.cpp b/DP/Subset_sum_problem.cpp similarity index 100% rename from Subset_sum_problem.cpp rename to DP/Subset_sum_problem.cpp diff --git a/DP/knapsack.cpp b/DP/knapsack.cpp new file mode 100644 index 00000000..6cc041d3 --- /dev/null +++ b/DP/knapsack.cpp @@ -0,0 +1,65 @@ +// C++ program of a space optimized DP solution for +// 0-1 knapsack problem. +#include +using namespace std; + +// val[] is for storing maximum profit for each weight +// wt[] is for storing weights +// n number of item +// W maximum capacity of bag +// mat[2][W+1] to store final result +int KnapSack(int val[], int wt[], int n, int W) +{ + // matrix to store final result + int mat[2][W+1]; + memset(mat, 0, sizeof(mat)); + + // iterate through all items + int i = 0; + while (i < n) // one by one traverse each element + { + int j = 0; // traverse all weights j <= W + + // if i is odd that mean till now we have odd + // number of elements so we store result in 1th + // indexed row + if (i%2!=0) + { + while (++j <= W) // check for each value + { + if (wt[i] <= j) // include element + mat[1][j] = max(val[i] + mat[0][j-wt[i]], + mat[0][j] ); + else // exclude element + mat[1][j] = mat[0][j]; + } + + } + + // if i is even that mean till now we have even number + // of elements so we store result in 0th indexed row + else + { + while(++j <= W) + { + if (wt[i] <= j) + mat[0][j] = max(val[i] + mat[1][j-wt[i]], + mat[1][j]); + else + mat[0][j] = mat[1][j]; + } + } + i++; + } + + // Return mat[0][W] if n is odd, else mat[1][W] + return (n%2 != 0)? mat[0][W] : mat[1][W]; +} + +// Driver program to test the cases +int main() +{ + int val[] = {7, 8, 4}, wt[] = {3, 8, 6}, W = 10, n = 3; + cout << KnapSack(val, wt, n, W) << endl; + return 0; +} \ No newline at end of file diff --git a/DP/longest_subsequence.cpp b/DP/longest_subsequence.cpp new file mode 100644 index 00000000..c74c406a --- /dev/null +++ b/DP/longest_subsequence.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +int max(int a, int b); + +/* Returns length of LCS for X[0..m-1], Y[0..n-1] */ +int lcs( char *X, char *Y, int m, int n ) +{ + if (m == 0 || n == 0) + return 0; + if (X[m-1] == Y[n-1]) + return 1 + lcs(X, Y, m-1, n-1); + else + return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)); +} + +/* Utility function to get max of 2 integers */ +int max(int a, int b) +{ + return (a > b)? a : b; +} + +/* Driver code */ +int main() +{ + char X[] = "AGGTAB"; + char Y[] = "GXTXAYB"; + + int m = strlen(X); + int n = strlen(Y); + + cout<<"Length of LCS is "<< lcs( X, Y, m, n ) ; + + return 0; +} \ No newline at end of file diff --git a/Function overloading/factorial_of_big_numbers.cpp b/Function overloading/factorial_of_big_numbers.cpp index c2e50c96..c610033e 100644 --- a/Function overloading/factorial_of_big_numbers.cpp +++ b/Function overloading/factorial_of_big_numbers.cpp @@ -1,3 +1,67 @@ +<<<<<<< HEAD:hack.cpp +/ C++ program to compute factorial of big numbers +#include +using namespace std; + +// Maximum number of digits in output +#define MAX 500 + +int multiply(int x, int res[], int res_size); + +// This function finds factorial of large numbers +// and prints them +void factorial(int n) +{ + int res[MAX]; + + // Initialize result + res[0] = 1; + int res_size = 1; + + // Apply simple factorial formula n! = 1 * 2 * 3 * 4...*n + for (int x=2; x<=n; x++) + res_size = multiply(x, res, res_size); + + cout << "Factorial of given number is \n"; + for (int i=res_size-1; i>=0; i--) + cout << res[i]; +} + + +int multiply(int x, int res[], int res_size) +{ + int carry = 0; // Initialize carry + + // One by one multiply n with individual digits of res[] + for (int i=0; i using namespace std; @@ -58,4 +122,5 @@ int main() { factorial(100); return 0; -} \ No newline at end of file +} +>>>>>>> upstream/master:Function overloading/factorial_of_big_numbers.cpp diff --git a/SLIDING WINDOW/sliding_window.cpp b/SLIDING WINDOW/sliding_window.cpp new file mode 100644 index 00000000..b422a330 --- /dev/null +++ b/SLIDING WINDOW/sliding_window.cpp @@ -0,0 +1,33 @@ +// C++ Program to find the maximum for +// each and every contiguous subarray of size k. +#include +using namespace std; + +// Method to find the maximum for each +// and every contiguous subarray of size k. +void printKMax(int arr[], int n, int k) +{ + int j, max; + + for (int i = 0; i <= n - k; i++) + { + max = arr[i]; + + for (j = 1; j < k; j++) + { + if (arr[i + j] > max) + max = arr[i + j]; + } + cout << max << " "; + } +} + +// Driver code +int main() +{ + int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int n = sizeof(arr) / sizeof(arr[0]); + int k = 3; + printKMax(arr, n, k); + return 0; +} \ No newline at end of file diff --git a/maximum_fixed_point.cpp b/Searching Algo/maximum_fixed_point.cpp similarity index 94% rename from maximum_fixed_point.cpp rename to Searching Algo/maximum_fixed_point.cpp index 9a0835cf..4184d464 100644 --- a/maximum_fixed_point.cpp +++ b/Searching Algo/maximum_fixed_point.cpp @@ -1,35 +1,35 @@ -// C++ implementation of the above approach -#include -using namespace std; - -// Function to find the maximum index -// i such that arr[i] is equal to i -void findLargestIndex(int arr[], int n) -{ - - // Traversing the array from - // backwards - for (int i = n - 1; i >= 0; i--) { - - // If arr[i] is equal to i - if (arr[i] == i) { - cout << i << endl; - return; - } - } - - // If there is no such index - cout << -1 << endl; -} - -// Driver code -int main() -{ - // Given Input - int arr[] = { -10, -5, 0, 3, 7 }; - int n = sizeof(arr) / sizeof(arr[0]); - - // Function Call - findLargestIndex(arr, n); - return 0; -} +// C++ implementation of the above approach +#include +using namespace std; + +// Function to find the maximum index +// i such that arr[i] is equal to i +void findLargestIndex(int arr[], int n) +{ + + // Traversing the array from + // backwards + for (int i = n - 1; i >= 0; i--) { + + // If arr[i] is equal to i + if (arr[i] == i) { + cout << i << endl; + return; + } + } + + // If there is no such index + cout << -1 << endl; +} + +// Driver code +int main() +{ + // Given Input + int arr[] = { -10, -5, 0, 3, 7 }; + int n = sizeof(arr) / sizeof(arr[0]); + + // Function Call + findLargestIndex(arr, n); + return 0; +} diff --git a/palindrome_check_using_stl.cpp b/Sorting Algorithms/palindrome_check_using_stl.cpp similarity index 94% rename from palindrome_check_using_stl.cpp rename to Sorting Algorithms/palindrome_check_using_stl.cpp index 8cc9230c..cad8a2a0 100644 --- a/palindrome_check_using_stl.cpp +++ b/Sorting Algorithms/palindrome_check_using_stl.cpp @@ -1,30 +1,30 @@ -// C++ program for the above approach -#include -using namespace std; - -// Function to check whether string -// is palindrome -string isPalindrome(string S) -{ - // Iterate over the range [0, N/2] - for (int i = 0; i < S.length() / 2; i++) { - - // If S[i] is not equal to - // the S[N-i-1] - if (S[i] != S[S.length() - i - 1]) { - // Return No - return "No"; - } - } - // Return "Yes" - return "Yes"; -} - -// Driver Code -int main() -{ - string S = "ABCDCBA"; - cout << isPalindrome(S); - - return 0; -} +// C++ program for the above approach +#include +using namespace std; + +// Function to check whether string +// is palindrome +string isPalindrome(string S) +{ + // Iterate over the range [0, N/2] + for (int i = 0; i < S.length() / 2; i++) { + + // If S[i] is not equal to + // the S[N-i-1] + if (S[i] != S[S.length() - i - 1]) { + // Return No + return "No"; + } + } + // Return "Yes" + return "Yes"; +} + +// Driver Code +int main() +{ + string S = "ABCDCBA"; + cout << isPalindrome(S); + + return 0; +}