You are on page 1of 3

ASSIGNMENT II

ESO207A :- Data Structures and Algorithms

Name :- Saurabh Budholiya


Roll Number :- 13633

Q1. Maximum area of rectangle in a Histogram

int MaxArea(int H[], int n)


{
int max_area = 0 // max area =0 in start
int top_element // To store in the top of stack
int area_top // To store area with top bar of stack
int i = 0
while (i < n)
{ // If height is greater than the element with top value in stack
if (empty() || H[top()] <= H[i])
push(i++) // If height is less than element with top value in stack then calculate the area
else
{
top_element = top() // store the top index
pop() // pop the top
area_with_top = H[top_element] * ( i top() 1)
// update max area, if needed
if (max_area < area_with_top)
max_area = area_with_top
}
}
// area of remaining bar in the stack
while (empty() == 0)
{
top_element = top()
pop()
area_with_top = H[top_element] * ( i top() 1)
if (max_area < area_with_top)
max_area = area_with_top
}
return max_area
}

Q2. Height of the tallest building in every k consecutive buildings

void print(int A[], int n, int k)

//create a dequeue using sequence deque container//

int m=1

do

// For every element, remove the previous smaller elements which are useless than current elements//

while ( ( !isempty()) && A[m] >= A[Back()])

Dequeue(T) // Remove from back//

// Add new element at back of queue//

Enqueue(T,i)

m++

while( m <= k)

// Process for m=k to m=n

do

{
// The element at the front of the queue is the largest element of

previous window, so print it //

printf(%d ,A[Front()])

// Remove the elements which are out of this window//

while ( ( !isempty()) && Front() <= m - k)

Dequeue(H) // Remove from front of queue//

while ( ( isempty()) && A[m] >= A[Back()])

Dequeue(T)

Enqueue(T,x)

m++

while( m <=k)

printf(%d ,A[Front()])

You might also like