MAXDIFF - Editorial

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bf.readLine());
while(t-- > 0) {
String str = bf.readLine();
String[] ar = str.trim().split(“\s+”);
int n = Integer.parseInt(ar[0]);
int k = Integer.parseInt(ar[1]);
str = bf.readLine();
ar = str.trim().split(“\s+”);
int[] arr = new int[n];
for(int i=0;i<n;i++) {
arr[i] = Integer.parseInt(ar[i]);
}
Arrays.sort(arr);
int childCount = 0;
int parentCount = 0;
int lastCount = 0;
int l = (n-1)-k;
for(int i = 0;i<n;i++){
if(i<k){
childCount+=arr[i];
parentCount+=arr[i];
}
else if((n-l)<=k && l<n){
lastCount+=arr[l];
parentCount+=arr[i];
l++;
}
else{
parentCount+=arr[i];
l++;
}
}
System.out.println(Math.max(Math.abs(childCount-(parentCount-childCount)),Math.abs(lastCount-(parentCount-lastCount))));
}
}
}

Please suggest me where my code is failing in test cases and why it’s giving WA… :slight_smile:

When things are simple keep it simple, you just take 2 variables
int childCount = 0;
int parentCount = 0;
Just make sure, k contains minimum number of items as follows :
k = min(k,n-k)
then sum the first k elements from the array and assign it to childCount
then sum the elements from k+1 to last value of the array and assign it to parentCount
then print the difference of parentCount and childCount

my solutions
#include
#include
#include
using namespace std;
int main()
{
int t;
cin >> t;
while (t–) {
int n, k; cin >> n >> k;
vectorv(n);
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
}

	sort(v.begin(), v.end());
	int l = 0;
	int r = 0;
	for (int i = 0; i < k; i++) {
		l += v[i];
	}
	for (int i = (n - 1); i >= (n - k); i--) {
		r += v[i];
	}

	if (abs(sum - 2 * r) > abs(sum - 2 * l))
		l = r;

	cout << abs(sum - 2 * l) << endl;
}
return(0);

}

by looking at your code i think u code i failing at this test case:
1
3 2
2 4 5
ans should be 7 but your code produces answer 1 in order to get AC u will have to calculate first from 0 to K then K to N and also from 0 to K-N then K-N to N. Compare both result and print greater one.

Your missing some points from the question.

  1. he suggests that the items are split into two groups, and one group contains exactly K items.
  2. Then Chef will carry the heavier group, and his son will carry the other group.

According to your code if k>(n-k) then heavier part is carried by son and smaller part is carried by chef, which is not true.

When k>(n-k) in this case assign k=n-k, Here

  1. we got the on group exactly contains k elements and
  2. Chef is carrying the heavier group .

corrected code snippet is

weights.sort()

if k>(n-k):
k=(n-k)

for i in range(0,len(weights)):

I hope my answer helped to solve This issue.

https://www.codechef.com/viewsolution/37315998

why it is showing wrong answer

https://www.codechef.com/viewsolution/37456584
please help me it show WA I don’t why??

Can someone please help me in this java code, I have solved it exactly like the editorial but I am getting a wrong answer.
https://www.codechef.com/viewsolution/39032571

C++ code passing all test cases: -

#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;
    while(t--) {
        int n, k;
        cin >> n >> k;
        int arr[n], S = 0;
        for(int i = 0; i < n; i++) {
            cin >> arr[i];
            S += arr[i];
        }
        sort(arr, arr+n);
        if(k < n-k)
            k = n-k;
        int S1 = 0;
        for(int i = n-1; k > 0; i--, k--)
            S1 += arr[i];
        cout << abs(S1 - (S - S1)) << "\n";
    }
    return 0;
}

This is how i have understood the editorial

There can be two ways to solve this problem

  • we can give the K lightest elements to the son
  • we can give the k heaviest elements to the father

so we need to compare both the ways

instead of doing that if we just make sure that we give the son the least number of elements then the objective is fulfilled so if (n-k) < k then we have to give n - k elements to the son

please correct me if i am wrong