Header Ad

HackerEarth Easy Going (Very Easy) problem solution

In this HackerEarth Easy Going(Very Easy) problem solution 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.


HackerEarth Easy Going.(Very Easy) problem solution


HackerEarth Easy Going.(Very Easy) problem solution.

#include <iostream>

using namespace std;

void input (int arr[], int n) {

for (int i = 0; i < n; i++)

cin >> arr[i];

}
void sort (int a[], int s) {

for (int i = 0; i < s - 1; i++) {

for (int j = 0; j < s - i - 1; j++) {

if (a[j] > a[j + 1]) {

int temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

}

}

}

}




int main()

{

int n, m, t, arr[1000];

cin >> t;



for (int i = 0; i < t; i++) {

cin >> n >> m;

input (arr, n);

sort (arr, n);

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

for (int j = 0; j < k; j++){

max += arr[n - j - 1];

min += arr[j];

}

cout << max - min << endl;

}

return 0;

}



Post a Comment

0 Comments