Don't understand where my logic is failing

Link to my solution and problem : https://www.codechef.com/viewsolution/62231492

My logic : If N is even, N/2 will be even. There will be even number( i.e. N/2) of odd numbers and even numbers in the series 1, 2, 3,…,N. So, the length of sub-array will be N.

If N is odd, N/2 can can even or odd. If N/2 is even there will be odd number of odd numbers in the series and hence the length of sub-array would be N-1. If N/2 is even, length of sub-array would be N.

There is 1 special case where N=2 for which length of sub-array would be 1.

Hey, this one seems like a running contest. Please refrain from posting solution of a running contests publicly.
Also, while the contests has not ended, you are supposed to solve the problem on your own.

Once the contest has ended and if you are still unable to solve the problem, we are happy to help :).

It already has a video solution in editorial. The problem is part of self-learning program on CodeChef. I understand the solution they have given but didn’t understand why my logic was failing some of the test cases. My solution was passing the sample test cases.

Hey @gangwar_98 :wave:
Your given link is not working can you please share your code.

@amiytiwari_adm idk link is working for me. So, I have shared the code below :

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <cmath>
using namespace std;

#define ll  long long int

int main() {
   int t;
   cin >> t;
   while(t--) {
      int n;
      cin >> n;
      if(n==2) {
          cout << "1" << endl;
      } else if(n%2==0) {
          cout << n << endl;
      } else {
          if((n>>1)%2==0) {
              cout << n-1 << endl;
          } else {
              cout << n << endl;
          }
      }
   }
}

Please share the problem link too. Solution of the problem can be private so it only visible to you only.

@amiytiwari_adm Sorry about that…this is my first time posting. Here is the problem link : Contest Page | CodeChef

Hey @gangwar_98 :wave:
In case of n being even your code will give wrong output as it not necessary to sum of all n number be even.
take the example n = 6 , in this sum is 21 which is not even but your code will give 6 as answer

1 Like

Hi @amiytiwari_adm
Oh I see the fault. Thank You for explaining.

if n is even, n/2 does not need need to be even
take 6 for example.