Average numbers in array - GeeksforGeeks

13 May, 2022
Given a sequence of positive integers a1, a2, ..., an. Find all such indexes i such that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).Examples :  
Input : 5
        1 2 3 4 5
Output : 1 no. of elements
         2 index of element
Average of 1, 2, 4 & 5 is 3 so the 
output is single index i.e. 3.

Input : 4
        50 50 50 50
Output : 4 no. of elements
         0 1 2 3 index of element
Average of 50, 50, 50 & 50 is 50 and 
all the indexes has the same i.e. 50
so the output is indexes 1, 2, 3 & 4.
 
 
C++
C
Java
Python3
C#
PHP
Javascript
// CPP program to print all such indices such// that the i-th element equals the arithmetic// mean of all other elements#include <bits/stdc++.h>using namespace std; // function to find number of elements// satisfying condition and their indexesvoid averageNumbers(int arr[], int n, int sum){     int cnt = 0;     // calculating average    sum /= (double)n;     // counting how many elements    // satisfies the condition.    cout << count(arr, arr + n, sum)         << endl;    for (int i = 0; i < n; i++) {        if ((double)arr[i] == sum) {             // output the indices.            cout << i << " ";            cnt++;        }    }} // Driver codeint main(){    int n;    int arr[] = { 1, 2, 3, 4, 5 };    n = sizeof(arr) / sizeof(arr[0]);    double sum = 0;    int cnt = 0;     // sum of the elements of the array    for (int i = 0; i < n; i++) {        sum += (double)arr[i];    }    averageNumbers(arr, n, sum);    return 0;}
// C program to print all such indices such// that the i-th element equals the arithmetic// mean of all other elements#include <stdio.h> // function to find number of elements// satisfying condition and their indexesvoid averageNumbers(int arr[], int n, int sum){     int cnt = 0;     // calculating average    sum /= (double)n;     for (int i = 0; i < n; i++) {        if ((double)arr[i] == sum) {            // counting how many elements           // satisfies the condition            cnt++;        }    }    printf("%d\n",cnt);    for (int i = 0; i < n; i++) {        if ((double)arr[i] == sum) {             // output the indices.            printf("%d ",i);            cnt++;        }    }} // Driver codeint main(){    int n;    int arr[] = { 1, 2, 3, 4, 5 };    n = sizeof(arr) / sizeof(arr[0]);    double sum = 0;    int cnt = 0;     // sum of the elements of the array    for (int i = 0; i < n; i++) {        sum += (double)arr[i];    }    averageNumbers(arr, n, sum);    return 0;} // This code is contributed by kothvvsaakash.
// Java program to print all such indices such// that the i-th element equals the arithmetic// mean of all other elementspublic class GFG { // function to find number of elements// satisfying condition and their indexes    static void averageNumbers(int arr[], int n, int sum) {         int cnt = 0;         // calculating average        sum /= (double) n;         // counting how many elements        // satisfies the condition.        System.out.println(count(arr, sum));        for (int i = 0; i < n; i++) {            if ((double) arr[i] == sum) {                 // output the indices.                System.out.print(i + " ");                cnt++;            }        }    }     static int count(int[] array, int sum) {        int count = 0;        for (int i = 0; i < array.length; i++) {            if (array[i] == sum) {                count++;            }        }        return count;    }// Driver code     public static void main(String[] args) {        int n;        int arr[] = {1, 2, 3, 4, 5};        n = arr.length;        int sum = 0;        int cnt = 0;         // sum of the elements of the array        for (int i = 0; i < n; i++) {            sum += (double) arr[i];        }        averageNumbers(arr, n, sum);     } }// This code is contributed by 29AjayKumar
# Python 3 program to print all such indices# such that the i-th element equals the# arithmetic mean of all other elements # Function to find number of elements# satisfying condition and their indexesdef averageNumbers(arr, n, sum):    cnt = 0     # calculating average    sum /= n     # counting how many elements    # satisfies the condition.    print(count(arr, sum))    for i in range(0, n):        if (arr[i] == sum):                         # output the indices.            print(i, " ")            cnt += 1             def count(array, sum):    count = 0    for i in range(0, len(array)):        if (array[i] == sum):            count += 1    return count         # Driver codeif __name__ == '__main__':    n = 0    arr = [ 1, 2, 3, 4, 5 ]    n = len(arr)    sum = 0    cnt = 0     # sum of the elements of the array    for i in range(0, n):        sum += arr[i]    averageNumbers(arr, n, sum) # This code contributed by 29AjayKumar
     // C# program to print all such indices such// that the i-th element equals the arithmetic// mean of all other elementsusing System;public class GFG {  // function to find number of elements// satisfying condition and their indexes    static void averageNumbers(int []arr, int n, int sum) {          int cnt = 0;          // calculating average        sum /=  n;          // counting how many elements        // satisfies the condition.        Console.WriteLine(count(arr, sum));        for (int i = 0; i < n; i++) {            if ((double) arr[i] == sum) {                  // output the indices.                Console.Write(i + " ");                cnt++;            }        }    }      static int count(int[] array, int sum) {        int count = 0;        for (int i = 0; i < array.Length; i++) {            if (array[i] == sum) {                count++;            }        }        return count;    }// Driver code      public static void Main() {        int n;        int []arr = {1, 2, 3, 4, 5};        n = arr.Length;        int sum = 0;          // sum of the elements of the array        for (int i = 0; i < n; i++) {            sum += arr[i];        }        averageNumbers(arr, n, sum);      }  }// This code is contributed by 29AjayKumar
