how to reduce time complexity of this one
Time complexity
Hello @Codarikh,
Your code is taking O(n^2) time complexity due to two nested four loops
You can implement the following optimal approach that would take O(nlogn) for sorting:
-
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 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.