Help me to fix the run time error (SIGSEGV) in my code

I am trying to solve “Walk or Train Problem”, but getting run time error (SIGSEGV) every time, i tried to find what causes the issue in code but can’t find one. Please help me to find the bug. I might be using the old way to do stuff in c++ as i learn it few years ago, i’m also new to competitive programming but not to coding in general. So i’ll appreciate some advice for a beginner :slight_smile:

Problem Link: https://www.codechef.com/problems/WALKFAST

My Solution:

#include <iostream>
using namespace std;

int main() {
	int T, N, A, B, C, D, P, Q, Y, walk_time=0, train_time=0;
	cin >> T;
	for (int i = 0; i < T; i++) {
		cin >> N >> A >> B >> C >> D >> P >> Q >> Y;
		int* X = new int[N];
		for (int i = 0; i < N; i++)
			cin >> X[i];
		walk_time = abs(X[A - 1] - X[B - 1]) * P;
		if (abs(X[A - 1] - X[C - 1]) * P <= Y){
			train_time = Y + (abs(X[C - 1] - X[D - 1]) * Q) + (abs(X[D - 1] - X[B - 1]) * P);
			walk_time = (walk_time < train_time) ? walk_time : train_time;
		}
		cout << walk_time;
	}
	return 0;
}

Error:

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

Edit:

If you’re talking about this, then it’s a WA, not a RE. Consider the testcase:

2
4 1 3 2 4 3 2 4
1 2 3 4
4 1 3 2 4 3 2 4
1 2 3 4

You’d probably get RE if you tried to Run without providing Custom Input, though :slight_smile:

2 Likes

May be the values for A,B,C or D larger than N .So, there may be chance that u are trying to access the illegel memory.Try to check the constraints.

1 Like

thanks for letting me know about code formatting @ssjgz, i tried your testcase on my local PC IDE and output is 6 both times, it is a sample testcase from the problem and i already tested it many time on my IDE, but the problem is whenever i run my code on codechef IDE, it throws “Runtime Error” showing “SIGSEGV” in the output. And about WA, i just submit my solution anyways and codechef count it as wrong answer. I just edit my post and attached Screenshot.

Do i need to explicity apply all constraints in the solution since inputs are taken by user? I see others correct submissions but no one apply constraint in the solution and fun fact is that i tried to run many correct submissions on my codechef IDE, but it still throws same runtime error on those submissions too.

No - see e.g. here.

Please re-read the last line of my previous post (and follow the link) :slight_smile:

1 Like