We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You most likely check for the previous value in the array to check if it's bigger than the sorted value before inserting it where you are. Test case #2 however goes all the way to ar[0], so when you check for ar[0-1], it crashes. I solved it by checking if i==0 || ar[i-1] for insertion.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Insertion Sort - Part 1
You are viewing a single comment's thread. Return to all comments →
You most likely check for the previous value in the array to check if it's bigger than the sorted value before inserting it where you are. Test case #2 however goes all the way to ar[0], so when you check for ar[0-1], it crashes. I solved it by checking if i==0 || ar[i-1] for insertion.