From 2685e40b23eac124c18032c16ced6ae6144bcb6b Mon Sep 17 00:00:00 2001 From: VINEET IYER Date: Thu, 7 Oct 2021 16:54:00 +0530 Subject: [PATCH 1/5] Implementing two stacks in an Array --- stack/Implementing two stacks in an array | 102 ++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 stack/Implementing two stacks in an array diff --git a/stack/Implementing two stacks in an array b/stack/Implementing two stacks in an array new file mode 100644 index 0000000..9127458 --- /dev/null +++ b/stack/Implementing two stacks in an array @@ -0,0 +1,102 @@ +#include +#include + +using namespace std; + +class twoStacks { + int* arr; + int size; + int top1, top2; + +public: + // Constructor + twoStacks(int n) + { + size = n; + arr = new int[n]; + top1 = n / 2 + 1; + top2 = n / 2; + } + + // Method to push an element x to stack1 + void push1(int x) + { + // There is at least one empty + // space for new element + if (top1 > 0) { + top1--; + arr[top1] = x; + } + else { + cout << "Stack Overflow" + << " By element :" << x << endl; + return; + } + } + + // Method to push an element + // x to stack2 + void push2(int x) + { + + // There is at least one empty + // space for new element + if (top2 < size - 1) { + top2++; + arr[top2] = x; + } + else { + cout << "Stack Overflow" + << " By element :" << x << endl; + return; + } + } + + // Method to pop an element from first stack + int pop1() + { + if (top1 <= size / 2) { + int x = arr[top1]; + top1++; + return x; + } + else { + cout << "Stack UnderFlow"; + exit(1); + } + } + + // Method to pop an element + // from second stack + int pop2() + { + if (top2 >= size / 2 + 1) { + int x = arr[top2]; + top2--; + return x; + } + else { + cout << "Stack UnderFlow"; + exit(1); + } + } +}; + +/* Driver program to test twStacks class */ +int main() +{ + twoStacks ts(5); + ts.push1(5); + ts.push2(10); + ts.push2(15); + ts.push1(11); + ts.push2(7); + cout << "Popped element from stack1 is " + << " : " << ts.pop1() + << endl; + ts.push2(40); + cout << "\nPopped element from stack2 is " + << ": " << ts.pop2() + << endl; + return 0; +} From f6ba612c6d45b4182a4476bdf56a3978f48cc1cb Mon Sep 17 00:00:00 2001 From: VINEET IYER Date: Thu, 7 Oct 2021 17:07:01 +0530 Subject: [PATCH 2/5] Created Largest Contiguos sum subaaray --- array/largest contguos sum subarray | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 array/largest contguos sum subarray diff --git a/array/largest contguos sum subarray b/array/largest contguos sum subarray new file mode 100644 index 0000000..162dbc0 --- /dev/null +++ b/array/largest contguos sum subarray @@ -0,0 +1,20 @@ +int maxSubarraySum(int arr[], int size) +{ + int max_ending_here = 0, max_so_far = INT_MIN; + for (int i = 0; i < size; i++) { + + // include current element to previous subarray only + // when it can add to a bigger number than itself. + if (arr[i] <= max_ending_here + arr[i]) { + max_ending_here += arr[i]; + } + + // Else start the max subarray from current element + else { + max_ending_here = arr[i]; + } + if (max_ending_here > max_so_far) + max_so_far = max_ending_here; + } + return max_so_far; +} From f1be3ccc57c6db410a1912bdb5ac1850a24dbf0c Mon Sep 17 00:00:00 2001 From: VINEET IYER Date: Thu, 7 Oct 2021 17:16:44 +0530 Subject: [PATCH 3/5] Minimum Distance between Two Numbers --- .../Find Minimum Distance between two Numbers | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 array/Find Minimum Distance between two Numbers diff --git a/array/Find Minimum Distance between two Numbers b/array/Find Minimum Distance between two Numbers new file mode 100644 index 0000000..5304b80 --- /dev/null +++ b/array/Find Minimum Distance between two Numbers @@ -0,0 +1,37 @@ +// C++ program to Find the minimum +// distance between two numbers +#include +using namespace std; + +int minDist(int arr[], int n, int x, int y) +{ + int i, j; + int min_dist = INT_MAX; + for (i = 0; i < n; i++) { + for (j = i + 1; j < n; j++) { + if ((x == arr[i] && y == arr[j] + || y == arr[i] && x == arr[j]) + && min_dist > abs(i - j)) { + min_dist = abs(i - j); + } + } + } + if (min_dist > n) { + return -1; + } + return min_dist; +} + +/* Driver code */ +int main() +{ + int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 }; + int n = sizeof(arr) / sizeof(arr[0]); + int x = 3; + int y = 6; + + cout << "Minimum distance between " << x << " and " << y + << " is " << minDist(arr, n, x, y) << endl; +} + + From 9e5295e33aa90a7bcbc766cc88b6e4048f30300b Mon Sep 17 00:00:00 2001 From: VINEET IYER Date: Thu, 7 Oct 2021 17:20:45 +0530 Subject: [PATCH 4/5] Finding the subarray with least average --- array/Find the subarray with least average | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 array/Find the subarray with least average diff --git a/array/Find the subarray with least average b/array/Find the subarray with least average new file mode 100644 index 0000000..66bf317 --- /dev/null +++ b/array/Find the subarray with least average @@ -0,0 +1,49 @@ +// A Simple C++ program to find minimum average subarray +#include +using namespace std; + +// Prints beginning and ending indexes of subarray +// of size k with minimum average +void findMinAvgSubarray(int arr[], int n, int k) +{ + // k must be smaller than or equal to n + if (n < k) + return; + + // Initialize beginning index of result + int res_index = 0; + + // Compute sum of first subarray of size k + int curr_sum = 0; + for (int i = 0; i < k; i++) + curr_sum += arr[i]; + + // Initialize minimum sum as current sum + int min_sum = curr_sum; + + // Traverse from (k+1)'th element to n'th element + for (int i = k; i < n; i++) { + // Add current item and remove first item of + // previous subarray + curr_sum += arr[i] - arr[i - k]; + + // Update result if needed + if (curr_sum < min_sum) { + min_sum = curr_sum; + res_index = (i - k + 1); + } + } + + cout << "Subarray between [" << res_index << ", " + << res_index + k - 1 << "] has minimum average"; +} + +// Driver program +int main() +{ + int arr[] = { 3, 7, 90, 20, 10, 50, 40 }; + int k = 3; // Subarray size + int n = sizeof arr / sizeof arr[0]; + findMinAvgSubarray(arr, n, k); + return 0; +} From 0f2a12a02daa4013ad7b0b97d065ede0accddcb4 Mon Sep 17 00:00:00 2001 From: VINEET IYER Date: Thu, 7 Oct 2021 17:23:20 +0530 Subject: [PATCH 5/5] Minimum number of jumps to reach the end --- .../Minimum number of jumps to reach the end | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 array/Minimum number of jumps to reach the end diff --git a/array/Minimum number of jumps to reach the end b/array/Minimum number of jumps to reach the end new file mode 100644 index 0000000..2653843 --- /dev/null +++ b/array/Minimum number of jumps to reach the end @@ -0,0 +1,43 @@ +// C++ program to find Minimum +// number of jumps to reach end +#include +using namespace std; + +// Function to return the minimum number +// of jumps to reach arr[h] from arr[l] +int minJumps(int arr[], int n) +{ + + // Base case: when source and + // destination are same + if (n == 1) + return 0; + + // Traverse through all the points + // reachable from arr[l] + // Recursively, get the minimum number + // of jumps needed to reach arr[h] from + // these reachable points + int res = INT_MAX; + for (int i = n - 2; i >= 0; i--) { + if (i + arr[i] >= n - 1) { + int sub_res = minJumps(arr, i + 1); + if (sub_res != INT_MAX) + res = min(res, sub_res + 1); + } + } + + return res; +} + +// Driver Code +int main() +{ + int arr[] = { 1, 3, 6, 3, 2, + 3, 6, 8, 9, 5 }; + int n = sizeof(arr) / sizeof(arr[0]); + cout << "Minimum number of jumps to"; + cout << " reach the end is " << minJumps(arr, n); + return 0; +} +