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);
#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;
}
}
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.
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:]))))
#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 ??
/* 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…
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.