Skip to content

Commit 2875b0a

Browse files
Added solution for maximise the array
1 parent 831644c commit 2875b0a

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Maximize difference between the Sum of the two halves of the Array
2+
// after removal of N elements
3+
4+
5+
// Given an integer N and array arr[] consisting of 3 * N integers, the task is to find the maximum difference between first half and second half of the array after the removal of exactly N elements from the array.
6+
7+
// Examples:
8+
9+
// Input: N = 2, arr[] = {3, 1, 4, 1, 5, 9}
10+
// Output: 1
11+
// Explanation:
12+
// Removal of arr[1] and arr[5] from the array maximizes the difference = (3 + 4) – (1 + 5) = 7 – 6 = 1.
13+
14+
// Input: N = 1, arr[] = {1, 2, 3}
15+
// Output: -1
16+
17+
18+
#include <bits/stdc++.h>
19+
using namespace std;
20+
21+
// Function to print the maximum difference
22+
// possible between the two halves of the array
23+
long long FindMaxDif(vector<long long> a, int m)
24+
{
25+
int n = m / 3;
26+
27+
vector<long long> l(m + 5), r(m + 5);
28+
29+
// Stores n maximum values from the start
30+
multiset<long long> s;
31+
32+
for (int i = 1; i <= m; i++) {
33+
34+
// Insert first n elements
35+
if (i <= n) {
36+
37+
// Update sum of largest n
38+
// elements from left
39+
l[i] = a[i - 1] + l[i - 1];
40+
s.insert(a[i - 1]);
41+
}
42+
43+
// For the remaining elements
44+
else {
45+
l[i] = l[i - 1];
46+
47+
// Obtain minimum value
48+
// in the set
49+
long long d = *s.begin();
50+
51+
// Insert only if it is greater
52+
// than minimum value
53+
if (a[i - 1] > d) {
54+
55+
// Update sum from left
56+
l[i] -= d;
57+
l[i] += a[i - 1];
58+
59+
// Remove the minimum
60+
s.erase(s.find(d));
61+
62+
// Insert the current element
63+
s.insert(a[i - 1]);
64+
}
65+
}
66+
}
67+
68+
// Clear the set
69+
s.clear();
70+
71+
// Store n minimum elements from the end
72+
for (int i = m; i >= 1; i--) {
73+
74+
// Insert the last n elements
75+
if (i >= m - n + 1) {
76+
77+
// Update sum of smallest n
78+
// elements from right
79+
r[i] = a[i - 1] + r[i + 1];
80+
s.insert(a[i - 1]);
81+
}
82+
83+
// For the remaining elements
84+
else {
85+
86+
r[i] = r[i + 1];
87+
88+
// Obtain the minimum
89+
long long d = *s.rbegin();
90+
91+
// Insert only if it is smaller
92+
// than maximum value
93+
if (a[i - 1] < d) {
94+
95+
// Update sum from right
96+
r[i] -= d;
97+
r[i] += a[i - 1];
98+
99+
// Remove the minimum
100+
s.erase(s.find(d));
101+
102+
// Insert the new element
103+
s.insert(a[i - 1]);
104+
}
105+
}
106+
}
107+
108+
long long ans = -9e18L;
109+
110+
for (int i = n; i <= m - n; i++) {
111+
112+
// Compare the difference and
113+
// store the maximum
114+
ans = max(ans, l[i] - r[i + 1]);
115+
}
116+
117+
// Return the maximum
118+
// possible difference
119+
return ans;
120+
}
121+
122+
// Driver Code
123+
int main()
124+
{
125+
126+
vector<long long> vtr = { 3, 1, 4, 1, 5, 9 };
127+
int n = vtr.size();
128+
129+
cout << FindMaxDif(vtr, n);
130+
131+
return 0;
132+
}

0 commit comments

Comments
 (0)