Skip to content

Commit

Permalink
the second
Browse files Browse the repository at this point in the history
  • Loading branch information
Hero-2000 authored Oct 3, 2020
1 parent f6ac288 commit 618f795
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 0 deletions.
64 changes: 64 additions & 0 deletions max_sub_arr_dANDq.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// A Divide and Conquer based program for maximum subarray sum problem
#include <iostream>
#include <limits.h>
using namespace std ;
// A utility funtion to find maximum of two integers
int max(int a, int b) { return (a > b)? a : b; }

// A utility funtion to find maximum of three integers
int max(int a, int b, int c) { return max(max(a, b), c); }

// Find the maximum possible sum in arr[] auch that arr[m] is part of it
int maxCrossingSum(int arr[], int l, int m, int h)
{
// Include elements on left of mid.
int sum = 0;
int left_sum = INT_MIN;
for (int i = m; i >= l; i--)
{
sum = sum + arr[i];
if (sum > left_sum)
left_sum = sum;
}

// Include elements on right of mid
sum = 0;
int right_sum = INT_MIN;
for (int i = m+1; i <= h; i++)
{
sum = sum + arr[i];
if (sum > right_sum)
right_sum = sum;
}

// Return sum of elements on left and right of mid
return left_sum + right_sum;
}

// Returns sum of maxium sum subarray in aa[l..h]
int maxSubArraySum(int arr[], int l, int h)
{
// Base Case: Only one element
if (l == h)
return arr[l];

// Find middle point
int m = (l + h)/2;

/* Return maximum of following three possible cases
a) Maximum subarray sum in left half
b) Maximum subarray sum in right half
c) Maximum subarray sum such that the subarray crosses the midpoint */
return max(maxSubArraySum(arr, l, m),
maxSubArraySum(arr, m+1, h),
maxCrossingSum(arr, l, m, h));
}
int main()
{
int arr[] = {-2, -5, 6, -2, -3, 1, 5, -6};
int n = sizeof(arr)/sizeof(arr[0]);
int max_sum = maxSubArraySum(arr, 0, n-1);
cout<<"Maximum contiguous sum is "<< max_sum;

return 0;
}
41 changes: 41 additions & 0 deletions maximum element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<bits/stdc++.h>
using namespace std ;
int main() {
std::stack<int> st;
int n, x;
scanf("%d", &n);

for (int i = 0; i < n; i++) {
int q;
scanf("%d", &q);

switch (q)
{
case 1:
scanf("%d", &x);

if (st.empty()) {
st.push(x);
}
else {
st.push(max(x, st.top()));
}
break;

case 2:
if (!st.empty()) {
st.pop();
}
break;

case 3:
printf("%d\n", st.top());
break;

default:
break;
}
}

return 0;
}
51 changes: 51 additions & 0 deletions median.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include<iostream>
using namespace std;
int main()
{
int n1,n2;

float a[10],b[10];
int c[22];
float y;
cout<<"enter the size of arrays"<<endl;
cin>>n1>>n2;
cout<<"enter the elements of first array"<<endl;
for(int i=1;i<=n1;i++)
cin>>a[i];
cout<<"enter the elements of second array:"<<endl;;
for(int i=1;i<=n2;i++)
cin>>b[i];
int r=n1+n2;
a[n1+1]=1000;
b[n2+1]=1000;
int i=1;
int j=1;

for(int k=1;k<=r;k++)
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
j++;
}
}
for(int i=1;i<=r;i++)
cout<<c[i]<<" ";
if(!(n1+n2)%2)
{
cout<<"median element is"<<endl;
y=c[(n1+n2)/2]+c[(n1+n2)/2 +1];
cout<<(y/2.0)<<endl;
}
else
{
cout<<"median element is"<<endl;
cout<<c[(n1+n2)/2+1]<<endl;
}
return 0;
}
148 changes: 148 additions & 0 deletions merge_two_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <bits/stdc++.h>

using namespace std;

class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;

SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};

class SinglyLinkedList {
public:
SinglyLinkedListNode *head;
SinglyLinkedListNode *tail;

SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}

void insert_node(int node_data) {
SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);

if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}

this->tail = node;
}
};

void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) {
while (node) {
fout << node->data;

node = node->next;

if (node) {
fout << sep;
}
}
}

void free_singly_linked_list(SinglyLinkedListNode* node) {
while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;

free(temp);
}
}


SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2)
{
SinglyLinkedList* l = new SinglyLinkedList();

SinglyLinkedListNode* temp1 = head1 ;
SinglyLinkedListNode* temp2 = head2;

while(temp1 != NULL && temp2 != NULL)
{
if( temp1->data < temp2->data )
{

l->insert_node(temp1->data) ;
temp1=temp1->next ;
}
else
{
l->insert_node(temp2->data) ;
temp2 = temp2->next ;
}

}

while(temp1!=NULL)
{
l->insert_node(temp1->data) ;
temp1=temp1->next ;
}

while(temp2!=NULL)
{
l->insert_node(temp2->data) ;
temp2 = temp2->next ;
}

return l->head ;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH")) ;

int tests;
cin >> tests;

cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
SinglyLinkedList* llist1 = new SinglyLinkedList();

int llist1_count;
cin >> llist1_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int i = 0; i < llist1_count; i++) {
int llist1_item;
cin >> llist1_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

llist1->insert_node(llist1_item);
}

SinglyLinkedList* llist2 = new SinglyLinkedList();

int llist2_count;
cin >> llist2_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int i = 0; i < llist2_count; i++) {
int llist2_item;
cin >> llist2_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

llist2->insert_node(llist2_item);
}

SinglyLinkedListNode* llist3 = mergeLists(llist1->head, llist2->head);

print_singly_linked_list(llist3, " ", fout);
fout << "\n";

free_singly_linked_list(llist3);
}

fout.close();

return 0;
}
30 changes: 30 additions & 0 deletions min-steps-in-infinite-grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<iostream>
#include<vector>
using namespace std ;
int main()
{
int n;
cin>>n ;

vector< int > a(n) , b(n) ;

for(int i:a)
cin>>a[i] ;

for(int i:a)
cin>>b[i] ;
int count = 0 ;
for(int c=0 ; c<n-1 ; c++)
{
if( a[c]==a[c+1] ) //in x axis
count += abs(b[c+1] - b[c]) ;
else if ( b[c]==b[c+1] )
count += abs(a[i+1] - a[i] ) ;
else //digonal move
{
if(c==n-2) // last
count+=( abs(b[c+1] - b[c]) + abs(a[c+1] - a[c] ) ) ;
}
}
cout << count ;
}
50 changes: 50 additions & 0 deletions min_heap_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// C++ program for implementation of Heap Sort
#include <iostream>
using namespace std;
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2*i + 1; // left = 2*i + 1
int r = 2*i + 2; // right = 2*i + 2

if (l < n && arr[l] > arr[largest])
largest = l;


if (r < n && arr[r] > arr[largest])
largest = r;

if (largest != i)
{
swap(arr[i], arr[largest]);

heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

for (int i=n-1; i>=0; i--)
{
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
void printArray(int arr[], int n)
{
for (int i=0; i<n; ++i)
cout << arr[i] << " ";
cout << "\n";
}
int main()
{
int arr[] = {12, 11,81, 13,75, 5, 6, 7,1,69};
int n = sizeof(arr)/sizeof(arr[0]);

heapSort(arr, n);

cout << "Sorted array is \n";
printArray(arr, n);
}
Loading

0 comments on commit 618f795

Please sign in to comment.