Arshiya's First Code

Limits 2s, 512 MB

Arshiya is just three and she already started programming. She has learned how to write loops. Recently, she found a simple problem written in the exercises segment of her programming book. The problem says, “Given a positive integer N, find the number of odd numbers from 1 to N inclusive”. Arshiya thought to herself, “It’s a piece of cake” and wrote down the following code to solve the problem:

This is a companion discussion topic for the original entry at https://toph.co/p/arshiya-s-first-code

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. Please say it if your submission was accepted 21 hours ago. Anyways congrats on your accepted submission.