<?php// PHP program to print all such indices// such that the i-th element equals the// arithmetic mean of all other elements // counting how many elements// satisfies the condition.function coun_t($arr, $sum){    $cnt = 0;    for ( $i = 0; $i < count($arr); $i++)    {        if ($arr[$i] == $sum)        {            $cnt++;        }    }    return $cnt;} // function to find number of elements// satisfying condition and their indexesfunction averageNumbers($arr, $n, $sum){     $cnt = 0;     // calculating average    $sum /= $n;     // counting how many elements    // satisfies the condition.    echo coun_t($arr, $sum) . "\n";             for ( $i = 0; $i < $n; $i++)    {        if ($arr[$i] == $sum)        {             // output the indices.            echo $i . " ";            $cnt++;        }    }} // Driver Code$n = 0;$arr = array( 1, 2, 3, 4, 5 );$n = count($arr);$sum = 0;$cnt = 0; // sum of the elements of the arrayfor ($i = 0; $i < $n; $i++){    $sum += $arr[$i];}averageNumbers($arr, $n, $sum); // This code is contributed by// Rajput-Ji?>
<script>    // Javascript program to print all such indices such    // that the i-th element equals the arithmetic    // mean of all other elements         // function to find number of elements    // satisfying condition and their indexes    function averageNumbers(arr, n, sum) {            let cnt = 0;            // calculating average        sum /=  n;            // counting how many elements        // satisfies the condition.        document.write(count(arr, sum) + "</br>");        for (let i = 0; i < n; i++) {            if (arr[i] == sum) {                    // output the indices.                document.write(i + " ");                cnt++;            }        }    }        function count(array, sum) {        let count = 0;        for (let i = 0; i < array.length; i++) {            if (array[i] == sum) {                count++;            }        }        return count;    }         // Driver code    let n;    let arr = [1, 2, 3, 4, 5];    n = arr.length;    let sum = 0;     // sum of the elements of the array    for (let i = 0; i < n; i++) {      sum += arr[i];    }    averageNumbers(arr, n, sum);         // This code is contributed by mukesh07.</script>
Output:
1
2 
Time Complexity: O(n) This article is contributed by Sagar Shukla. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
29AjayKumar
Rajput-Ji
gfg_sal_gfg
Akanksha_Rai
mukesh07
kothavvsaakash
Arrays
School Programming
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Chocolate Distribution Problem
Reversal algorithm for array rotation
Window Sliding Technique
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Python Dictionary
Inheritance in C++
Reverse a string in Java
Interfaces in Java
C++ Classes and Objects