< Back to forum

Codechef problem TWTCLOSE

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int N,K,i,count=0,X;
    string s;
    int a[1000]={0};
    cin >> N >> K;
    for (i=0;i<=K;i++)
    {
        cin >> s;
        if (s[2]=='I')
        {
            cin >> X;
        if (a[X]==0)
        {
        a[X]=1;
        count++;
        }
        else
        {
        a[X]=0;
        count--;
        }
        cout<<count<<endl;
    }
    else if (s[2]=='O')
    {
    count=0;
    for(i=1;i<=N;i++)
    a[i]=0;
    cout<<count<<endl;
    }
    }
    return 0;
}

Asked by: Rishav_Mundhra on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




2 Answer(s)

avatar

Check your 1st loop for i, it is wrong it should be-

for(i=0;i<k;i++)

 

Anand_Jhunjhunwala last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

avatar

Read the constraint carefully. Your array size is not enough. As n can get maximum value equal to 1000 and your size of array is 1000 so your last index is 999.

rishup132 last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.