-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxAlternatingSum.java
More file actions
70 lines (55 loc) · 2.41 KB
/
MaxAlternatingSum.java
File metadata and controls
70 lines (55 loc) · 2.41 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Arrays;
class Solution {
public long maxAlternatingSum(int[] nums) {
int n = nums.length;
// Square all numbers and convert to long to avoid overflow
long[] squares = new long[n];
for (int i = 0; i < n; i++) {
squares[i] = (long) nums[i] * nums[i];
}
// Sort the squared values
Arrays.sort(squares);
// Calculate the alternating sum
// Put largest values at even positions (positive contribution)
// Put smallest values at odd positions (negative contribution)
long sum = 0;
// Number of positive positions (0, 2, 4, ...) = (n + 1) / 2
// Number of negative positions (1, 3, 5, ...) = n / 2
int positiveCount = (n + 1) / 2;
int negativeCount = n / 2;
// Sum of largest 'positiveCount' elements (from the end)
for (int i = n - positiveCount; i < n; i++) {
sum += squares[i];
}
// Subtract sum of smallest 'negativeCount' elements (from the beginning)
for (int i = 0; i < negativeCount; i++) {
sum -= squares[i];
}
return sum;
}
public static void main(String[] args) {
Solution solution = new Solution();
// Test case 1: [1, 2, 3]
int[] nums1 = {1, 2, 3};
System.out.println("Input: " + Arrays.toString(nums1));
System.out.println("Output: " + solution.maxAlternatingSum(nums1));
System.out.println("Expected: 12");
System.out.println();
// Test case 2: [1, -1, 2, -2, 3, -3]
int[] nums2 = {1, -1, 2, -2, 3, -3};
System.out.println("Input: " + Arrays.toString(nums2));
System.out.println("Output: " + solution.maxAlternatingSum(nums2));
System.out.println("Expected: 16");
System.out.println();
// Additional test cases
int[] nums3 = {5};
System.out.println("Input: " + Arrays.toString(nums3));
System.out.println("Output: " + solution.maxAlternatingSum(nums3));
System.out.println("Expected: 25");
System.out.println();
int[] nums4 = {-5, 10};
System.out.println("Input: " + Arrays.toString(nums4));
System.out.println("Output: " + solution.maxAlternatingSum(nums4));
System.out.println("Expected: 75 (100 - 25)");
}
}