View Code of Problem 4065

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stdbool.h>
typedef struct{
	char name[20];
	int flag;
}Children;
int cmp(const void *a,const void *b){
	return *(int*)a-*(int*)b;
}
int main(){
	int n;
	int a[1010];
	int b[1010];
	scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		qsort(a,n,sizeof(int),cmp);
		int first=n-1;
		int second=0;
		int count=0;
		int sum=0;
		while(second<first){
			int dis=(a[first]-a[second])*(a[first]-a[second]);
			sum+=dis;
			count++;
			if(count%2==0){
				second++;
			}else{
				first--;
			}
		}
		sum+=a[n-1]*a[n-1];
		printf("%d\n",sum);
} 




	

Double click to view unformatted code.


Back to problem 4065