给定长度为N + K的数组。还给出了数组所有元素的平均平均值。如果从数组中删除了恰好K时间出现的元素(所有出现的情况)并给出了结果数组,则任务是找到元素X。请注意,如果X不是整数,则输出-1 。
例子:
Input: arr[] = {2, 7, 3}, K = 3, avg = 4
Output: 4
The original array was {2, 7, 3, 4, 4, 4}
where 4 which occurred thrice was deleted.
(2 + 7 + 3 + 4 + 4 + 4) / 6 = 4
Input: arr[] = {5, 2, 3}, K = 4, avg = 7;
Output: -1
The required element is 9.75 which is not an integer.
方法:
- 找到数组元素的总和并将其存储在变量sum中。
- 由于X出现了K次,则原始数组的总和将为sumOrg = sum +(X * K) 。
- 平均值为avg,即avg = sumOrg /(N + K) 。
- / K –现在,X可以很容易地为X =(SUM(平均*(N + K)))来计算
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the missing element
int findMissing(int arr[], int n, int k, int avg)
{
// Find the sum of the array elements
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
// The numerator and the denominator
// of the equation
int num = (avg * (n + k)) - sum;
int den = k;
// If not divisible then X is
// not an integer
// it is a floating point number
if (num % den != 0)
return -1;
// Return X
return (num / den);
}
// Driver code
int main()
{
int k = 3, avg = 4;
int arr[] = { 2, 7, 3 };
int n = sizeof(arr) / sizeof(int);
cout << findMissing(arr, n, k, avg);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the missing element
static int findMissing(int arr[], int n,
int k, int avg)
{
// Find the sum of the array elements
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
// The numerator and the denominator
// of the equation
int num = (avg * (n + k)) - sum;
int den = k;
// If not divisible then X is
// not an integer
// it is a floating point number
if (num % den != 0)
return -1;
// Return X
return (int)(num / den);
}
// Driver code
public static void main (String[] args)
{
int k = 3, avg = 4;
int arr[] = { 2, 7, 3 };
int n = arr.length;
System.out.println(findMissing(arr, n, k, avg));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to return the missing element
def findMissing(arr, n, k, avg):
# Find the sum of the array elements
sum = 0;
for i in range(n):
sum += arr[i];
# The numerator and the denominator
# of the equation
num = (avg * (n + k)) - sum;
den = k;
# If not divisible then X is
# not an integer
# it is a floating ponumber
if (num % den != 0):
return -1;
# Return X
return (int)(num / den);
# Driver code
k = 3; avg = 4;
arr = [2, 7, 3] ;
n = len(arr);
print(findMissing(arr, n, k, avg));
# This code is contributed by 29AjayKumar
C#
// C# implementation of above approach
using System;
class GFG
{
// Function to return the missing element
static int findMissing(int []arr, int n,
int k, int avg)
{
// Find the sum of the array elements
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
// The numerator and the denominator
// of the equation
int num = (avg * (n + k)) - sum;
int den = k;
// If not divisible then X is
// not an integer
// it is a floating point number
if (num % den != 0)
return -1;
// Return X
return (int)(num / den);
}
// Driver code
public static void Main (String[] args)
{
int k = 3, avg = 4;
int []arr = { 2, 7, 3 };
int n = arr.Length;
Console.WriteLine(findMissing(arr, n, k, avg));
}
}
// This code is contributed by Rajput-Ji
输出:
4
时间复杂度: O(1)
辅助空间: O(1)