-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2465-NumberOfDistinctAverages.go
More file actions
66 lines (55 loc) · 2.17 KB
/
2465-NumberOfDistinctAverages.go
File metadata and controls
66 lines (55 loc) · 2.17 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
package main
// 2465. Number of Distinct Averages
// You are given a 0-indexed integer array nums of even length.
// As long as nums is not empty, you must repetitively:
// Find the minimum number in nums and remove it.
// Find the maximum number in nums and remove it.
// Calculate the average of the two removed numbers.
// The average of two numbers a and b is (a + b) / 2.
// For example, the average of 2 and 3 is (2 + 3) / 2 = 2.5.
// Return the number of distinct averages calculated using the above process.
// Note that when there is a tie for a minimum or maximum number, any can be removed.
// Example 1:
// Input: nums = [4,1,4,0,3,5]
// Output: 2
// Explanation:
// 1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3].
// 2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3].
// 3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5.
// Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
// Example 2:
// Input: nums = [1,100]
// Output: 1
// Explanation:
// There is only one average to be calculated after removing 1 and 100, so we return 1.
// Constraints:
// 2 <= nums.length <= 100
// nums.length is even.
// 0 <= nums[i] <= 100
import "fmt"
import "sort"
func distinctAverages(nums []int) int {
sort.Ints(nums)
arr := make(map[int]bool, len(nums) / 2)
for l, r := 0, len(nums) - 1; l < r; l, r = l + 1, r - 1 {
arr[nums[l] + nums[r]] = true
}
return len(arr)
}
func main() {
// Example 1:
// Input: nums = [4,1,4,0,3,5]
// Output: 2
// Explanation:
// 1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3].
// 2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3].
// 3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5.
// Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
fmt.Println(distinctAverages([]int{4,1,4,0,3,5})) // 2
// Example 2:
// Input: nums = [1,100]
// Output: 1
// Explanation:
// There is only one average to be calculated after removing 1 and 100, so we return 1.
fmt.Println(distinctAverages([]int{1,100})) // 1
}