MAXDIFF - Editorial

For which case is the solution giving wa–

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

pls someone explain ?

#include
using namespace std;
#include
int main()
{
int t;
scanf("%d",&t);
while(t–)
{
int n,k;
scanf("%d%d",&n,&k);
int arr[n];
for(int i=0 ; i<n ; i++)
scanf("%d",&arr[i]);
sort(arr,arr+n);
int s1,s2;
s1=s2=0;
for(int i=0;i<n;i++)
{
if((i+1)<=k)
s1+=arr[i];
else
s2+=arr[i];
}
int ans;
if(s1>s2)
ans=s1-s2;
else
ans=s2-s1;
printf("%d\n",ans);
}
return 0;
}

https://www.codechef.com/viewsolution/13185103
please tell me what is wrong?

testcase=int(input())
while(testcase>0):
child=0
chef=0
lst=[int(i) for i in input().split()]
weights=[int(i) for i in input().split()]
weights.sort()
for i in range(0,len(weights)):
if(i<lst[1]):
child+=weights[i]
else:
chef+=weights[i]
print(chef-child)
testcase-=1

Can anybody will tell me why am i getting wrong answer?

Can someone tell me why am I getting the wrong answer for this code
while(t–>0)
{
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Arrays.sort(a);

		int chef=0;
		int child=0;
		if(n-k>=k)
		{
			for(int i=0;i<k;i++)
				child=child+a[i];
			for(int i=k;i<n;i++)
				chef=chef+a[i];
		}
			else
			{
				for(int i=0;i<n-k;i++)
					child=child+a[i];
				for(int i=n-k;i<k;i++)
					chef=chef+a[i];
			}
			System.out.println(chef-child);
			
		
	}

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

why is this getting WA

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

Why am I getting WA?

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int i,n,k;
long long int c=0,d=0;
cin>>n>>k;
long int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
int m=sizeof(a)/sizeof(a[0]);
sort(a,a+m);
for(i=0;i<k;i++)
{
c+=a[i];
}
for(i=k;i<n;i++)
{
d+=a[i];
}
int r=((c>d)?(c-d):(d-c));
cout<<r<<endl;
}
}

why i am getting WA

1 Like

Guys, help me please. Why my solution gets WA? I am sure that it my solution is correct! CodeChef: Practical coding for everyone

my approach:-

  1. sort the entire array of weights and find the total sum of all elements (let’s denote by S)
  2. find total sum of first k elements in the sorted array (let’s denote it by S0)
  3. max difference will be (S-S0)//fathers weight - (S0) //child weigth

can’t quite find the error in this
what am I missing ?

why is this showing wrong answer?

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

Hey karbish98 here is the solution to your question
https://discuss.codechef.com/questions/80572/maxdiff-question-problem

More over I have upvoted your question so that you can now ask question on your own and upvote and participate in codechef discuss and community rather
that posting questions in answer column of editorials.

Consider test case:

1 2 3 4 5 6 7 8 9 10 , k = 8 , n = 10

There are two possibilities:

case 1 index(0:k and k:):

group 1: [1,2…8] sum = 36

group 2: [9,10] sum = 19

diff_case1 : 17

case 2 index (0:n-k and n-k:):

group1: [1,2] sum = 3

group2: [3,4…10] sum = 52

diff_case2: 49

output: max(diff_case1,diff_case2) = 49

Here is the Python code:

for _ in range(int(input())):
n,k = map(int,input().split())
l = sorted(list(map(int,input().split())))
print(max(abs(sum(l[:k])-sum(l[k:])),abs(sum(l[:n-k])-sum(l[n-k:]))))
5 Likes

/*

  • cc_practise_maxdiff.cpp
  • Created on: Aug 19, 2019
  •  Author: ankit
    

*/

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
int main(){
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
int t, n, k;
ull sum, sum_k, sum_nminusk;
cin >> t;

vector<int> wt(101,0);
while(t--){
	sum =0;
	sum_k = 0;
	cin >> n >> k;
	for(int i=1 ; i<= n ;i++){
		cin >> wt[i];
		sum += wt[i];
	}
	sort(wt.begin()+1, wt.begin()+1+n);
	for(int i = 1; i <= k ;i++){
		sum_k += wt[i];
	}
	sum_nminusk = sum - sum_k;
	if(k > n-k){
		// implies sum of k elements might be
		// greater than sum of n-k elements
		// so in such case assign child n-k elements
		if(sum_k > sum_nminusk){
			cout << sum_k - sum_nminusk << "\n";
			continue;
		}
	}
	cout << sum_nminusk  - sum_k << "\n";
}
return 0;

}

Can anyone please tell me the test case where my code fails to give correct output ??

/* 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