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
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
@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));