Finding number of average elements

The question is to find the number of average elements in an array .(http://opc.iarcs.org.in/index.php/problems/AVERAGE)
I know that this can be made efficient but I wanted to try to code the NxNxN solution as well. However, it’s giving a wrong answer. What am I missing?

#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
 
        int n,a[10001];
        int count = 0;
        cin>>n;
        for(int i=0;i<n;i++)
        cin>>a[i];
        for(int i=0;i<n;i++)
        {
                for(int j=0;j<n;j++)
                {
 
                        for(int k=j+1;k<n;k++)
                        {
                               if(((a[k]+a[j])==2*a[i]))
                                {
                                	count++;
                                }
 
                        }
                }
 
        }
        cout<<count<<endl;
        return 0;
}
1 Like

Can you explain what you have done?

The question is, what is the result for input

3
1 2 4

if it is 1 or not…

The question asked is to count the number of average elements and not the number of possible pairs whose average value is in the array.

So if you have a case like this

5
1 2 3 4 5

The answer should be 3 and not 4.

Because 1+3=2* 2 (count=1), 1+5=2* 3(count=2), 2+4=2* 3 (this should not be counted as a[2] is already counted as an average element in the previous case(1 and 5).), 3+5=2* 4(count=3)

4 Likes
//