SIGABRT:out of range when using stoi/stol - c++

//I'm trying to solve this problem from HackerEarth: Binary Queries.
Though initially, the problem sounds easy to me but when actually submitted my code to run on all the test cases, my code is throwing SIGABRT error.
Upon checking the error, I found the error to be of type out of range. I'm unable to figure out how to resolve this:
problem: https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/range-query-2/
#include<iostream>
#include<algorithm>
#include<cctype>
using namespace std;
int main()
{
long N,Q,L,R,X;
int ch=0,buf;
unsigned long long intrim;
string str,cstr;
scanf("%ld %ld",&N,&Q);
cin.ignore();
getline(cin,str);
str.erase((remove_if(str.begin(),str.end(),(int(*)(int))isspace)),str.end()); // here its a key point
//cout<<str;
for(int i=0;i<Q;i++)
{
scanf("%d",&ch);
if(ch==1) // alter the X bit
{
scanf("%ld",&X);
if(str[X-1]==0){
str[X-1]=1;
}
else {
str[X-1]=0;
}
}
else if(ch==0)
{
scanf("%ld %ld",&L,&R);
cstr.append((str.begin()+L-1),str.begin()+R);
intrim=std::stoull(cstr,nullptr,2);
if(intrim%2==0){
cout<<"EVEN"<<endl;
}
else{
cout<<"ODD"<<endl;
}
cstr.clear();
}
}
}

stoi return an integer number
stol return a long int number
stoll return a long long int number
So, when you are converting a string to a number, thought about the possible range and use accordingly.

Related

Code gives different results for similar inputs

I was working on a problem to find the longest substring without repeating characters. While trying for the solution I wrote this code. I'm not asking for the solution, but just the reason why the code works the way it does.
#include<iostream>
#include<map>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char,int> mp;
map<char,int>::iterator it;
int i=0,k=0,max=0;
while(i<s.length()){
it = mp.find(s[i]);
if(it==mp.end()){
k++;
}
else{
k=k-distance(mp.begin(),it);
cout<<distance(mp.begin(),it)<<" "; //this distance
mp.erase(mp.begin(),it);
}
if(k>max){
max=k;
}
mp.insert({s[i],1});
i++;
}
return max;
}
};
int main(){
Solution obj;
cout<<obj.lengthOfLongestSubstring("yvyf");
}
When I give the input as "yvyf" it prints the distance as 1, but when I give the input as "avaf" or "pvpf", it prints the distance as 0.

CodeBlocks C++ Exception

I'm having problem with this code:
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
long long addV(int i) {
return pow(10,i);
}
int len;
void recurse(int n,long long &ways,int values[],int current=0,int p=0) {
if(p>len) return;
if(current>n) return;
if(current ==n) {
ways++;
return;
}
int cv = n-current;
cv/=values[p];
for(int i=0;i<=cv;i++) {
recurse(n,ways,values,current+values[p]*i,p+1);
}
}
int main() {
int n;
cin>>n;
long long ways=0;
int values[] ={1,2,3};
len = sizeof(values)/sizeof(int);
recurse(n,ways,values);
cout<<ways;
}
The exception comes from (cv/=values[p];) line. Of course the shitty CodeBlocks never shows what the exception is.
I'm sure its something easy to fix.
if(p>len)return;
Indeed you've already accessed over boundary when p == len. You need to return once p >= len.
Because among your ending condition for the recursion is p > len which means that p will be in the range from zero to three (inclusive). And as you know, an array of three entries have the indexes range from zero to two.

Project Euler #14 code output not coming

I used the following code for solving problem#14 but for some strange reason it gives no output.Maybe its taking too long to run???
P.S.I know that max is not supposed to be the answer but still there is no output anyways whereas for smaller values like i<100 I get the output.
#include <iostream>
long collatz(long);
int main()
{
using namespace std;
long i=3,max;
for(i=3;i<1000000;i++)
{
max=collatz(i-1);
if(collatz(i)>collatz(i-1))
{
max=collatz(i);
}
else
{
max=collatz(i-1);
}
}
cout<<max<<endl;
cin.clear();
cin.get();
}
long collatz(long n)
{
int count=0;
while(n!=1)
{
if(n%2==0)
{
n=n/2;
count+=1;
}
else
{
n=3*n+1;
}
}
return count;
}
If you call collatz with n = 113383, you get overflow and n becomes negative from which it never recovers. So you have an infinite loop as it will never be 1. You need to use a long long inside collatz.
However, your collatz functions has other problems as pointed out by others. Also, your logic for the loop in main is not correct. You are resetting max each time through the loop. So, the result you report would be either collatz(999999) or collatz(999998). But that is not be the correct answer.

why std::map throws an exception?

I run this code using both gpp and microsoft compiler but in both case I'v got an exception
but I can't understand why!
this is my code:
#include <iostream>
#include <map>
using namespace std;
map<int,int> fib;
int fibo(int i)
{
if (!fib.count(i))
{
fib.insert(pair<int, int>(i,fibo(i-1)+fibo(i-2)));
}
return fib[i];
}
int r(int i)
{
if(i<3)
{
return i;
}
else
{
return fibo(i)+r(i-2);
}
}
int main()
{
fib.insert(pair<int, int>(0,1));
fib.insert(pair<int, int>(1,1));
int a,b,n;
cin>>a>>b;
n=b-a;
int fiba=fibo(a);
int fibaa=fibo(a-1);
cout << (r(n+1)*fiba)+(r(n)*fibaa);
return 0;
}
can anyone help me?
I debugged this code and I found that fib.insert(pair<int, int>(i,fibo(i-1)+fibo(i-2))); doesn't work.
I've got Stack Overflow exception when I ran your code, obviously because of too deep recursion.
You should either increase stack size (but you can later choose to input a larger number and get the same exception again), or convert this algorithm into a non-recursive one (for example see this one: http://www.codeproject.com/Tips/109443/Fibonacci-Recursive-and-Non-Recursive-C)
Did you try to input some negative numbers? You don't do any check on the inputs you pass to your program, and in
return fib[i];
you will receive an error if you try to access non-existent locations.

SIGSEGV on Submission

i was solving the problem
https://www.spoj.pl/problems/ACPC11A/
and here is my code :
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
int main()
{
int tc,i,n;
scanf("%d",&tc);
while(tc--)
{
vector<string> v1,v2;
string str,w;
scanf("%d",&n);
int flag=0;
for(i=0;i<n;i++)
{
cin>>str;
if(str[0]!='#')
{
flag=1;
w=str;
}
else if(flag==0)
{
v1.push_back(str);
}
else
v2.push_back(str);
}
//print v2-->w-->v1
for(i=0;i<v2.size();i++)
{
cout<<v2[i]<<" ";
}
if(w!="")
cout<<w<<" ";
for(i=0;i<v1.size()-1;i++)
cout<<v1[i]<<" ";
cout<<v1[v1.size()-1]<<endl;
v1.clear();v2.clear();str.clear();w.clear();
}
return 0;
}
i am getting the correct output for the sample test case...but on submission my code gives segmentation fault.
my logic is simple..
i took 2 vectors 1 for storing words before a English word arrives(v1) and other for storing worlds after a English word arrives(v2)
after that i print the contents of v2 followed by word and then content of v1.
please help me in understanding why is this code giving segmentation fault.
don't bother guys...i got my mistake
Error is in line for(i=0;i<v1.size()-1;i++)
when v1.size() is 0 ,then as size() returns unsigned value...hence 0-1 will be very large value and hence the SIGSEGV