查找 Array 的所有索引,其值与其他元素的平均值相同
给定一个包含N个整数的数组arr[] ,任务是找到数组中的所有索引,使得对于每个索引i ,除arr[i]之外的所有元素的算术平均值等于该索引处元素的值。
例子 :
Input: N = 5, arr[] = {1, 2, 3, 4, 5}
Output : {2}
Explanation : Upon removing array element at index 2 (i.e. 3),
arithmetic mean of rest of the array equals (1 + 2 + 4 + 5) / 4 = 3, which is equal to the element at index 2.
It can be seen that 2 is the only such index.
Input : N = 6, arr[] = {5, 5, 5, 5, 5, 5}
Output : {0, 1, 2, 3, 4, 5}
方法:问题可以通过以下思路解决:
Calculate the total sum of the array and for each element (arr[i]) check if the average of all the other elements is same as arr[i].
请按照以下步骤解决问题:
- 求数组所有元素的总和,并存储在变量中,比如sum。
- 遍历数组,并且,
- 在每次迭代中,通过从总和中减去当前元素值来找到没有当前元素的数组的总和。说, current_sum
- 通过将其除以N-1找到current_sum的平均值
- 如果均值等于当前索引处的值,则将索引推入答案向量中,否则继续。
- 返回答案向量。
以下是基于上述方法的代码:
C++
// C++ code for above approach
#include
using namespace std;
// Function to find indices such that
// elements at those indices equals to the
// mean of the array except those indices
vector findIndices(int N, int A[])
{
// Vector to store answer (i.e. indices)
vector answer;
// Calculation of sum of all elements
int sum = 0;
for (int i = 0; i < N; i++) {
sum += A[i];
}
// For each element checking if its
// value is equal to the mean of the
// array except the current element
for (int i = 0; i < N; i++) {
int curr_sum = sum - A[i];
if (curr_sum % (N - 1) == 0
&& curr_sum / (N - 1) == A[i]) {
answer.push_back(i);
}
}
// returning answer
return answer;
}
// Driver Code
int main()
{
int N = 5;
int A[] = { 5, 5, 5, 5, 5 };
vector ans = findIndices(N, A);
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
}
Java
// Java code for above approach
import java.util.*;
class GFG {
// Function to find indices such that
// elements at those indices equals to the
// mean of the array except those indices
static Vector findIndices(int N, int[] A)
{
// Vector to store answer (i.e. indices)
Vector answer = new Vector();
// Calculation of sum of all elements
int sum = 0;
for (int i = 0; i < N; i++) {
sum += A[i];
}
// For each element checking if its
// value is equal to the mean of the
// array except the current element
for (int i = 0; i < N; i++) {
int curr_sum = sum - A[i];
if (curr_sum % (N - 1) == 0
&& curr_sum / (N - 1) == A[i]) {
answer.add(i);
}
}
// returning answer
return answer;
}
// Driver Code
public static void main (String[] args) {
int N = 5;
int A[] = { 5, 5, 5, 5, 5 };
Vector ans = findIndices(N, A);
for (int i = 0; i < ans.size(); i++) {
System.out.print(ans.get(i) + " ");
}
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python code for above approach
# Function to find indices such that
# elements at those indices equals to the
# mean of the array except those indices
def findIndices(N, A):
# Vector to store answer (i.e. indices)
answer = []
# Calculation of sum of all elements
sum = 0
for i in range(0, N):
sum += A[i]
# For each element checking if its
# value is equal to the mean of the
# array except the current element
for i in range(0, N):
curr_sum = sum - A[i]
if (curr_sum % (N - 1) == 0 and curr_sum // (N - 1) == A[i]):
answer.append(i)
# returning answer
return answer
# Driver Code
N = 5
A = [5, 5, 5, 5, 5]
ans = findIndices(N, A)
print(*ans)
# This code is contributed by Samim Hossain Mondal.
C#
// C# code for above approach
using System;
using System.Collections;
class GFG {
// Function to find indices such that
// elements at those indices equals to the
// mean of the array except those indices
static ArrayList findIndices(int N, int[] A)
{
// Vector to store answer (i.e. indices)
ArrayList answer = new ArrayList();
// Calculation of sum of all elements
int sum = 0;
for (int i = 0; i < N; i++) {
sum += A[i];
}
// For each element checking if its
// value is equal to the mean of the
// array except the current element
for (int i = 0; i < N; i++) {
int curr_sum = sum - A[i];
if (curr_sum % (N - 1) == 0
&& curr_sum / (N - 1) == A[i]) {
answer.Add(i);
}
}
// returning answer
return answer;
}
// Driver Code
public static void Main()
{
int N = 5;
int[] A = { 5, 5, 5, 5, 5 };
ArrayList ans = findIndices(N, A);
for (int i = 0; i < ans.Count; i++) {
Console.Write(ans[i] + " ");
}
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
// JavaScript code for the above approach
// Function to find indices such that
// elements at those indices equals to the
// mean of the array except those indices
function findIndices(N, A) {
// Vector to store answer (i.e. indices)
let answer = [];
// Calculation of sum of all elements
let sum = 0;
for (let i = 0; i < N; i++) {
sum += A[i];
}
// For each element checking if its
// value is equal to the mean of the
// array except the current element
for (let i = 0; i < N; i++) {
let curr_sum = sum - A[i];
if (curr_sum % (N - 1) == 0
&& curr_sum / (N - 1) == A[i]) {
answer.push(i);
}
}
// returning answer
return answer;
}
// Driver Code
let N = 5;
let A = [5, 5, 5, 5, 5];
let ans = findIndices(N, A);
for (let i = 0; i < ans.length; i++) {
document.write(ans[i] + " ")
}
// This code is contributed by Potta Lokesh
输出
0 1 2 3 4
时间复杂度: O(N)
辅助空间: O(N)