TWTCLOSE - Editorial

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:

I did it in a similar way and the answer is submitted

#include
#include
#include
#include
using namespace std;
int main ()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin>>n>>k;
std::string str;
std::bitset<1000> boo;
while(k–){
cin>>str;
if(str ==ā€œCLOSEALLā€){
boo.reset();
cout<<0<<"\n";

}
else{
    int c;
    cin>>c;
    boo = boo.flip(1000-c);
    cout<<boo.count();
    cout<<endl;
}

}

return 0;
}

Can anyone help me out here? I’m getting wrong answer status. What’s wrong in my code?
https://www.codechef.com/viewsolution/25035176

you didn’t consider elements bigger than 9
your code will fail on those test cases

1 Like

Thank you for pointing it out. It was kind of a silly mistake. Here’s the correct one.
https://www.codechef.com/viewsolution/25048355

This code is giving NZEC error Please help me :woozy_face:

class TWTCLOSE {

    int result = 0;
static int Program(String str, int val, int[] arr) {
	if (arr[val] == 0) {
		arr[val] = 1;
		result++;
	} else {
		arr[val] = 0;
		result--;
	}
	return result;
}

public static void main(String[] args) throws IOException {
	java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
	int n = 0;
	int k = 0;
	n = r.read();
	r.readLine();
	k = r.read();
	r.readLine();
	int[] arr = new int[Character.getNumericValue(n)];
	k = Character.getNumericValue(k);
	if (n >= 1 && k <= 1000) {
		while (k > 0) {
			String str = r.readLine();
			int length = str.length();
			str.trim();
			if (str.equalsIgnoreCase("closeAll")) {
				System.out.println("0");
				Arrays.fill(arr, 0);
				result = 0;
			} else {
				int val = Integer.parseInt((str.substring(length - 1, length)));
				System.out.println(Program(str, val-1, arr));
			}
			k--;
		}
	}
}

}

This is how I did it in Java.

class Codechef {

  public static void main (String[]args) throws java.lang.Exception
  {
    // your code goes here
    Scanner in = new Scanner(System.in);
    int n=in.nextInt();
    int k = in.nextInt();
    in.nextLine();
    int ans=0;
    int[] a = new int[n];
    for (int i=0;i<k;i++)
    {
        
        String s=in.nextLine();
        if (s.charAt(2)=='O')
        {
            ans=0;
            a = new int[n];
        }
        else
        {
            int l=s.length();
            String numstr=s.substring(6);
            int num=Integer.parseInt(numstr);
            
            
            if (a[num-1]==0)
            {
                a[num-1]=1;
                ans++;
                
            }
            else 
            {
                a[num-1]=0;
                ans--;
            }
        }
        System.out.println(ans);
    }
  }
}

I am getting TLE using React Js. I am new to this language. Can anyone help ?
My code

x,t = map(int,input().split())
st=""
while(t):
s = input()
if(ā€œALLā€ in s):
st=""
else:
for i in range(1,x+1):
if(str(i) in s):
if(str(i) in st):
st = st.replace(str(i),"",1)
else:
st+=str(i)
# print(st)
print(len(st))
t-=1

what is wrong in this pls help me