Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
779 B
2
Indexable
Never
#include <bits/stdc++.h>

using namespace std;

int main() {
    
    int len;
    cin>>len;
    int a[len];
    
    for(int i=0;i<len;i++) cin>>a[i];
    
    int small = INT_MAX, big = INT_MIN;
    
    for(int i=0;i<len;i++) {
        small = min(small, a[i]);
        big = max(big, a[i]);
    }
    
    float avg = 0;
    int countSmall=0, countBig=0;
    
    for(int i=0;i<len;i++) {
        if(a[i]==small) countSmall++;
        if(a[i]==big) countBig++;
    }
    
    if(small==big) { // If all elements are equal
        
        avg = small*countSmall;
        avg/=(float)countSmall;
    }
    
    else {
        
        avg = small*countSmall + big*countBig;
        avg /= ( (float)countBig + (float)countSmall );
    }
    
    
    cout<<avg;
    return 0;
}