# C Programming – HackerRank Solution | N-M Elements sorting |

### (HackerRank) Write a Modular C Programming code to solve N-M Elements sorting, Your task is to calculate the difference between the maximum sum and minimum sum of N-M elements

Coders here is a simple task for you, you have given an array of size N and an integer M.
Your task is to calculate the difference between the maximum sum and minimum sum of N-M elements of the given array.

Constraints:
1<=t<=10
1<=n<=1000
1<=a[i]<=1000

Input:
First line contains an integer T denoting the number of testcases.
First line of every testcase contains two integer N and M.
Next line contains N space separated integers denoting the elements of array
Output:

Sample Input

1

5 1

1 2 3 4 5

Sample Output

4

Explanation

M is 1 and N is 5 so you have to calculate maximum and minimum sum using (5-1 =) 4 elements.
Maximum sum using the 4 elements would be (2+3+4+5=)14.
Minimum sum using the 4 elements would be (1+2+3+4=)10.
Difference will be 14-10=4.

CODE:

```#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void sort(int a,int n)
{

int temp;

for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++) { if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

}
}
}
}

main()
{
int t,n,m,a,i;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,n);
}

int v,min=0,max=0;
v=n-m;

for(i=0;i<v;i++)
{
min=min+a[i];
}

for(i=n-v;i<n;i++)
{
max=max+a[i];
}

printf("%d",max-min);

}

```

OUTPUT

```Congratulations, you passed the sample test case.
Click the Submit Code button to run your code against all the test cases.

Input (stdin)```

1

5 1

1 2 3 4 5

```Your Output (stdout)

4
Expected Output

4```

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University 