What will be the better solution to this problem, as my code is showing runtime error?
My code is:
CPP : Grand Temple
Hello @Riankk,
You can implement the following approach:
-
Make two vectors:
1.1. first: to store the x coordinates provided as input.
1.2. second: to store all the y coordinates provided as input. -
Sort both the vectors.
-
Find the maximum difference between the adjacent elements of each vector as dx and dy.
-
Print the Area by multiplying both the differences.
Note:
Observe the figure carefully, you have to subtract 1 from both dx and dy before multiplication.
Hope, this would help.
Give a like, if you are satisfied.
I was able to solve the question. But my question is how did the use of vectors instead of arrays decrease the runtime ?
Hey @Riankk,
-
I suggested the optimal approach as asked by you.
-
Because vectors allocate memory dynamically.
And the reason for the run-time error is the size of your array.
You are not satisfying the constraints specified in the question.
Runtime error can occur due to the following reasons:
- Invalid memory access during run-time.
- Accessing linked list nodes that do not exist.
- Dividing by zero.
- Large allocation of memory together/Large Static Memory Allocation.
- Making a silly mistake.
I have modified your code:
I hope, this would help.
Give a like if you are satisfied.