forked from ZoranPandovski/al-go-rithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added cpp code for finding sum of areas of rectangle in an array
- Loading branch information
1 parent
9d814bf
commit 42577cd
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |