-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_sum_squares.cpp
37 lines (36 loc) · 933 Bytes
/
max_sum_squares.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
static const int __ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return 0;
}();
class Solution {
public:
int maxSum(vector<int> &nums, int k) {
int i, j;
int n = nums.size();
int sum = 0;
vector<int> arr;
sort(nums.begin(), nums.end());
// for(i=n-k-1; i>=n-(2*k); i--){
// for(j=i+k; j>=n-k; j--){
// if(i == j){
// continue;
// }
// else{
// nums[i] = (nums[i] & nums[j]);
// nums[j] = (nums[i] | nums[j]);
// //break;
// }
// }
// }
for (i = 0; i < k; i++) {
arr.push_back(nums[n - i - 1]);
}
int n1 = arr.size();
for (i = 0; i < n1; i++) {
sum = sum + (arr[i] * arr[i]);
}
return sum;
}
};