INTEG - Editorial

  • List item
  • #include<stdio.h>
    #include<stdlib.h>
    long long int sum=0;
    int count(int a[],int x,int n)
    {
    long long int i,k=0;
    for(i=0;i<n;i++)
    {
    if(a[i]<0) k++;
    }
    if(x<=k)
    {
    for(i=0;i<n;i++)
    {
    a[i]=a[i]+1;
    }
    sum = sum + x;
    count(a,x,n);
    }
    else if(x>k)
    {
    for(i=0;i<n;i++)
    {
    if(a[i]<0)
    {
    sum = sum + abs(a[i]);
    }
    }
    //printf("%d “,sum);
    }
    return sum;
    }
    int main()
    {
    int n,i,x,a[10005];
    scanf(”%d",&n);
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    scanf("%d",&x);
    if(x!=0)
    {
    sum = count(a,x,n);
    printf("%d",sum);
    }
    else if(x==0)
    printf("%d",0);
    return 0;
    }

why this code is giving run time error

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

what is wrong with my solution?
khopri udd gaya

What is wrong with my code?CodeChef: Practical coding for everyone

test cases contain an array with at least one negative element. so, 0 as an answer is not possible.
this is my solution using prefix sum and sorting.
link text

All links to the solutions broken

1 Like

Change the statement :-
long long int temp = v[v.size()-cost] * cost
to
long long int temp = v[cost-1] * cost, then it will be AC…)

2 Likes

Sorry, that was a typo. it should be K ≥ X. Corrected. Thanks for pointing out.

for which test case does my [solution][1] fail?
[1]: CodeChef: Practical coding for everyone

You need to store answer in long long as it can exceed limits of int

Thanks here’s the [modified solution][1] but I’m still getting WA. I need the test case where it fails
[1]: CodeChef: Practical coding for everyone

did not handle x = 0 case

2 Likes

That solved the problem… Thanks!

Looks like abs in C works only for 32 bit numbers.


if(x >= count_negetive)
		return abs(sum_negetive);

was the curlprit. Changing it gets AC

1 Like

Thank you so much for your help @utkarsh_lath :diamonds::diamonds:

really a neat approach… :slight_smile:

1 Like

Yup!.. There is a nice logic behind STEP 3… :stuck_out_tongue:

Yup!

I did the same thing. However I am getting TLE for such a simple code. Might be JAVA

Why are you doing input.split(" ") in for loop?

1 Like

JAVA cannot be the problem for the TLE , if you are applying the same approach as described above…

I tried the same approach using JAVA couple of days back…And it was AC well within the time limit… :slight_smile:

See this : CodeChef: Practical coding for everyone

There might be some other problem for the TLE…!!

Oops! That was so stupid and careless of me. I don’t have much experience with JAVA. I did changed that particular code to

    String[] temp = input.split(" ");
    for(int i = 0; i<n;i++){
        arr[i] = Long.parseLong(temp[i]);
    }

Now solution is accepted with a nice time of 0.84. Thank you for the help.

1 Like