# Problem code:#CHARGES

i used this code and it gave me right output for given test cases but it is still showing wrong answer.I have checked the editorial and it seems like my logic is right.can somebody help me

``````#include <iostream>
using namespace std;

int main() {
long long int t;
cin>>t;
while(t--)
{
long long int k;
long long int n;
cin>>n>>k;
string s;
cin>>s;
long long int length;
for(long long int i=0;i<n-1;i++)
{
length=length+1+(s[i]==s[i+1]);
}

while(k--)
{
long long int q;
cin>>q;q--;

if(s[q]=='1')
s[q]='0';

else
s[q]='1';

if(q==0)
{
if(s[q]==s[q+1])
length=length+1;

else
length=length-1;
}

else if(q==n-1)
{
if(s[q]==s[q-1])
length=length+1;

else
length=length-1;

}

else{
if(s[q]==s[q+1])
length++;

if(s[q]==s[q-1])
length++;

if(s[q]!=s[q+1])
length--;

if(s[q]!=s[q-1])
length--;

}
cout<<length<<endl;
}
}
return 0;
}
``````

check this testcase:-
1
1 3
0
1 1 1

its output should be one or zero?

0
0
0

error even after i adjusted it for both output=1 and output=0

``````#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
long long int k;
long long int n;
cin>>n>>k;
if(n==1)
cout<<0<<endl;

else
{
string s;
cin>>s;
long long int length;
for(long long int i=0;i<n-1;i++)
{
length=length+1+(s[i]==s[i+1]);
}

while(k--)
{
long long int q;
cin>>q;q--;

if(s[q]=='1')
s[q]='0';

else
s[q]='1';

if(q==0)
{
if(s[q]==s[q+1])
length=length+1;

else
length=length-1;
}

else if(q==n-1)
{
if(s[q]==s[q-1])
length=length+1;

else
length=length-1;

}

else{
if(s[q]==s[q+1])
length++;

if(s[q]==s[q-1])
length++;

if(s[q]!=s[q+1])
length--;

if(s[q]!=s[q-1])
length--;

}
cout<<length<<endl;
}
}
}
return 0;
}
``````

Your code is giving only 0

Expected output:
0
0
0

(since K is 3)

You can also try this,

`````` #include <bits/stdc++.h>
#define    ll         long long
#define    ui         unsigned int
#define    mod        1000000007
#define    pb		  push_back
using namespace std;
void solve()
{
int n, k;	cin >> n >> k;
string s;	cin >> s;

int curr = 0;
for(int i=1; i<n; i++) {
curr += (s[i]==s[i-1] ? 2 : 1);
}
if(n == 1) {
for(int i=0; i<k; i++) {
int q;	cin >> q;
cout << 0 << endl;
}
return ;
}
for(int i=0; i<k; i++) {
int q;	cin >> q;
int x = q-1;
if(s[x] == '1') {
int prev = x-1, next = q;
if(prev >= 0)
curr += (s[prev] == '1' ? -1 : 1);
if(next < n)
curr += (s[next] == '1' ? -1 : 1);
}
else {
int prev = x-1, next = q;
if(prev >= 0)
curr += (s[prev] == '1' ? 1 : -1);
if(next < n)
curr += (s[next] == '1' ? 1 : -1);
}

s[x] = (s[x] == '1' ? '0' : '1');
// cout << s << " ";
cout << curr << endl;
}
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("error.txt", "w", stderr);
freopen("output.txt", "w", stdout);
#endif

int t = 1;
/*is Single Test case?*/ cin >> t;
while (t--) {
solve();
}

cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
return 0;
}``````

i tried for 0 0 0 and 1 1 1 both

``````#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
long long int k;
long long int n;
cin>>n>>k;

string s;
cin>>s;

long long int length;
for(long long int i=0;i<n-1;i++)
{
length=length+1+(s[i]==s[i+1]);
}

while(k--)
{
long long int q;
cin>>q;q--;

if(s[q]=='1')
s[q]='0';

else
s[q]='1';

if(n==1)
cout<<0<<endl;

else
{
if(q==0)
{
if(s[q]==s[q+1])
length=length+1;

else
length=length-1;
}

else if(q==n-1)
{
if(s[q]==s[q-1])
length=length+1;

else
length=length-1;

}

else{
if(s[q]==s[q+1])
length++;

if(s[q]==s[q-1])
length++;

if(s[q]!=s[q+1])
length--;

if(s[q]!=s[q-1])
length--;

}
cout<<length<<endl;
}
}
}
return 0;
}
``````

Hey, can we solve this problem using Bit Manipulation?

i finally found the error
i had to initialize length with zero

Reading compiler warnings would have exposed this straight away:

``````[simon@simon-laptop][08:44:15]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling abhinav_700-CHARGES.cpp
+ g++ -std=c++14 abhinav_700-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
abhinav_700-CHARGES.cpp: In function ‘int main()’:
abhinav_700-CHARGES.cpp:21:26: warning: ‘length’ may be used uninitialized in this function [-Wmaybe-uninitialized]
length=length+1+(s[i]==s[i+1]);
~~~~~~^~
+ set +x
Successful
``````

i was not getting those warnings in codechef ide(i forgot that codechef only shows errors and not the warnings)

1 Like