-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMaximumDifference.cpp
49 lines (42 loc) · 1.11 KB
/
MaximumDifference.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Maximum Difference
Given an array of integers A and an integer B. Find and return the maximum value of | s1 - s2 | where s1 = sum of any subset of size B, s2 = sum of elements of A - sum of elemets of s1 Note |x| denotes the absolute value of x.
Input Format
The arguments given are the integer array A and integer B.
Output Format
Return the maximum value of | s1 - s2 |.
Constraints
1 <= B < length of the array <= 100000
1 <= A[i] <= 10^5
For Example
Input 1:
A = [1, 2, 3, 4, 5]
B = 2
Output 1:
9
Input 2:
A = [5, 17, 100, 11]
B = 3
Output 2:
123
*/
// Sort and take full sum, now s1=sum till b elments and s2 is sum from B till last elements
int Solution::solve(vector<int> &A, int B)
{
long long int pre = 0, suf = 0, sum = 0;
sort(A.begin(), A.end());
for (auto a : A)
{
sum += a;
}
int temp = 0;
for (int i = 0; i < B; i++)
{ // To calculate S1
pre += A[i];
}
for (int i = A.size() - B; i < A.size(); i++) // To calculate S2
{
suf += A[i];
}
return max(llabs(sum - 2 * pre), llabs(sum - 2 * suf)); // abs max of values
}