c - Summation of the absolute difference between every two adjacent number is maximum in a array -


e.g (1,2,7) , m =[1-7]+[2-7} gives m=11 instead of calculating directly gives 6 [1-2]+[2-7] please me how solve this?? have used array find max , min afterwards don't know how proceed.

#include <stdio.h>  int main() {     long int a[100000];     int n,i;     scanf("%d",&n);     for(int i=0;i<n;i++){         scanf("%lu",&a[i]);     }     int max=a[0];     int min=a[0];     for(int i=0;i<n;i++){         if(a[i]>max){           max=a[i];           }else if(a[i]<min){             min=a[i];         }     }     printf("%d\n",max);     printf("%d\n",min);      return 0; } 

#include <stdio.h> #include <stdlib.h> #include <limits.h>  int main(void){     long int a[100000], max = long_min;     int n;      scanf("%d", &n);     for(int = 0; < n; i++){         scanf("%lu", &a[i]);         if(a[i] > max)             max = a[i];     }     long long int sum = 0;     for(int = 0; < n; i++){         sum += labs(a[i] - max);     }     printf("%lld\n", sum);      return 0; } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -