Count index pairs whose removal preserves the array mean

Quick Overview

Given an integer array, count the index pairs whose removal leaves the arithmetic mean of the remaining elements unchanged. The problem tests a short derivation with sums and means, exact integer arithmetic on large values, and linear-time pair counting with a hash map.

Count index pairs whose removal preserves the array mean

Company: Zomato

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `nums`, return the number of index pairs `(i, j)` with `i < j` such that removing the elements at those two indices leaves the remaining array with the same arithmetic mean as the original array. The arithmetic mean of an array is the sum of its elements divided by the number of elements. You may assume the input array has at least 3 elements. ### Function Signature ```python def count_mean_preserving_pairs(nums: list[int]) -> int: ``` ### Rules - Pairs are counted by position. Equal values at different indices form different pairs, and each pair of indices is counted once. - The two means are compared exactly, with no rounding. - The result can reach 4,999,950,000 when `nums.length == 10^5`, which does not fit in a 32-bit integer. ### Constraints - `3 <= nums.length <= 10^5` - `-10^9 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [1, 2, 3, 4, 5] Output: 2 ``` The mean is 15 / 5 = 3, so the two removed values must add up to 6. Only the values 1 and 5 (indices 0 and 4) and the values 2 and 4 (indices 1 and 3) do, and removing either pair leaves three elements with mean 3. ```text Input: nums = [2, 2, 2, 2] Output: 6 ``` The mean is 2, and removing any two elements leaves `[2, 2]`, whose mean is also 2. All C(4, 2) = 6 index pairs count. ```text Input: nums = [1, 2, 4] Output: 0 ``` The mean is 7 / 3. Removing two elements leaves `[4]`, `[2]` or `[1]`, and none of those has mean 7 / 3.

Overview: Given an integer array, count the index pairs whose removal leaves the arithmetic mean of the remaining elements unchanged. The problem tests a short derivation with sums and means, exact integer arithmetic on large values, and linear-time pair counting with a hash map.

Zomato logo
Zomato
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, return the number of index pairs (i, j) with i < j such that removing the elements at those two indices leaves the remaining array with the same arithmetic mean as the original array.

The arithmetic mean of an array is the sum of its elements divided by the number of elements. You may assume the input array has at least 3 elements.

Function Signature

def count_mean_preserving_pairs(nums: list[int]) -> int:

Rules

  • Pairs are counted by position. Equal values at different indices form different pairs, and each pair of indices is counted once.
  • The two means are compared exactly, with no rounding.
  • The result can reach 4,999,950,000 when nums.length == 10^5 , which does not fit in a 32-bit integer.

Constraints

  • 3 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

Input: nums = [1, 2, 3, 4, 5]
Output: 2

The mean is 15 / 5 = 3, so the two removed values must add up to 6. Only the values 1 and 5 (indices 0 and 4) and the values 2 and 4 (indices 1 and 3) do, and removing either pair leaves three elements with mean 3.

Input: nums = [2, 2, 2, 2]
Output: 6

The mean is 2, and removing any two elements leaves [2, 2], whose mean is also 2. All C(4, 2) = 6 index pairs count.

Input: nums = [1, 2, 4]
Output: 0

The mean is 7 / 3. Removing two elements leaves [4], [2] or [1], and none of those has mean 7 / 3.