从 [1, M] 范围内的给定数组中找到 K 个缺失的数字,使得总平均值为 X
给定一个大小为N的整数数组arr[] ,其中每个元素可以在[1, M] 范围内,以及两个整数X和K 。任务是在[1, M]范围内找到K个可能的数字,使得所有 (N + K) 个数字的平均值等于 X。如果有多个有效答案,则任何一个都可以接受。
例子:
Input: arr[] = {3, 2, 4, 3}, M = 6, K = 2, X = 4
Output: 6 6
Explanation: The mean of all elements is (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4.
Input: arr[] = {1}, M = 8, K = 1, X = 4
Output: 7
Explanation: The mean of all elements is (1 + 7) / 2 = 4.
Input: arr[] = {1, 2, 3, 4}, M = 6, K = 4, X = 6
Output: []
Explanation: It is impossible for the mean to be 6 no matter what the 4 missing elements are.
方法:该方法基于以下数学观察。如果添加 M 个元素后的总期望总和使得添加的 M 个元素的总和需要小于 M 或大于 K*M,则没有解决方案是可能的。否则,总是有可能的解决方案。
- 求缺失元素的总和(Y),即 = X*(K + N) – sum(arr)。
- 如果这小于 K或大于 K*M ,则无法创建数组。所以返回一个空数组。
- 否则,尝试将值 Y 等分到 K 个元素中,即为所有 K 个元素分配值 Y/K。
- 如果仍有一些值需要分配,那么在平均分配每个元素之后,剩下的值将是 = (Y%K)。因此,将 1 添加到新数组的 (Y%K) 个元素中。
下面是上述方法的实现:
C++
// C++ code to implement above approach
#include
using namespace std;
// Function to get the missing elements
vector missing(vector& arr,
int M, int X, int K)
{
int N = arr.size(),
sum = accumulate(arr.begin(),
arr.end(), 0),
newsum = 0;
newsum = X * (K + N) - sum;
// If this newsum is less than M
// or greater than K*M then
// no array can be created.
if (newsum < K || newsum > K * M)
return {};
int mod = newsum % K;
vector ans(K, newsum / K);
for (int i = 0; i < mod; i++)
ans[i] += 1;
return ans;
}
// Driver code
int main()
{
vector arr{ 3, 2, 4, 3 };
int X = 4;
int K = 2;
int M = 6;
// Vector to store resultant list
vector ans = missing(arr, M, X, K);
for (auto i : ans)
cout << i << " ";
return 0;
}
Java
// Java code to implement above approach
import java.util.*;
class GFG{
// Function to get the missing elements
static int []missing(int []arr,
int M, int X, int K)
{
int N = arr.length,
sum = accumulate(arr,0,N),
newsum = 0;
newsum = X * (K + N) - sum;
// If this newsum is less than M
// or greater than K*M then
// no array can be created.
if (newsum < K || newsum > K * M)
return new int[]{};
int mod = newsum % K;
int []ans = new int[K];
Arrays.fill(ans, newsum / K);
for (int i = 0; i < mod; i++)
ans[i] += 1;
return ans;
}
static int accumulate(int[] arr, int start, int end){
int sum=0;
for(int i= 0; i < arr.length; i++)
sum+=arr[i];
return sum;
}
// Driver code
public static void main(String[] args)
{
int[]arr = { 3, 2, 4, 3 };
int X = 4;
int K = 2;
int M = 6;
// Vector to store resultant list
int []ans = missing(arr, M, X, K);
for (int i : ans)
System.out.print(i+ " ");
}
}
// This code is contributed by shikhasingrajput
Python3
# Python code for the above approach
# Function to get the missing elements
def missing(arr, M, X, K):
N = len(arr)
sum = 0
for i in range(len(arr)):
sum += arr[i]
newsum = 0
newsum = X * (K + N) - sum
# If this newsum is less than M
# or greater than K*M then
# no array can be created.
if (newsum < K or newsum > K * M):
return []
mod = newsum % K
ans = [newsum // K] * K
for i in range(mod):
ans[i] += 1
return ans
# Driver code
arr = [3, 2, 4, 3]
X = 4
K = 2
M = 6
# Vector to store resultant list
ans = missing(arr, M, X, K)
for i in ans:
print(i, end=" ")
# This code is contributed by gfgking
C#
// C# code to implement above approach
using System;
class GFG {
// Function to get the missing elements
static int[] missing(int[] arr, int M, int X, int K)
{
int N = arr.Length, sum = accumulate(arr, 0, N),
newsum = 0;
newsum = X * (K + N) - sum;
// If this newsum is less than M
// or greater than K*M then
// no array can be created.
if (newsum < K || newsum > K * M)
return new int[] {};
int mod = newsum % K;
int[] ans = new int[K];
Array.Fill(ans, newsum / K);
for (int i = 0; i < mod; i++)
ans[i] += 1;
return ans;
}
static int accumulate(int[] arr, int start, int end)
{
int sum = 0;
for (int i = 0; i < arr.Length; i++)
sum += arr[i];
return sum;
}
// Driver code
public static void Main(string[] args)
{
int[] arr = { 3, 2, 4, 3 };
int X = 4;
int K = 2;
int M = 6;
// Vector to store resultant list
int[] ans = missing(arr, M, X, K);
foreach(int i in ans) Console.Write(i + " ");
}
}
// This code is contributed by ukasp.
Javascript
6 6
时间复杂度: O(N)
辅助空间: O(1) 当不考虑结果列表的空间时