From 561674b6216b5ac798000a3f85616a00572577b7 Mon Sep 17 00:00:00 2001 From: Nikhil Jaiswal Date: Wed, 12 Oct 2022 10:09:08 +0530 Subject: [PATCH 1/4] Added max_area_histogram in cpp --- CPP/DataStructures/max_area_histogram.cpp | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 CPP/DataStructures/max_area_histogram.cpp diff --git a/CPP/DataStructures/max_area_histogram.cpp b/CPP/DataStructures/max_area_histogram.cpp new file mode 100644 index 0000000..188a3c7 --- /dev/null +++ b/CPP/DataStructures/max_area_histogram.cpp @@ -0,0 +1,73 @@ +// C++ program to find maximum rectangular area in +// linear time +#include +using namespace std; + +// The main function to find the maximum rectangular +// area under given histogram with n bars +int getMaxArea(int hist[], int n) +{ + // Create an empty stack. The stack holds indexes + // of hist[] array. The bars stored in stack are + // always in increasing order of their heights. + stack s; + + int max_area = 0; // Initialize max area + int tp; // To store top of stack + int area_with_top; // To store area with top bar + // as the smallest bar + + // Run through all bars of given histogram + int i = 0; + while (i < n) { + // If this bar is higher than the bar on top + // stack, push it to stack + if (s.empty() || hist[s.top()] <= hist[i]) + s.push(i++); + + // If this bar is lower than top of stack, + // then calculate area of rectangle with stack + // top as the smallest (or minimum height) bar. + // 'i' is 'right index' for the top and element + // before top in stack is 'left index' + else { + tp = s.top(); // store the top index + s.pop(); // pop the top + + // Calculate the area with hist[tp] stack + // as smallest bar + area_with_top + = hist[tp] + * (s.empty() ? i : i - s.top() - 1); + + // update max area, if needed + if (max_area < area_with_top) + max_area = area_with_top; + } + } + + // Now pop the remaining bars from stack and calculate + // area with every popped bar as the smallest bar + while (s.empty() == false) { + tp = s.top(); + s.pop(); + area_with_top + = hist[tp] * (s.empty() ? i : i - s.top() - 1); + + if (max_area < area_with_top) + max_area = area_with_top; + } + + return max_area; +} + +// Driver code +int main() +{ + int hist[] = { 6, 2, 5, 4, 5, 1, 6 }; + int n = sizeof(hist) / sizeof(hist[0]); + + // Function call + cout << "Maximum area is " << getMaxArea(hist, n); + return 0; +} From af6a053e5a61bd9103f89e7f5a2a37489a5d0c0d Mon Sep 17 00:00:00 2001 From: Nikhil Jaiswal Date: Wed, 12 Oct 2022 11:30:51 +0530 Subject: [PATCH 2/4] Added Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp --- ...her_linked_list_at_alternate_positions.cpp | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp diff --git a/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp b/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp new file mode 100644 index 0000000..cc5604a --- /dev/null +++ b/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp @@ -0,0 +1,85 @@ +/* C++ program to merge a linked list into another at + alternate positions */ +#include +using namespace std; + +// node creation +class Node +{ + public: + int data; + Node *next; +}; + +/* Function to insert a node at the beginning */ +void push(Node ** head_ref, int new_data) +{ + Node* new_node = new Node(); + new_node->data = new_data; + new_node->next = (*head_ref); + (*head_ref) = new_node; +} + +/* Utility function to print a singly linked list */ +void printList(Node *head) +{ + Node *temp = head; + while (temp != NULL) + { + cout<data<<" "; + temp = temp->next; + } + cout<next; + q_next = q_curr->next; + + // Make q_curr as next of p_curr + q_curr->next = p_next; // Change next pointer of q_curr + p_curr->next = q_curr; // Change next pointer of p_curr + + // Update current pointers for next iteration + p_curr = p_next; + q_curr = q_next; + } + + *q = q_curr; // Update head pointer of second list +} + +//Driver code... +int main() +{ + Node *p = NULL, *q = NULL; + push(&p, 3); + push(&p, 2); + push(&p, 1); + cout<<"First Linked List:\n"; + printList(p); + + push(&q, 8); + push(&q, 7); + push(&q, 6); + push(&q, 5); + push(&q, 4); + cout<<"Second Linked List:\n"; + printList(q); + + merge(p, &q); + + cout<<"Modified First Linked List:\n"; + printList(p); + + cout<<"Modified Second Linked List:\n"; + printList(q); + + return 0; +} \ No newline at end of file From 81cd223b02abf76416a50b307285c80fa2a63629 Mon Sep 17 00:00:00 2001 From: Nikhil Jaiswal Date: Wed, 12 Oct 2022 11:51:39 +0530 Subject: [PATCH 3/4] Added Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp --- ...ked_list_into_another_linked_list_at_alternate_positions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp b/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp index cc5604a..1e84183 100644 --- a/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp +++ b/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp @@ -55,7 +55,7 @@ void merge(Node *p, Node **q) *q = q_curr; // Update head pointer of second list } -//Driver code... +//Driver code int main() { Node *p = NULL, *q = NULL; From 3315cd1440463fdc74dbd7991ee74903996f68ca Mon Sep 17 00:00:00 2001 From: Nikhil Jaiswal Date: Wed, 12 Oct 2022 11:54:32 +0530 Subject: [PATCH 4/4] Added Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp --- ...list_into_another_linked_list_at_alternate_positions.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp b/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp index 1e84183..00fdb3e 100644 --- a/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp +++ b/CPP/DataStructures/Merge_a_linked_list_into_another_linked_list_at_alternate_positions.cpp @@ -1,5 +1,5 @@ -/* C++ program to merge a linked list into another at - alternate positions */ + + #include using namespace std; @@ -55,7 +55,7 @@ void merge(Node *p, Node **q) *q = q_curr; // Update head pointer of second list } -//Driver code + int main() { Node *p = NULL, *q = NULL;