**Largest Rectangle**

### Problem Statement :

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by h[ i ] where i [ 1, n ]. If you join k adjacent buildings, they will form a solid rectangle of area . For example, the heights array . A rectangle of height and length can be constructed within the boundaries. The area formed is h * k = 2 * 3 = 6. Function Description Complete the function largestRectangle int the editor below. It should return an integer representing the largest rectangle that can be formed within the bounds of consecutive buildings. largestRectangle has the following parameter(s): h: an array of integers representing building heights Input Format The first line contains n, the number of buildings. The second line contains n space-separated integers, each representing the height of a building. Constraints 1 <= n <= 10^5 1 <= h[ i ] <= 10^6 Output Format Print a long integer representing the maximum area of rectangle formed. .

### Solution :

` ````
Solution in C :
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i, j, k, n, h;
int ar[100000];
int area, max=0;
scanf("%d",&n);
for(i=0;i<=n;++i)
scanf("%d",&ar[i]);
for(i=0;i<=n;++i)
{
int c = 1;
j = i-1;
k = i+1;
while(j>=0 && ar[j]>ar[i]){
j--;
c++;
}
while(k<=n && ar[k]>ar[i]){
k++;
c++;
}
area = c * ar[i];
max = (area>max)? area : max;
}
printf("%d",max);
return 0;
}
```

` ````
Solution in C++ :
In C ++ :
#include <stack>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int N, h[100005];
int p = 1, s[100005];
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) scanf("%d", &h[i]);
int ans = 0;
for (int i = 0; i < N + 2; ++i) {
while (h[i] < h[s[p - 1]]) {
int y = h[s[p - 1]];
p--;
ans = max(ans, (i - s[p - 1] - 1) * y);
}
s[p++] = i;
}
printf("%d\n", ans);
return 0;
}
```

` ````
Solution in Java :
In Java :
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Solution
{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 64 * 1024);
final int N = Integer.parseInt(br.readLine().trim(), 10);
final String[] data = br.readLine().trim().split(" ");
final long[] hist = new long[N];
for (int i = 0; i < N; i++) {
final long v = Long.parseLong(data[i], 10);
hist[i] = v;
}
long res0 = 0L;
for (int i = 0; i < N; i++) {
int idx0 = i;
for (; idx0 >= 1; idx0--) {
if (hist[idx0 - 1] < hist[i]) {
break;
}
}
int idx1 = i;
for (; idx1 < hist.length - 1; idx1++) {
if (hist[idx1 + 1] < hist[i]) {
break;
}
}
final long area = hist[i] * (idx1 - idx0 + 1);
if (area > res0) {
res0 = area;
}
}
System.out.println(res0);
br.close();
br = null;
}
}
```

` ````
Solution in Python :
In Python3 :
def solve(H) :
s,i,m = [],0,0
while i < len(H) :
if not s or H[i] > H[s[-1]] :
s.append(i)
i += 1
else :
t = s.pop()
a = H[t] * ((i - s[-1] -1) if s else i)
if a > m :
m = a
while s :
t = s.pop()
a = H[t] * ((i - s[-1] -1) if s else i)
if a > m :
m = a
return m
N = int(input())
H = list(int(_) for _ in input().split())
print(solve(H))
```

## View More Similar Problems

## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →