-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberDivision.java
More file actions
31 lines (28 loc) · 884 Bytes
/
NumberDivision.java
File metadata and controls
31 lines (28 loc) · 884 Bytes
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
/**
* https://codeforces.com/problemset/problem/1106/C #greedy #implementation #math #sorting the max
* element must be divided in a group with another element that you have to make sure the the square
* of group is minimum => sort the array and make two pointer: from left, right => make efficient
* group
*/
import java.util.Arrays;
import java.util.Scanner;
public class NumberDivision {
static long calMinSum() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
int temp = sc.nextInt();
arr[i] = temp;
}
Arrays.sort(arr);
long result = 0;
for (int i = 0, j = n - 1; i < j; i++, j--) {
result += (long) (arr[i] + arr[j]) * (arr[i] + arr[j]);
}
return result;
}
public static void main(String[] args) {
System.out.println(calMinSum());
}
}