15,963,022 members
1.00/5 (1 vote)
See more:
```Given an array, find the average of values in the array that satisfies the below condition.

condition is, element value and the number of times that element is present in the array must be the same.

Note:1) if no element is satisfying the given condition print -1.

2)print the average up to two decimal places.

Input Format:

First-line contains an integer 'N' which indicates the length of the Array

The next line contains 'N' array elements

Output Format:

Display the average value of elements satisfying given condition

Sample input:
7
1 2 3 4 5 6 2
Sample Output:
1.50```

What I have tried:

```#include<stdio.h>
int main()
{
int n,arr[100],i,c=0,m=0,j,b=0,temp=0;
float res;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[100]);
for(i=0;i<n;i++)
{
temp=arr[i];
for(j=1;j<n;j++)
{
if(temp==arr[j]){
c++;
}

}
if(c==temp)
{
m++;
b=b+c;
}

}
if(b==0)
{
printf("-1");
}
else
{

res=(float)b/m;
printf("%.2f",res);
}
}```
Posted
Updated 22-Apr-22 1:47am

## Solution 1

A better approach would be simple: sort the array - C includes the C library function - qsort()[^] which will help you with that.

Once sorted, all identical values are side by side - so counting identical values becomes trivial - a single pass through the sorted array will identify all values whose count equals their value.
Then it's trivial: when you identify a value as matching your condition count it, and add it to a total.
After the loop you can decide what to print and calculate the average from the count and total.

And do yourself a favour: stop using single character variable names: use names that describe what their function is: `count` instead of `b` (or possibly `m`?), `total` instead of ... well, to be honest I'm not what your total is called ... and I'm not convinced you know either looking at that code.

lalli307 22-Apr-22 5:33am
can u please write the code for me ..it would be more helpfull
OriginalGriff 22-Apr-22 7:09am
Not for me - I have no need to complete this task, you do.
And surprisingly, not for you either! Yes, you get homework you can hand it, but you have no idea how to write it (and that's not the same thing as "looking at the code" at all) so the next task you are given will be even harder for you: it assumes that you learned something from this task that you haven't if I give you full code.

Give it a try: it's not a complex task so see how far you can get!
CPallini 22-Apr-22 6:27am
5.

## Solution 2

Your approach would work only if you keep track of already visited numbers, e.g.
C
```#include<stdio.h>

#define MAX_N 100

int main()
{
int n, arr[MAX_N];
int skip[MAX_N]; // this is used for skipping already visited numbers

int sum = 0;
int items = 0;
double res;
scanf("%d",&n);
if ( n > MAX_N)
{
printf("invalid argument");
return -1;
}

for(int i =0 ;i < n; ++i)
{
scanf("%d",&arr[i]);
skip[i] = 0;
}

for(int i=0; i<n; i++)
{
int count = 0;

if (skip[i]) continue;

for(int j=0; j<n; j++)
{
if(arr[i]==arr[j])
{
++count;
skip[j] = 1;
}
}
if(arr[i] == count)
{
sum += arr[i];
++items;
}
}

if (items == 0)
{
printf("-1");
}
else
{
res = ((double)sum)/items;
printf("%.2f",res);
}
printf("\n");
return 0;
}```