找到一个数组,使得这个和给定数组的平均值等于 K
给定两个整数N 、 K和一个由正整数组成的数组arr[] 。任务是找到任何可能的大小为N的数组,使得arr[]和要一起找到的数组的平均值为K 。
例子:
Input: arr[] = {1, 5, 6}, N = 4, K = 3
Output: {1, 2, 3, 3}
Explanation: Mean of {1, 5, 6} and {1, 2, 3, 3} together is the following:
(1 + 5 + 6 + 1 + 2 + 3 + 3) / (3+4) = 3.
Therefore {1, 2, 3, 3} is possible array to make the mean = 3.
Input: arr[] = {7, 8, 6}, N = 2, K = 3
Output: Not Possible
方法:这个问题可以通过使用简单的数字系统平均概念来解决。假设sumArr是数组new_arr[]和M的总和是 new_arr[ ]的大小。然后根据平均概念,方程可以形成为:
(sumArr + X) / (N+M) = K, where X is sum of required array.
X = K * (N + M) – sumArr.
因此,所需数组的总和应为X以使均值等于K 。如果X小于N ,则无法形成数组。
形成所需数组的最简单方法是
1, 1, 1, …(M-1 times), X-(M-1)
.
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Find the required array such that mean of both
// the arrays is equal to K
vector findArray(vector arr,
int N, int K)
{
// To store sum of elements in arr[]
int sumArr = 0;
int M = int(arr.size());
// Iterate to find sum
for (int i = 0; i < M; i++) {
sumArr += arr[i];
}
// According to the formula derived above
int X = K * (N + M) - sumArr;
// If requiredSum if less than N
if (X < N) {
cout << "Not Possible";
return {};
}
// Otherwise create an array to store answer
vector res(N);
// Putting all 1s till N-1
for (int i = 0; i < N - 1; i++) {
res[i] = 1;
}
res[N - 1] = X - (N - 1);
// Return res as the final result
return res;
}
// Driver Code
int main()
{
vector arr = { 1, 5, 6 };
int N = 4, K = 3;
vector ans = findArray(arr, N, K);
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
}
Java
// Java program for the above approach
public class GFG {
// Find the required array such that mean of both
// the arrays is equal to K
static void findArray(int []arr,
int N, int K)
{
// To store sum of elements in arr[]
int sumArr = 0;
int M = arr.length;
// Iterate to find sum
for (int i = 0; i < M; i++) {
sumArr += arr[i];
}
// According to the formula derived above
int X = K * (N + M) - sumArr;
// If requiredSum if less than N
if (X < N) {
System.out.println("Not Possible");
}
// Otherwise create an array to store answer
int []res = new int[N];
// Putting all 1s till N-1
for (int i = 0; i < N - 1; i++) {
res[i] = 1;
}
res[N - 1] = X - (N - 1);
// Return res as the final result
for (int i = 0; i < res.length; i++)
System.out.print(res[i] + " ");
}
// Driver Code
public static void main (String[] args)
{
int []arr = { 1, 5, 6 };
int N = 4, K = 3;
findArray(arr, N, K);
}
}
// This code is contributed by AnkThon
Python3
# python program for the above approach
# Find the required array such that mean of both
# the arrays is equal to K
def findArray(arr, N, K):
# To store sum of elements in arr[]
sumArr = 0
M = len(arr)
# Iterate to find sum
for i in range(0, M):
sumArr += arr[i]
# According to the formula derived above
X = K * (N + M) - sumArr
# If requiredSum if less than N
if (X < N):
print("Not Possible")
return []
# Otherwise create an array to store answer
res = [0 for _ in range(N)]
# Putting all 1s till N-1
for i in range(0, N-1):
res[i] = 1
res[N - 1] = X - (N - 1)
# Return res as the final result
return res
# Driver Code
if __name__ == "__main__":
arr = [1, 5, 6]
N = 4
K = 3
ans = findArray(arr, N, K)
for i in range(0, len(ans)):
print(ans[i], end=" ")
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
public class GFG {
// Find the required array such that mean of both
// the arrays is equal to K
static void findArray(int []arr,
int N, int K)
{
// To store sum of elements in arr[]
int sumArr = 0;
int M = arr.Length;
// Iterate to find sum
for (int i = 0; i < M; i++) {
sumArr += arr[i];
}
// According to the formula derived above
int X = K * (N + M) - sumArr;
// If requiredSum if less than N
if (X < N) {
Console.WriteLine("Not Possible");
}
// Otherwise create an array to store answer
int []res = new int[N];
// Putting all 1s till N-1
for (int i = 0; i < N - 1; i++) {
res[i] = 1;
}
res[N - 1] = X - (N - 1);
// Return res as the final result
for (int i = 0; i < res.Length; i++)
Console.Write(res[i] + " ");
}
// Driver Code
public static void Main (string[] args)
{
int []arr = { 1, 5, 6 };
int N = 4, K = 3;
findArray(arr, N, K);
}
}
// This code is contributed by AnkThon
Javascript
1 1 1 6
时间复杂度: O(N)
辅助空间: O(N)