Mohammad Azmee Tofail
Mohammad Azmee Tofail

Reputation: 31

C programming output doesn't match with logic (maybe Type Casting)

I'm trying to solve this problem at CodeForces:

There is an array a of the size 1e9 filled with natural numbers with alternating sign:

  • a[1] = -1
  • a[2] = 2
  • a[3] = -3
  • a[4] = 4
  • a[5] = -5
  • and so on...

There are q queries (1 ≤ q ≤ 1000) each in the form of two numbers, l and r (1 ≤ l ≤ r ≤ 1e9). The answer to a query is the sum of all the elements of the array a at positions from l to r inclusive.

Here's my code:

#include <stdio.h>

int solve(int l, int r)
{
    long int res;
    if ((l == r) && (l % 2 == 0)) {
        res = l;
        return res;
    } else if ((l == r) && (l % 2 != 0)) {
        res = l*(-1);
        return res;
    }
    long int sum, esum, osum, min = l/2, max = r/2;
    sum = (r*(r+1))/2 - ((l-1)*l)/2;
    if (l % 2 == 0) {
        min--;
    }
    esum = (max*(max+1)) - (min*(min+1));
    osum = (sum-esum)*(-1);
    res = esum + osum;
    return res;
}

int main()
{
    long int t, l, r;
    scanf("%ld", &t);
    while (t--) {
        scanf("%ld %ld", &l, &r);
        long res = solve(l, r);
        printf("%ld\n", res);
    }
    return 0;
}

This code doesn't give an applicable result for a very large value. But it's OK for normal values. What could be the error?

Input

6
617758920 825919887
775957146 950878973
404173573 553845184
25837072 795166931
756434592 838258528
590139756 977664562

My Output

-104080484
2060022734
74835806
1762818718
797346560
783902159

Correct Answer

-104080484
-87460914
74835806
-384664930
797346560
783902159

Upvotes: 3

Views: 108

Answers (2)

NIKhil kumar
NIKhil kumar

Reputation: 32

The edited code

    #include<stdio.h>
    int solve(int l, int r){
        long int res;
        if((l == r) && (l % 2 == 0)){
            res = l;
            return res;
        }else if((l == r) && (l % 2 != 0)){
            res = l*(-1);
            return res;
        }
        long int sum=0,i=0;
        for(i=l;i<=r;++i)
        {
            if(i % 2 == 0)
                sum+=i;
            else
                sum-=i;
        }
        return sum;
    }
    int main()
    {
        long int t, l, r;
        scanf("%ld", &t);
        while(t--){
            scanf("%ld %ld", &l, &r);
            long res = solve(l, r);
            printf("%ld\n", res);
        }
        return 0;
    }

the problem in your code is with somewhat range of long and it arises on the sum line itself. However dry run gives a succesfull result for your codedry run

Upvotes: 0

CiaPan
CiaPan

Reputation: 9571

Are you sure you need those squares...?

Each pair of consecutive odd- and even-indexed term gives a partial sum of 1. So all you need is a number of such pairs fitting within the given interval plus possibly a[l] if l is even and a[r] if r is odd.

Solution:

long solve(long l, long r)
{
    long res = r/2 - l/2;
    if (l % 2 == 0)
        res += l;
    if (r % 2 != 0)
        res -= r;

    return res;
}

Upvotes: 1

Related Questions