TWTCLOSE - Editorial

most welcome :slight_smile:

#include<string.h>
#include<stdlib.h>
void main()
{
int t,n,p,q,i,j,k,x;
char a[8];
scanf("%d %d",&t,&n);
p=(int
)calloc(t,sizeof(int));
q=(int
)calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
k=0;
scanf("%s",a);
if(strcmp(a,“CLICK”)==0)
{
scanf("%d",&x);
if(p[x-1]==1)
p[x-1]=0;
else
p[x-1]=1;
}
else{
for(j=0;j<t;j++)
p[j]=0;}
for(j=0;j<t;j++)
if(p[j]==1)
k++;
q[i]=k;
}
for(i=0;i<n;i++)
printf("%d\n",q[i]);
}

Is there any error in my code???
It works fine when i run it in my system but it is giving a runtime error when i submit it!! :confused:

import java.io.*;
import java.util.ArrayList;
class TWTCLOSE{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out, true);
String str[] = br.readLine().split(" “);
ArrayList al = new ArrayList(),ans = new ArrayList();
int tweets = Integer.parseInt(str[0]);
int clicks = Integer.parseInt(str[1]);
while(clicks–>0){
int twtnum;
String twtstr[] = br.readLine().split(” ");
if(!“CLOSEALL”.equals(twtstr[0])){
twtnum = Integer.parseInt(twtstr[1]);
if(al.contains(twtnum))
al.remove(twtnum);
else
al.add(twtnum);
}
else
al.clear();
ans.add(al.size());
}
for(int a:ans)
pw.println(a);
}
}

https://www.codechef.com/viewsolution/8138468
Please have a look at the above link. I need some help. What I did?
I simply calculated the length of the choice-“7 for CLICK X” otherwise “CLOSEALL”.
If len==7,then I just checked choice[6] what is it’s numeric value.To calculate the numeric value I simply did this: choice[6]-‘0’. But it showed wrong answer. I will be thankful to anyone who will take some patience and will help me for this. I will learn something which I should have known till now. Thank You. :slight_smile:

i cant find out my mistake…can anyone help me…

#include<stdio.h>
#include<string.h>
#include<assert.h>

 void closeAll(int t[],int n)
{
int i;
 for(i=0;i<=n;i++)
{
	t[i]=0;
}
printf("%d\n",countOpen(t,n));
  }


   void toggle(int t[],int pos,int n)
  {
  if(t[pos]==0)
	t[pos]=1;
else
	t[pos]=0;
	
printf("%d\n",countOpen(t,n));
 }
 int countOpen(int t[],int n)
{
int i,count=0;
for(i=1;i<=n;i++)
{
	if(t[i]==1)
		count++;
}

return count;
   }
   int main()
   {
int t[1000]={0},n,k,x=0;
char operation[10];
scanf("%d",&n);
assert(n>0&& n<1001);
scanf("%d",&k);
assert(k>0&&k<1001);
while(k){
						scanf("%s",operation);
						if(strcmp("CLICK",operation)==0)
						{
							scanf("%d",&x);
							//printf("val x=%d",x);
							assert(x>0&&x<=n);
							toggle(t,x,n);
						}
						else
							closeAll(t,n);
					k--;
				}





return 0;

}

please help me out in this problem name “Closing the Tweets”. I am getting wrong output error for my solution

I have used a Set for inserting and deleting of elements which requires O(logN) for both operations and clear for closing all the tweets, which takes O(N) hence, overall it took O(NK) time. Even though my solution got AC in 0.0 seconds, still O(NK) is more for this problem right?

On what test case this code fails?

include

int main()
{
int n,k,i;
scanf("%d %d",&n,&k);
char s[10];
int a[n];
for(i=0;i<n;i++)
a[i]=0;

while(k–){
int flag=0,y=0,m,num=0;
scanf (" [^\n]c",s);
for(i=6;s[i]!=’\0’;i++)
{
m=s[i]-‘0’;
num=num
10+m;
}

if(num>=1 && num<=n)
{
if(a[num-1]==0)
a[num-1]=1;
else
a[num-1]=0;
}
else{
y=0;
flag=1;
}

if(flag==0){
for(i=0;i<n;i++)
{
if(a[i]==1)
y++;
}
}
printf("%d\n",y);
}
}

Link to my solution :

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

1 Like

Why is this problem placed among medium difficulty problems? It looks trivial to me and should belong to the beginner problems.

#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

https://www.codechef.com/viewsolution/16737490 correct

this got correct when i didn’t.

https://www.codechef.com/viewsolution/16737259 is not correct

can anyone help me finding out why.

Can anyone help why I am getting a wrong answer?
My solution is https://www.codechef.com/viewsolution/18952476

I am unable to understand what is wrong with this solution : https://www.codechef.com/viewsolution/19271123
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