Segmentation fault in solving dynamic problem [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Is there any difference in these type of calculating ans in dynamic problems ? Because in case of Code 3 I am getting segmentation fault while Code 1 and Code 2 are passing
int dp[8005][2];//dp is initialized as -1 in main
int a=4;
int b=6;
int tar=90;
int solve(int pos,int ba)
{
if(pos<0||pos>8000||ba>=2)
return 1e6+1;
if(pos==tar)
return 0;
//Code 1
int &ans=dp[pos][ba];
if(ans!=-1)
return dp[pos][ba];
ans=1+solve(pos+a,0);
ans=min(ans,1+solve(pos-b,ba+1));
return ans;
//Code 2
if(dp[pos][ba]!=-1)
return dp[pos][ba];
dp[pos][ba]=1+solve(pos+a,0);
dp[pos][ba]=min(dp[pos][ba],1+solve(pos-b,ba+1));
return dp[pos][ba];
//Code 3
if(dp[pos][ba]!=-1)
return dp[pos][ba];
int ans=1+solve(pos+a,0);
ans=min(ans,1+solve(pos-b,ba+1));
return dp[pos][ba]=ans;
}

Code 3 doesn't update dp until after all the calculations are done.
The first two go
Recurse
Update dp
Recurse
Update dp
Return new value
Note that 1 and 2 are equivalent, since ans is the same as dp[pos][ba].
The third is
Recurse
Recurse
Update dp and return the new value

Related

Whats Wrong in it? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Problem: https://www.hackerrank.com/challenges/migratory-birds/problem
Test Case Link: https://hr-testcases-us-east-1.s3.amazonaws.com/33294/input04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1573162301&Signature=MgpSHa3lxX%2FwYwumjzAmF8uviZE%3D&response-content-type=text%2Fplain
for this test case i cant get any output why ?
Thanks in advance
#include<iostream>
using namespace std;
int main()
{
long long n,i=0,num=0,mx=0,r=0;
cin>>n;
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
for(int i=1;i<6;i++)
{
if(arr[i]>mx)
{
mx=arr[i];
r=i;
}
}
cout<<r;
}
This looks problematic:
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
If the input value read into num is greater than 5 (or less than zero), undefined behavior will exist when a value is written into an invalid memory location offset from arr.
There is nothing wrong with you code i got accepted all test cases. But the solution can be return in more optimized way

Unordered_set not working on codechef ide [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
"unordered_set" is not working properly on codechef and giving wrong output on its online ide whereas I am getting right output on geeksforgeeks ide and codeblocks
for Input like 3 2 10 1 100 4 3 i am getting 4 rows as expected in codeblocks and geeksforgeeks because n+m-1 is 4 whereas I am getting only 2 rows in codechef what can be the reason behind and now how it will work on codechef?
#include<stdio.h>
#include<bits/stdc++.h>
#include<unordered_set>
using namespace std;
int main()
{
int n,m,c=0,d,i,j,sum;
int a[10000];
int b[10000];
unordered_set <int> s;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
sum=a[i]+b[j];
if(s.find(sum)==s.end())
{
s.insert(sum);
printf("%d %d\n",i,j);
c++;
}
if(c>=(n+m-1))
{d=1;break;}
}
if(d==1)
break;
}
}
Your program exhibits undefined behavior, by way of accessing the value of an uninitialized variable d.

