Can anyone please tell me why i am getting runtime error in this code?

Problem: INCREAST Problem - CodeChef
Submission Link: CodeChef: Practical coding for everyone

My test cases are running fine but don’t know why this code is giving runtime error while submission.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    //Start from here
    int t;
    cin>>t;
    while(t--){
    	string s;
    	cin>>s;
    	//find the min character in the string
    	char small='|';//taking the max char
    	int pos=-1;
    	//to capture the location of the min char aswell
    	for(int i=0;i<s.size();i++){
    		if(s[i]<=small){
    			small=s[i];
    			pos=i;
    		}
    	}

    	if(pos<=0){
    		cout<<s;
    	}else{
	    	//now take a vector and store big elements in it
	    	vector<char> bade;
	    	vector<char> chote;
	    	for(int i=0;i<=pos;i++){
	    		if(s[i]>small)bade.push_back(s[i]);
	    		else chote.push_back(s[i]);
	    	}

	    	//now talking about the left elements .... after pos + 1
	    	if(pos==s.size()-1){
	    		//means no element after it.... 
	    		//do nothing
	    	}
	    	else{
	    		if(s[pos+1]<bade[bade.size()-1]){
	    			//append the left elements at the end of chote
	    			for(int i=pos+1;i<s.size();i++){
	    				chote.push_back(s[i]);
	    			}

	    		}
	    		else {
	    			//append at the end of bade
	    			for(int i=pos+1;i<s.size();i++){
	    				bade.push_back(s[i]);
	    			}
	    		}
	    	}

	    	//print the chote and bade
	    	for(auto &x : chote)cout<<x;
	    	for(auto &x : bade)cout<<x;
    	}
    	cout<<endl;

    }

}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Here is the link of my submission : CodeChef: Practical coding for everyone

1 Like

Out-of-bounds access on the following test input:

1                         
aaaz
[simon@simon-laptop][06:52:03]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh                                       
Compiling codingtriangle-INCREAST.cpp
Executing command:
  g++ -std=c++17 codingtriangle-INCREAST.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG    -fsanitize=undefined -ftrapv
codingtriangle-INCREAST.cpp: In function ‘int main()’:
codingtriangle-INCREAST.cpp:22:19: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   22 |      for(int i=0;i<s.size();i++){
      |                  ~^~~~~~~~~
codingtriangle-INCREAST.cpp:41:13: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   41 |       if(pos==s.size()-1){
      |          ~~~^~~~~~~~~~~~
codingtriangle-INCREAST.cpp:48:26: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   48 |         for(int i=pos+1;i<s.size();i++){
      |                         ~^~~~~~~~~
codingtriangle-INCREAST.cpp:55:26: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   55 |         for(int i=pos+1;i<s.size();i++){
      |                         ~^~~~~~~~~
codingtriangle-INCREAST.cpp: In function ‘void call()’:
codingtriangle-INCREAST.cpp:6:12: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
    6 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
codingtriangle-INCREAST.cpp:7:12: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
    7 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Successful
[simon@simon-laptop][06:52:10]
[~/devel/hackerrank/otherpeoples]>echo "1                                                       
aaaz" | ./a.out
/usr/include/c++/9/debug/vector:427:
In function:
    std::__debug::vector<_Tp, _Allocator>::reference 
    std::__debug::vector<_Tp, 
    _Allocator>::operator[](std::__debug::vector<_Tp, 
    _Allocator>::size_type) [with _Tp = char; _Allocator = 
    std::allocator<char>; std::__debug::vector<_Tp, _Allocator>::reference = 
    char&; std::__debug::vector<_Tp, _Allocator>::size_type = long unsigned 
    int]

Error: attempt to subscript container with out-of-bounds index -1, but 
container only holds 0 elements.

Objects involved in the operation:
    sequence "this" @ 0x0x7ffe706a1a70 {
      type = std::__debug::vector<char, std::allocator<char> >;
    }
Aborted (core dumped)

Thanks … i got it…

1 Like