diff --git a/C++/Kadane's_Algorithm.cpp b/C++/Kadane's_Algorithm.cpp new file mode 100644 index 0000000..1c84dbc --- /dev/null +++ b/C++/Kadane's_Algorithm.cpp @@ -0,0 +1,31 @@ +// Problem Statement +/* +Write an efficient program to find the sum of contiguous subarray within a one-dimensional array +of numbers which has the largest sum. +*/ + +#include +using namespace std; + +int maxSubArraySum(int a[], int size) +{ +int max_so_far = a[0]; +int curr_max = a[0]; + +for (int i = 1; i < size; i++) +{ + curr_max = max(a[i], curr_max+a[i]); + max_so_far = max(max_so_far, curr_max); +} +return max_so_far; +} + +/* Driver program to test maxSubArraySum */ +int main() +{ +int a[] = {-2, -3, 4, -1, -2, 1, 5, -3}; +int n = sizeof(a)/sizeof(a[0]); +int max_sum = maxSubArraySum(a, n); +cout << "Maximum contiguous sum is " << max_sum; +return 0; +} diff --git a/C++/Knapsack_0-1.cpp b/C++/Knapsack_0-1.cpp new file mode 100644 index 0000000..42374a5 --- /dev/null +++ b/C++/Knapsack_0-1.cpp @@ -0,0 +1,52 @@ +// Problem Statement +/* +Given weights and values of n items, put these items in a knapsack of capacity W to +get the maximum total value in the knapsack. In other words, given two integer arrays +val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items +respectively. Also given an integer W which represents knapsack capacity, +find out the maximum value subset of val[] such that sum of the weights +of this subset is smaller than or equal to W. You cannot break an item, +either pick the complete item or don’t pick it (0-1 property). + +*/ + +// Algorithm : + +// A Dynamic Programming based +// solution for 0-1 Knapsack problem +#include +using namespace std ; + +// Returns the maximum value that +// can be put in a knapsack of capacity W +int knapSack(int W, int wt[], int val[], int n) +{ + int i, w; + int K[n + 1][W + 1]; + + // Build table K[][] in bottom up manner + for (i = 0; i <= n; i++) { + for (w = 0; w <= W; w++) { + if (i == 0 || w == 0) + K[i][w] = 0; + else if (wt[i - 1] <= w) + K[i][w] = max( + val[i - 1] + K[i - 1][w - wt[i - 1]], + K[i - 1][w]); + else + K[i][w] = K[i - 1][w]; + } + } + + return K[n][W]; +} + +int main() +{ + int val[] = { 60, 100, 120 }; + int wt[] = { 10, 20, 30 }; + int W = 50; + int n = sizeof(val) / sizeof(val[0]); + out<< knapSack(W, wt, val, n)) <<'\n'; + return 0; +} diff --git a/C++/Longest_Common_Subsequence.cpp b/C++/Longest_Common_Subsequence.cpp new file mode 100644 index 0000000..5f170e8 --- /dev/null +++ b/C++/Longest_Common_Subsequence.cpp @@ -0,0 +1,38 @@ +#include +using namespace std ; + +int LCS(string a,string b){ + int m = a.length() ; + int n = b.length() ; + + int lcs[m + 1][n + 1]; + + for (int i = 0; i <= m; i++) + { + for (int j = 0; j <= n; j++) + { + if (i == 0 || j == 0) + lcs[i][j] = 0; + + else if (X[i - 1] == Y[j - 1]) + lcs[i][j] = lcs[i - 1][j - 1] + 1; + + else + lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]); + } + } + + /* lcs[m][n] contains length of LCS + for X[0..n-1] and Y[0..m-1] */ + return lcs[m][n]; +} + +int main(){ + string str1,str2 ; + cout << "Take two strings to find their LCS\n"; + cin >> str1 >> str2 ; + cout << "Length of LCS is " + << LCS( str1 , str2 ); + + return 0 ; +} \ No newline at end of file diff --git a/javascript/Binary_Search.js b/javascript/Binary_Search.js new file mode 100644 index 0000000..2f4666b --- /dev/null +++ b/javascript/Binary_Search.js @@ -0,0 +1,23 @@ +// Iterative function to implement Binary Search +let iterativeFunction = function (arr, x) { + + let start=0, end=arr.length-1; + + // Iterate while start not meets end + while (start<=end){ + + // Find the mid index + let mid=Math.floor((start + end)/2); + + // If element is present at mid, return True + if (arr[mid]===x) return true; + + // Else look in left or right half accordingly + else if (arr[mid] < x) + start = mid + 1; + else + end = mid - 1; + } + + return false; +} \ No newline at end of file