Ohh yess I tried to do it in one line but should have put in curly braces,
thanks for your help got AC!
But I did’nt get any compiler warnings as such
when I executed that…
I just removed the void solve() and placed all the code inside main .Now i am getting correct answer . Can you please tell me why is it so? what wrong in using another function ?
Your AC solution still has the out-of-bounds access:
[simon@simon-laptop][14:59:27]
[~/devel/hackerrank/otherpeoples]>./codechef-download-solution.rb https://www.codechef.com/viewsolution/47337805
Solution written to: aadarshsinha-CHARGES.cpp
[simon@simon-laptop][14:59:37]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling aadarshsinha-CHARGES.cpp
+ g++ -std=c++14 aadarshsinha-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][14:59:40]
[~/devel/hackerrank/otherpeoples]>echo "1
1 1
0
1" | ./a.out
aadarshsinha-CHARGES.cpp:38:42: runtime error: index 1 out of bounds for type 'char [*]'
aadarshsinha-CHARGES.cpp:39:47: runtime error: index 1 out of bounds for type 'char [*]'
0
so you got lucky.
But i am still confused , when i used void solve() function and write the code there it give me wrong answer and when i write the entire code inside main function it gives my correct answer .Why is it so ?
using void solve() : Solution: 47336440 | CodeChef
without viod solve() : Solution: 47341451 | CodeChef
There’s Undefined Behaviour for you 
okay , thank you so much for helping me out .
Out of bounds access on sample testcase:
[simon@simon-laptop][18:19:24]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling arjav_jain-CHARGES.cpp
+ g++ -std=c++14 arjav_jain-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][18:19:28]
[~/devel/hackerrank/otherpeoples]>echo "1
3 3
010
2 1 3
" | ./a.out
/usr/include/c++/7/bits/basic_string.h:1057: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]: Assertion '__pos <= size()' failed.
Aborted (core dumped)
Can someone please tell what is wrong with my code?
https://www.codechef.com/viewsolution/47349723
I am getting RE (SIGSEGV) error
Out-of-bounds access on the following test input:
1
4 8
0110
1 4 2 1 3 2 1 2
[simon@simon-laptop][20:32:25]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling ikshul130-CHARGES.cpp
+ g++ -std=c++14 ikshul130-CHARGES.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][20:32:29]
[~/devel/hackerrank/otherpeoples]>echo "1
4 8
0110
1 4 2 1 3 2 1 2
" | ./a.out
5
6
4
5
/usr/include/c++/7/bits/basic_string.h:1057: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]: Assertion '__pos <= size()' failed.
Aborted (core dumped)
Can you please suggest what changes do I need to make in my code? and also, how can I think of it on my own?
If you have watched the official video editorial, making changes in your code shouldnt be tough !! To answer your second question, just keep practicing variety of problems. Most of the problems are new to everyone, but the one having more practice can link it with a standard problem or past problem / idea.
what’s wrong with my code pls… help-
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
string s;
cin>>s;
int q[k];
for(int i=0;i<k;i++)
{
cin>>q[i];
}
long long int count=0;
if(n==1)
{
cout<<“0”<<"\n";
}
else{
for(int i=0;i<n-1;i++)
{
if(s[i]==s[i+1])
{
count+=2;
}
else
{
count++;
}
}
for(int i=0;i<k;i++)
{
s[q[i]-1]=s[q[i]-1]^1;
if((q[i]-1)<k-1 && (q[i]-1)>0)
{
if(s[q[i]-1]!=s[q[i]-2] && s[q[i]-1]!=s[q[i]])
{
count-=2;
}
if(s[q[i]-1]==s[q[i]-2] && s[q[i]-1]==s[q[i]])
{
count+=2;
}
}
else
{
if((q[i]-1)==0)
{
if(s[q[i]-1]==s[q[i]])
{
count++;
}
if(s[q[i]-1]!=s[q[i]])
{
count--;
}
}
if((q[i]-1)==k-1)
{
if(s[q[i]-1]==s[q[i]-2])
{
count++;
}
if(s[q[i]-1]!=s[q[i]-2])
{
count--;
}
}
}
cout<<count<<"\n";
}
}}
return 0;
}
thnx for trying and helping…!
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! ![]()
#include <bits/stdc++.h>
using namespace std;
define ll long long
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll int t;
cin>>t;
for(ll int i=0;i<t;i++)
{
ll int n,k;
cin>>n>>k;
string s;
cin>>s;
ll int a[k+1]={};
for(ll int x=1;x<=k;x++)
{
cin>>a[x];
}
ll int len=0;
for(ll int r=0;r<s.size()-1;r++)
{
// cout<<s[r]<<s[r+1]<<endl;
if(s[r]==s[r+1])
len+=2;
else
len+=1;
}
if(n==1)
{
cout<<“0”<<endl;
continue;
}
//cout<<len<<endl;
for(ll int j=1;j<=k;j++)
{
if(s[a[j]-1]==‘0’)
{
s[a[j]-1]=‘1’;
//cout<<“z”<<endl;
}
else
{
s[a[j]-1]=‘0’;
}
if(s[a[j]-1]==s[a[j]] && a[j]>1 && a[j]<k)
{
len++;
}
if(s[a[j]-1]!=s[a[j]] && a[j]>1 && a[j]<k)
{
len–;
}
if(s[a[j]-1]==s[a[j]-2] && a[j]>1 && a[j]<k)
{
len++;
}
if(s[a[j]-1]!=s[a[j]-2] && a[j]>1 && a[j]<k)
{
len–;
}
if(a[j]==1)
{
if(s[a[j]-1]==s[a[j]])
{
len++;
}
else
{
len–;
}
}
if(a[j]==k)
{
if(s[a[j]-2]==s[a[j]-1])
len++;
else
len–;
}
cout<<len<<endl;
}
}
return 0;
}
can someone tell me why my code is showing wrong answer @darshancool25
Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! ![]()