Magical Doors codechef solution in c++

#include <iostream>
using namespace std;

int main() {
    int T;
    cin >> T;

    while (T--) {
        int N, K;
        cin >> N >> K;

        int count = 0;
        for (int i = 0; i < N; i++) {
            int S;
            cin >> S;
            if (S % 2 == 1) {
                count++;
            }
        }

        if (count >= K && ((K % 2 == 1 && count % 2 == 1) || (K % 2 == 0 && count % 2 == 0))) {
            cout << "YES\n";
        } else {
            cout << "NO\n";
        }
    }

    return 0;
}

Explanation:

  • #include <iostream>: Includes the input/output stream library.
  • using namespace std;: Avoids writing std:: before standard methods like cin and cout.
  • int main() { ... }: The main function where the execution of the program starts.
  • int T; cin >> T;: Reads the number of test cases.
  • while (T--) { ... }: Loops through each test case.
  • int N, K; cin >> N >> K;: Reads the values of N (number of doors) and K (minimum odd doors to open).
  • int count = 0;: Initializes a counter for odd doors.
  • for (int i = 0; i < N; i++) { ... }: Loops through each door.
  • int S; cin >> S;: Reads the state of the current door.
  • if (S % 2 == 1) { count++; }: Increments the count if the door's state is odd.
  • if (count >= K && ((K % 2 == 1 && count % 2 == 1) || (K % 2 == 0 && count % 2 == 0))) { ... }: Checks if the condition to open the doors is met.
  • count >= K: Ensures that the count of odd doors is greater than or equal to K.
  • ((K % 2 == 1 && count % 2 == 1) || (K % 2 == 0 && count % 2 == 0)): Ensures that if K is odd, the count of odd doors is also odd, and if K is even, the count of odd doors is even.
  • cout << "YES\n";: Prints "YES" if the condition is met.
  • cout << "NO\n";: Prints "NO" if the condition is not met.
  • return 0;: Indicates successful execution of the program.