What are the dimensions of the matrix?
I considered x as 15 and y as 8 as given in the figure but i’m getting wrong answer only.
Check my code : https://ide.codingblocks.com/s/107151
Grand Temple Greedy
Hello @Kishan-Mishra-2464674013761441,
You cannot hard code the size of the matrix as(15,8). This would be wrong.
The size can be identified using the coordinates given in the inputs:
The max x value is columns and max y value is the rows.
Isn’t it so inefficient to calculate the size of array this way and then declaring the array.
May be you don’t need to create any matrix.
This is wrong comparision:
dx = x[0];
dx = max(dx, x[i]-x[i-1]);
What if the minimum x-coordinate is 8 and the maximum difference in less than 8.
Solution:
1.
Use a vector instead as it is dynamic in nature and you need not to specify the size while declaration.
2.
dx=x[1]-x[0];
dx = max(dx, x[i+1]-x[i]);
Hope, this would help.
Give a like if you are satisfied.
What would be dx and dy for the last comparison when we don’t know the size from begining. Like i wrote in my code dx = max(dx, 15-x[n-1]). What will i write here
Once you would add elements in vector.
There is a function vector.length() associated with the vector to find it’s length.
Use that for iterating over vector and as you have sorted the elements. So, the last element would be the largest ordinate.
Hope, this would help.
I have made the changes, but now the answer that i am getting is wrong. I am getting answer as 6 whereas it should be 2 for the sample test case.
Here is the code:
This is because you have to subtract 1 from both dx and dy before multiplication.
Observe the figure for the same.
Modified Code:
Hope, this would help.
Give a like, if you are satisfied.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.