let see how much deep u know about segmentation fault in c++

Question link - link text

solution link -link text

I want to know why i am getting segmentation fault on answer where my code is working for smaller input.
It work on on dijkistra algo and code help is from geeksforgeeks code link https://www.geeksforgeeks.org/printing-paths-dijkstras-shortest-path-algorithm/

for more test cases refer to hackerrank https://www.hackerrank.com/contests/w38/challenges/a-time-saving-affair/submissions/code/1308366537

And this question form hackerrank week of code 38


to understand the code quickly u can visit geeksforgeeks link given

Well all those who can suggest possible reason for segmentation fault are most welcome , i think it will help others to know about segmentation fault or runtime error which is hard to debug. And for any assistance just comment here i will serve it.

Hi @gyanendra371,

I have check your code and the ONE MAJOR BUG in your code is that you have created the graph using adjacency matrix and not adjacency list.

The given constraint for number of nodes is 1ā‰¤Nā‰¤104 and in most of the programming languages you can allocate at most 106 to 107 memory space. But, in your code you have created the graph as long graph[n][n] , which would try to allocated 108 memory locations and hence would fail.

So, in my opinion you should learn to build graph using adjacency list.

You can check my accepted solution here.

You can ping me in case of any doubts.

Hope this helps!! Happy Coding!! :slight_smile:


hey can u provide reference for dijkistra algo using adjancey list which can store the shortest path @brijwasi1995 and would u mind adding little bit of comment in ur code that can help lot.
And thanks for giving ur precios time .

Hi, you can learn about adjacency list and shortest path algorithm implementation from the following links.

  1. Adjacency List - https://www.hackerearth.com/practice/algorithms/graphs/graph-representation/tutorial/

  2. Shortest path algorithm implementation -

1 Like