Skip to content

Commit

Permalink
added cpp code for finding sum of areas of rectangle in an array
Browse files Browse the repository at this point in the history
  • Loading branch information
codefeeder committed Oct 2, 2018
1 parent 9d814bf commit 42577cd
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions greedy/sum_areas_rectangle/cpp/program.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

// CPP code to find sum of all
// area rectangle possible
#include <bits/stdc++.h>
using namespace std;

int MaxTotalRectangleArea(int a[],
int n)
{
sort(a, a + n, greater<int>());

int sum = 0;
bool flag = false;

int len;

for (int i = 0; i < n; i++)
{
if ((a[i] == a[i + 1] || a[i] -
a[i + 1] == 1) && (!flag))
{
flag = true;

len = a[i + 1];

i++;
}

else if ((a[i] == a[i + 1] ||
a[i] - a[i + 1] == 1) && (flag))
{
sum = sum + a[i + 1] * len;

flag = false;


i++;
}
}

return sum;
}

// Driver code
int main()
{
int a[] = { 10, 10, 10, 10,
11, 10, 11, 10,
9, 9, 8, 8 };
int n = sizeof(a) / sizeof(a[0]);

cout << MaxTotalRectangleArea(a, n);

return 0;
}

0 comments on commit 42577cd

Please sign in to comment.