Arshiya's First Code

Arshiya is just three and she already started programming. She has learned how to write loops. Recen…

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”.

the correct code in c-

#include <stdio.h>

int main() {
long int N,countodd;
scanf(“%li”,&N);
if(N%2==1){countodd=(N-1)/2+1;}
else{countodd=N/2;}
printf(“%li”,countodd);
return 0;
}

#include<stdio.h>
int main()
{

 int N;
 scanf("%d",&N);
 int countOdd = 0;

 for (int i = 0; i < N; i++) {
        if ((i+1)%2 == 1) {
              countOdd++;
        }
 }
 printf("%d\n", countOdd);
 return 0;

}
whats wrong?

I think you are just calculating the average. Read the problem statement again carefully and see the sample input outputs then try again.

1 Like

Use long long data type. And including <iostream> instead of <bits/stdc++.h> maybe work. However, you can solve it without defining any function.

1 Like

Let me share my thoughts with you.

Ok…it is a range from 1 to n inclusive and at least half of them will be odd and half of them will be even! now if the range is odd i.e. my res variable is odd then odd number will be more unless exactly half of the number will be odd.

I hope you are not regarding my theory as a trash .

and thanks @imamanik05

Try out your theory on paper and see if it works. Take some numbers and manually calculate the summation of their odd ones. Now try to do the same thing using your method. Check if these matches.

Man! The problem is not about finding their summation. its about finding how many odds are there in the range.

Oh sorry! I misread the problem statement!

The problem is not in your theory but how you are implementing it. In an inclusive range if you deduct the lower value fron the upper value sometimes you might lose the lower value itself! Let me give you an example. Assume that you want to know the number of number in the range 2 to 4 inclusive. Now there are 3 numbers which are 2, 3 and 4. However, if you do 4 - 2 then the answer would be 2! Meaning that you are losing a number in your calculation!
You have done the same thing here. Check if you have lost any number in your calculation.

I have said half of them not the R-L and I have also got AC

Try a couple of more times cause I think i have given you all the clues you need. It got half the test cases right cause your code will generate AC for half of the cases. Try to fix the rest yourself.

I have got AC…with my theory…

Oh ok. So you got AC. :sweat_smile: Please say it if your submission was accepted 21 hours ago. Anyways congrats on your accepted submission. :smiley: