Poltu and Odd Number

Today is Poltu’ first C programming class. And, he needs your help with a programming task. Given tw…

Click here to read the complete problem statement.


If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

#include<bits/stdc++.h>
using namespace std;

int main()
{
unsigned long long int n,m,t,div=0;
cin >> t;
for(int i=1;i<=t;i++)
{
cin >> n >> m;
if(n==1)div=m/2;
else div=(m-n)/2;

if(m%2!=0)div++;
else m--;

if(n%2==0)n++;
cout<<"Case "<<i<<": "<<((n+m)*div)/2<<endl;
div=0;

}
}

What is problem??

whats the problem in my code.
it shows: CPU limit exceeded on test 2

#include <iostream>
//#include <iomanip>
//#include <cmath>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <=n; i++)
    {
        int a, b;
        int odd_sum=0;


        cin >> a >> b;
        for (int j = a; j <= b; j++)
        {
            if (j%2!=0)
            {
                odd_sum += j;
            }
            
            
        }
        cout << "Case " << i << ": " << odd_sum << endl;
    }
    
    return 0;
}

1 Like

Your solution is naive and will take too long to calculate the summation. You will need to think of a better way.

Try to think of a way to calculate the sum without looping over all the numbers.

1 Like

Any hint pls??? i tried my best… give me a hint … which method I should try !!!

Why Second testCase Wrong Ans
Code Here
#include<stdio.h>
int main(){
int L,R,N,count,ck=0;

scanf("%d",&N);
while(N--){
    scanf("%d %d",&L,&R);
    if(R%2==0){
        R--;
    }if(L%2==0){
       L++;
    }
    count=((R-L)/2)+1;
    sum=((R+L)/2)*count;
    ck++;
    printf("Case %d: %d\n",ck,sum);
}

}