Uva 10041 – Vito’s Family

Solution :-

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int i,t,n,a[5000],sum;
    scanf(“%d”,&t);
    while(t–)
    {
        int p=50000000;
        scanf(“%d”,&n);

        for(i=0;i<n;i++)
        scanf(“%d”,&a[i]);

        sort(a,a+i);

        for(int j=0;j<n;j++)
        {
            sum=0;
            for(int k=0;k<n;k++)
            {
            sum+=abs(a[j]-a[k]);
            }
            p=min(sum,p);
        }
        printf(“%dn”,p);
    }
    return 0;
}

At First you can try to solve this problem if you can’t than see this code & find what’s your problem . If you copy paste this code you could not improve yourself in programming .

Posted by Shipu Ahamed

3 comments

p=min(sum,p) didn’t understand this why is this statement needed?
Getting Minimum value.
You use O(n^2) . So,you no need to sort array.

Leave a Reply