Following is the code to a problem I have been doing over course of 1 week , I came close, 1subtask(With huge input) is passing , but other testcases are showing Runtime error(SIGSEGV)(Segementation fault)

The question problem: Contest Page | CodeChef

Basic IDEA:

- keep track of two of the longest streaks of ones and after each rotation query, change the pointer of the starting index(rotation) and check how this cuts both the streaks(making possibly 4 streaks) and out of which find the max and print.

Problem:

Segmentation error..

The source code:

Code:#include <iostream> #include <cmath> #include <algorithm> #include <limits> #include <vector> #include <bitset> #include <cstdlib> #include <cstdio> #include <cstring> #include <time.h> #include <list> #include <set> #include <map> #include <queue> #include <cctype> #include <list> #include <iterator> #include <iosfwd> using std::cin; using std::cout; using std::vector; using std::string; using std::map; const int MAX=1e5+5; int start_ind_count_ones[MAX]; bool compa(int a,int b) { if(a>b) return true; return false; } int main() { long long n,q,k;cin>>n>>q>>k; vector<int> arr(n); for(int i=0;i<n;i++) {cin>>arr[i];} string query_string;cin>>query_string; int highest_1_start_ind=0,highest_1_end_ind=0,highest_2_start_ind=0,highest_2_end_ind=0; vector<int> count_of_ones; for(int i=0;i<n;) { int c=0;int starting_ind=0;int f=0; while(arr[i]==1) { if(f==0) {starting_ind=i;f=1;} c++;i++; } if(starting_ind<n) {start_ind_count_ones[starting_ind]=c; count_of_ones.push_back(c);} i++; } int last_count_of_ones; if(arr[arr.size()-1]==1 && arr[0]==1) { count_of_ones.push_back(count_of_ones[0]+count_of_ones[count_of_ones.size()-1]); last_count_of_ones=count_of_ones[count_of_ones.size()-1]; for(int i=n-1;i>=0;i--) { if(start_ind_count_ones[i]!=0) {start_ind_count_ones[i]=count_of_ones[count_of_ones.size()-1];break;} } } sort(count_of_ones.begin(),count_of_ones.end(),compa); for(int i=0;i<n;i++) { if(start_ind_count_ones[i]==count_of_ones[0]) { highest_1_start_ind=i; highest_1_end_ind=(start_ind_count_ones[i]+i-1)%n; } else if( count_of_ones.size()>=1 && start_ind_count_ones[i]==count_of_ones[1]) { highest_2_start_ind=i; highest_2_end_ind=(start_ind_count_ones[i]+i-1)%n; } } int pointer_for_rotation=0; for(int i=0;i<q;i++) { if(query_string[i]=='!') { pointer_for_rotation--; if(pointer_for_rotation<0) pointer_for_rotation=n-1; } else { int max1a=count_of_ones[0],max1b=0,max2a=count_of_ones[1],max2b=0; if(pointer_for_rotation>=highest_1_start_ind && pointer_for_rotation<=highest_1_end_ind) { max1a=highest_1_end_ind-pointer_for_rotation+1; max1b=count_of_ones[0]-max1a; } if(pointer_for_rotation>=highest_2_start_ind && pointer_for_rotation<=highest_2_end_ind) { max2a=highest_2_end_ind-pointer_for_rotation+1; if(count_of_ones.size()>=1) max2b=count_of_ones[1]-max2a; } if(highest_1_start_ind>highest_1_end_ind && pointer_for_rotation<=highest_1_end_ind) { max1a=highest_1_end_ind-pointer_for_rotation+1; max1b=count_of_ones[0]-max1a; } if(highest_2_start_ind>highest_2_end_ind && pointer_for_rotation<=highest_2_end_ind) { max2a=highest_2_end_ind-pointer_for_rotation+1; if(count_of_ones.size()>=1) max2b=count_of_ones[1]-max2a; } int max1=std::max(max1a,max1b); int max2=std::max(max2a,max2b); int final_max=std::max(max1,max2); if(final_max>k) final_max=k; cout<<final_max<<"\n"; } } return 0; }