TWTCLOSE - Editorial

#include
#include
using namespace std;

int change(int* arr, char c,int n)
{
int sum=0;
arr[int©-49]=(arr[int©-49])?0:1;
for(int i=0;i<n;i++)
sum+=arr[i];

return sum;

}

int main()
{
int n,k;
string s1,s2;
char c;
cin>>n>>k;
int arr[n];
for(int j=0;j<n;j++)
arr[j]=0;

for(int i=0;i<k;i++)
{
	cin>>s1;
	if(s1=="CLICK")
	{
		cin>>s2;
		c=s2.at(0);		
		cout<<change(arr,c,n)<<"\n";
	}
	if(s1=="CLOSEALL")
	{
		for(int j=0;j<n;j++)
			arr[j]=0;
		cout<<"0\n";
	}	
}

}

Why am I getting wrong answer?

https://www.codechef.com/viewsolution/10581225

Why is this getting WA?

can anyone please point out my mistake in this solution, it gives wrong ans on submission.
https://www.codechef.com/viewsolution/11548341

its showing wrong answer but works fine. can anyone please tell me whats wrong?

def checker(x):
    p = 0
    for i in range(len(x)):
        if x[i] == 1:
            p += 1
    print(p)
 
 
def end(x):
    return x[-1:]
n,k = input().split()
n,k = int(n),int(k)
 
tweetstatus = []
while n>0:
    tweetstatus.append(0)
    n -= 1
while k>0:
    s = str(input())
 
    if s == 'CLOSEALL':
        for i in range(len(tweetstatus)):
            tweetstatus[i] = 0
 
    else:
        z = int(end(s))
 
        if tweetstatus[z-1] == 0:
            tweetstatus[z-1] = 1
        else:
            tweetstatus[z-1] = 0
 
    checker(tweetstatus)
 
 
 
    k-= 1

https://www.codechef.com/viewsolution/15965360

Can anyone tell me what’s wrong in my solution? It’s showing Runtime error.

My solution is working correctly with the sample test case(after running on my system)
But not working after submission.
Please help me out!

#include<iostream>
#include<string>
using namespace std;
int main(){
    int N,K;
    cin>>N>>K;
    string choice;
    int in,count = 0,arr[N];
    for(int i = 0;i < N;i++)
        arr[i] = 0;
    while(K--){
        cin>>choice;
        if(choice.compare("CLICK")==0){
            cin>>in;
            if(arr[in-1]){
                arr[in] = 0;
                count--;
            }
            else{
                arr[in-1] = 1;
                count++;
            }
        }
        else if(choice.compare("CLOSEALL")==0){
            for(int j = 0;j < K;j++)
                arr[j] = 0;
            count = 0;
        }
        cout<<count<<endl;
    }
}

I applied the same approach

while it gets wrong when i use getline

CodeChef: Practical coding for everyone correct

this got correct when i didn’t.

CodeChef: Practical coding for everyone is not correct

can anyone help me finding out why.

Can anyone help why I am getting a wrong answer?
My solution is CodeChef: Practical coding for everyone

I am unable to understand what is wrong with this solution : CodeChef: Practical coding for everyone
When i run this program it shows : Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 7
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:44)
at java.base/java.lang.String.charAt(String.java:692)
at numberOfOpenTweets.main(numberOfOpenTweets.java:16)
So if you can spot the error please tell

Memset would also take O(N) time to go through each element. But yes, the actual time might be lesser.
For every CLOSEALL, we do not do anything if number of tweets is 0. That could help but doesn’t lower worst case time.

I used memset for filling 0s. My solution was showing wrong answer though I have implemented it same as mentioned in the tutorial.
http://www.codechef.com/viewsolution/1132512
Can’t find what is the difference :frowning:

@vikram535 : Please check the ‘memset’ function in the library. The last parameter should be the number of bytes, and not number of elements in the array. If your array is int A[1001]; , you should use it as, memset(A,0,sizeof(A));

1 Like

You meant for example O(1/2 * N)? In fact that doesn’t matter, it’s asymptotically O(N).

Thank you so much for pointing out my mistake. I was not able to find it. Thanks again :slight_smile:

here is counter example - A6o21 - Online Java Compiler & Debugging Tool - Ideone.com

hey thnx…my mistake i didn’t observe dat…thnx a lot…

Your class cannot be public, that’s the first problem at CodeChef. In addition

int c = Integer.parseInt(t1.substring(n - 1, n));
c = c - 1;

smells - J3TYWf - Online IDE & Debugging Tool - Ideone.com

@ajinkya1p3

You forgot to print number of open tweets after input “CLOSEALL”

I have added print statement and here’s the accepted version of your code

http://www.codechef.com/viewsolution/5479172

1 Like

Thanks a lot!! :smiley:

most welcome :slight_smile: