How to write optimized code. I have written a brute force by checking i and (n-i-1). But how can i optimize that?
Check if array if spirally sorted
An example would be
4
1 3 4 2
Actually this problem came in a hiring challenge. I solved it partially. I dont have the link.
Can you share something about the problem then? I cant check the code unless I know what the problem is
So the problm goes like this…
Given an array arr of size n. We have to check if the array is spirally sorted.
like 1 3 4 2
SIf its spirally sorted then it goes like 1 2 3 4. If u see the the above array the sorted pattern is kind of spiral design.
n was upto 10^5
arr[i] is upto 10^5
inputs
n - size of array
followed by n elements of arr. ( 0 based indexing)
Output
print yes if its spirally sorted or else print no
example
4
1 3 4 2
yes
I dont remember the constraints for n and arr[i] exactly but it was somewhat close to what i have mentioned.
I dont think you can optimize it further than o(n) as you will have to iterate over each element at least once. Please mark your doubt as resolved if you are satisifed.
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.
SORRY, i forgot. Its done now