Very Simple Code: Link
Can you pleaseš tell me which test case am i missing . I have tried all including n=1 ,but still WA
link : CodeChef: Practical coding for everyone
Already told you
Fix the out-of-bounds access.
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ā¦!