Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2465.html

2465. Number of Distinct Averages

  • Difficulty: Easy.
  • Related Topics: Array, Hash Table, Two Pointers, Sorting.
  • Similar Questions: Two Sum, Finding Pairs With a Certain Sum.

Problem

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

Solution (Java, C++, Python)

  • class Solution {
        public int distinctAverages(int[] nums) {
            Arrays.sort(nums);
            int n = nums.length;
            Set<Integer> s = new HashSet<>();
            for (int i = 0; i < n >> 1; ++i) {
                s.add(nums[i] + nums[n - i - 1]);
            }
            return s.size();
        }
    }
    
  • class Solution {
    public:
        int distinctAverages(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            int n = nums.size();
            unordered_set<int> s;
            for (int i = 0; i < n >> 1; ++i) s.insert(nums[i] + nums[n - i - 1]);
            return s.size();
        }
    };
    
  • class Solution:
        def distinctAverages(self, nums: List[int]) -> int:
            n = len(nums)
            nums.sort()
            return len(set(nums[i] + nums[n - i - 1] for i in range(n >> 1)))
    
    
  • func distinctAverages(nums []int) int {
    	sort.Ints(nums)
    	n := len(nums)
    	s := map[int]struct{}{}
    	for i := 0; i < n>>1; i++ {
    		s[nums[i]+nums[n-i-1]] = struct{}{}
    	}
    	return len(s)
    }
    
  • function distinctAverages(nums: number[]): number {
        nums.sort((a, b) => a - b);
        const cnt: number[] = Array(201).fill(0);
        let ans = 0;
        const n = nums.length;
        for (let i = 0; i < n >> 1; ++i) {
            if (++cnt[nums[i] + nums[n - i - 1]] === 1) {
                ++ans;
            }
        }
        return ans;
    }
    
    
  • use std::collections::HashSet;
    
    impl Solution {
        pub fn distinct_averages(nums: Vec<i32>) -> i32 {
            let mut set = HashSet::new();
            let mut ans = 0;
            let n = nums.len();
            let mut nums = nums;
            nums.sort();
    
            for i in 0..n >> 1 {
                let x = nums[i] + nums[n - i - 1];
    
                if set.contains(&x) {
                    continue;
                }
    
                set.insert(x);
                ans += 1;
            }
    
            ans
        }
    }
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions