Skip to content

Instantly share code, notes, and snippets.

@RohithKumar1368
Created August 27, 2017 13:10
Show Gist options
  • Save RohithKumar1368/88fa5bcd1d726b4f1bf62b43d613efc9 to your computer and use it in GitHub Desktop.
Save RohithKumar1368/88fa5bcd1d726b4f1bf62b43d613efc9 to your computer and use it in GitHub Desktop.
MAX-MIN SUM
// problem at https://www.hackerrank.com/challenges/mini-max-sum
// Simply sort the elements and add the first 4 elements for min
// and the last 4 for the max
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int *arr = malloc(sizeof(int) * 5);
long int sum_max = 0 ;
long int sum_min = 0 ;
for(int arr_i = 0; arr_i < 5; arr_i++){
scanf("%d",&arr[arr_i]);
}
// Bubble sort; can use a better sorting algorithm
for(int i = 0 ; i < 5 ; i ++){
for(int j = 0 ; j < 4 ; j++){
if(arr[j] > arr[j+1]) {
long temp = arr[j] ;
arr[j] = arr[j+1] ;
arr[j+1] = temp ;
}
}
}
for(int i = 0 ; i < 4 ; i++){
sum_min += arr[i] ;
sum_max += arr[i+1] ;
}
printf("%ld %ld\n",sum_min,sum_max) ;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment