Find all indices of Array having value same as average of other elements
Last Updated :
29 Mar, 2022
Given an array arr[] of N integers, the task is to find all indices in the array, such that for each index i the arithmetic mean of all elements except arr[i] is equal to the value of the element at that index.
Examples :
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}
Approach: The problem can be solved by the following idea:
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].
Follow the steps to solve the problem:
- Find sum of all elements of the array and store in a variable say sum.
- Traverse the array and,
- At each iteration, find the sum of the array without current element by subtracting current element value from the sum. say, current_sum
- Find the mean of current_sum, by dividing it with N-1
- If mean is equal to the value at current index, push the index in answer vector, else continue.
- Return the answer vector.
Following is the code based on above approach :
C++
// C++ code for above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find indices such that
// elements at those indices equals to the
// mean of the array except those indices
vector<int> findIndices(int N, int A[])
{
// Vector to store answer (i.e. indices)
vector<int> 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<int> 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<Integer> findIndices(int N, int[] A)
{
// Vector to store answer (i.e. indices)
Vector<Integer> answer = new Vector<Integer>();
// 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<Integer> 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
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Find the deleted value from the array when average of original elements is given Given an array of length N + K. Also given the average avg of all the elements of the array. If an element that appears exactly K time got removed from the array (all the occurrences) and the resultant array is given, the task is to find the element X. Note that if X is not an integer then print -1.
5 min read
Find array elements that are greater than average Given an array of numbers, print all those elements that are greater than average. Examples: Input : 5, 4, 6, 9, 10 Output : 9 10 Explanation: avg = 5 + 4 + 6 + 9 + 10 / 5; avg = 34 / 5 avg = 6.8 Elements greater than 6.8 are 9 and 10 Input : 1, 2, 4, 0, 5 Output : 4 5 1) Find average of elements. 2
5 min read
Count occurrences of the average of array elements with a given number Given an array of N integers and an integer x . For every integer of the array a[i], the task is to calculate the count of numbers in the array with value equals to the average of element a[i] and x. That is, the number of occurrences of the (average of element a[i] and x) in the array. Examples: In
7 min read
Count of Unique elements after inserting average of MEX and Array Max K times Given an array A[] of N non-negative integers and an integer K. Each time, you can do the following two operations Find Average of MAX and MEX(rounded up to closest greater integer) of the array.Insert calculated average in the array. After performing the above operation K times, find the count of u
9 min read
Replace all elements of given Array with average of previous K and next K elements Given an array arr[] containing N positive integers and an integer K. The task is to replace every array element with the average of previous K and next K elements. Also, if K elements are not present then adjust use the maximum number of elements available before and after. Examples: Input: arr[] =
11 min read