problem link : https://www.codechef.com/problems/MAXDIFF

please help

my code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,k,t,i,j,swap,sum1=0,sum2=0;
    scanf("%d",&t);
    while(t--)
    {
        sum1=0,sum2=0;
        scanf("%d%d",&n,&k);
        int w[n];
        for(i=1; i<=n; i++)
        {
            scanf("%d",&w[i]);
        }
        for(i=1; i<=n-1; i++)
        {
            for(j=1; j<=n-i-1; j++)
            {
                if(w[j]>w[j+1])
                {
                    swap=w[j];
                    w[j]=w[j+1];
                    w[j+1]=swap;
                }
            }
        }
        if(k>n-k)
        {
           k=n-k;
        }
        for(i=1;i<=k;i++)
        {
            sum1=sum1+w[i];
        }
        for(i=k+1;i<=n;i++)
        {
            sum2=sum2+w[i];
        }
        int total=sum2-sum1;
        printf("%d\n",abs(total));
    }
    return 0;
}

asked 08 Jun, 09:02

Rejwan's gravatar image

Rejwan
1056

Be the first one to answer this question!
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×58

question asked: 08 Jun, 09:02

question was seen: 172 times

last updated: 08 Jun, 09:02