-3

The question was

A semester in Chef's University has 120 working days. The University's requirement is that a student should be present for at least 75% of the working days in the semester. If not, the student is failed.

Chef has been taking a lot of holidays, and is now concerned whether he can pass the attendance requirement or not. N working days have already passed, and you are given N bits - B1​, B2​, ..., BN​. Bi​ = 0 denotes that Chef was absent on the ith day, and Bi​ = 1 denotes that Chef was present on that day.

Can Chef hope to pass the requirement by the end of the semester?

Input:

First line will contain T, the number of testcases. Then the testcases follow. Each testcase contains two lines of input. The first line of each testcase contains a single integer, N, the number of days till now. The second line of each testcase contains a string BB of length N where Bi​ represents the status of the ith day.

Output:

For each testcase, output the answer in a single line - "YES" if Chef can pass the attendance requirement and "NO" if not.

And i came up with this code

#include <iostream>
using namespace std;

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

    while (t--) {
        int n;
        cin >> n;
    
        int a[n];
        int cnt = 0;
    
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            if (a[i] == 1) {
                cnt++;
            }
        }
    
        if (cnt >= n-30) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }

    return 0;
}

I don't know what's wrong with this code.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 3
    `int a[n];` isn't valid c++ code: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard?r=Saves_AllUserSaves – πάντα ῥεῖ Mar 11 '23 at 10:16
  • 2
    @πάνταῥεῖ - that's pedantic and not the OP's issue. It's well known that g++ happily accepts variable sized arrays. – selbie Mar 11 '23 at 10:36
  • 1
    @selbie and g++ was mentioned as specific compiler where in this question? Or what specifically is the OPs problem with that code?? – πάντα ῥεῖ Mar 11 '23 at 10:39
  • @selbie how do you know that isn’t ops issue? Do you know his compiler? Do you know the number of test cases won’t blow the stack? Do you know even if their compiler supports this as an extension that it’s not been disabled with pedantic or similar arguments. – Mike Vine Mar 11 '23 at 10:44
  • 1
    @MikeVine - there's a presumption that the OP's code compiles. Also, all of those leet coding sites use g++. – selbie Mar 11 '23 at 10:50
  • *"The second line of each testcase contains a **string** BB of length N"* So, why not use a `std::string`? You could probably also read a `char` at a time and count the `1`s, but reading N `int`s is probably not the way to go. Please provide an example of input. – Bob__ Mar 11 '23 at 10:52
  • *"I don't know what's wrong with this code."* -- why do you think there is something wrong with this code? What are the symptoms? What have you done to debug the issue so far? – JaMiT Mar 11 '23 at 10:57
  • If [this](https://www.codechef.com/problems/ATTENDU) is what you are trying to solve, look at the sample and ask yourself how could you read `00000000000000000000000000000000000000000000000000` – Bob__ Mar 11 '23 at 11:01
  • Programmers spend much of their professional lives in a state of 'I don't know what's wrong with this code'. You really will learn C++ much faster if you learn how to debug your own code. Just staring at the code and saying 'I don't know what is wrong with it' is for newbies. Learn how to use a debugger first of all. – john Mar 11 '23 at 11:44

1 Answers1

1

I see the problem.

You are presented a test case such as the following:

 5
 11111

But are attempting to read it in "integer by integer". Since a[i] is an int cin >> a[i] will read that in as the number 11111 and not as a single 1.

Instead of reading one integer at a time. Read it one character at a time. And you don't need the a array either since you are just counting the number of occurences of 1. Also note the comparison of == '1' (comparison to a char literal) instead of the integer comparison == 1

This should fix it:

    while (t--) {
        int n;
        int cnt = 0;

        cin >> n;
    
        for (int i = 0; i < n; i++) {
            char c;
            cin >> c;
            if (c == '1') {
                cnt++;
            }
        }
    
        if (cnt >= n-30) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }

Also, wouldn't it be easier to just count the number of occurrences of the the character '0' and test if it's more than 30? A quick reverse of your logic:

    while (t--) {
        int n;
        int cnt = 0;

        cin >> n;
    
        for (int i = 0; i < n; i++) {
            char c;
            cin >> c;
            if (c == '0') {
                cnt++;
            }
        }
    
        if (cnt <= 30) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
selbie
  • 100,020
  • 15
  • 103
  • 173