What is the difference between the two codes in C++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I was trying the problem Hash Tables: Ice Cream Parlor on Hackerrank. It is a simple question, but here is the bizzare situation i got to. How does the change of data structure matter?
Case 1:
void whatFlavors(vector<int> cost, int money) {
int ans1,ans2;
vector<int> arr(100,0); //notice this
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
And output is:
Case 2:
code:
void whatFlavors(vector<int> cost, int money) {
int arr[100]={0}; //notice this
int ans1,ans2;
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
output:
Let's just notice this part of your code:
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
This means that your expect money-cost[i] to be negative for some values of i. So you end up reading locations that are outside your array (vector or array) which will lead to undefined behavior in both cases.

How to count negatives? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I wrote this code and the output is not coming as expected. The function returns the negatives recursively. But it is not doing returning what it should be.
size_t recursive_x(stack<int> mystack){
int count=0;
if(!mystack.empty()){
if(mystack.top()<0){
count+=1;
}
mystack.pop();
count+=recursive_x(mystack);
}cout<<count<<endl;
return count;
}
int main(){
stack<int> mystack;
mystack.push(9);
mystack.push(-2);
mystack.push(-2);
mystack.push(1);
mystack.push(-3);
mystack.push(-1);
mystack.push(99);
mystack.push(-1);
mystack.push(1);
mystack.push(1);
recursive_x(mystack);
}
The output should be 5 but it is coming like this:
Output:
0
0
1
2
2
3
4
4
5
5
5
Assuming you do call the function, the output is exactly what I would expect.
The display shows the number of negative numbers on the stack at the point where
cout<<count<<endl;
is invoked.
It is invoked first for the innermost recursion, because you make your recursive call before doing any output for the current recursion.
count+=recursive_x(mystack);
}cout<<count<<endl;
If you just want the output 5, you could print the result returned from your initial call
int finalCount = recursive_x(mystack);
cout<<finalCount<<endl;
and remove the output in recursive_x().
Each recursive call will print the result.
You just need to move the print command to the main:
size_t recursive_x(stack<int> mystack){
int count=0;
if(!mystack.empty()){
if(mystack.top()<0){
count+=1;
}
mystack.pop();
count+=recursive_x(mystack);
}
return count;
}
int main(){
stack<int> mystack;
mystack.push(9);
mystack.push(-2);
mystack.push(-2);
mystack.push(1);
mystack.push(-3);
mystack.push(-1);
mystack.push(99);
mystack.push(-1);
mystack.push(1);
mystack.push(1);
cout<<recursive_x(mystack)<<endl;
}
Output:
5

Is if statement over complicated? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is the part that brakes, zod1 zod2 zodA are vectors. In vectors I store int. Cant tell why it crashes cause it gives no error
if(zod1[u]+zod2[y]==zodA[t] || (zod1[u]+zod2[y])%10==zodA[t])
Questions:
1 Is if statement over complicated
2 Can I use vectors like that for solving math in if statement (sorry can't explain better)
3 If 2 question is true how should I solve it
Explanations:
variable names are in my own language (sorry if it looks weird)
Values are zero for all (ex zod1[u]=0)
added whole function (variables going to the function are passed correctly and I know I use some unnecessary thing)
void calc(vector<char> zodis1, vector<char> zodis2, vector<char> zodisAts,int zo1,int zo2,int zoA)
{int i,keliamas=0;
int k =0;
vector<int> zod1(0);
vector<int> zod2(0);
vector<int> zodA(0);
for(i=0;i<zodis1.size();i++)
{
zod1.push_back(0);
}
for(i=0;i<zodis2.size();i++)
{
zod2.push_back(0);
}
for(i=0;i<zodisAts.size();i++)
{
zod2.push_back(0);
}
int u=zodis1.size()-1;
int y=zodis2.size()-1;
int t=zodisAts.size()-1;
if(zod1[u]+zod2[y]==zodA[t] || (zod1[u]+zod2[y])%10==zodA[t])
{//if((zod1[u]+zod2[y])/10==1)
{
keliamas=1;
}
if(u==0||y==0||t!=0)
{
if(keliamas==1)
{
}
}
u--;
y--;
t--;
}
else
{if(zod1[u]!=9)
zod1[u]=zod1[u]+1;
else
{ if(u!=zodis1.size()-1)
u++;
else
{
cout<<"something wrong man";
}
}
}
}
EDIT:
Error is here:
for(i=0;i<zodisAts.size();i++)
{
zod2.push_back(0); // should be zodA
}
Is if statement over complicated
No, I think it is very simple with just one 'or'. However, if zodA[t] is always less than 10, then your if condition can be written as:
if ( (zod1[u]+zod2[y])%10==zodA[t] )
Can I use vectors like that for solving math in if statement (sorry
can't explain better)
Yes you can.
If 2 question is true how should I solve it
If it compiles but then crashes, then probably you are accessing out of bounds indices. Check that your indices are less than the vector sizes.