Using an Array as a built in index for LeetCode problems

Algorithms in Swift

Image for post
Image for post
An example array with index places shown

Before We Start

Why?

Prerequisites

Keywords and Terminology

Setting elements to their correct position

The sort solution:

A real in-place solution:

Image for post
Image for post
The target transformation for an in place sort using the natural index in an array
Image for post
Image for post
Finding the two “out of place” elements using the formula array[index] — 1 == index
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Conclusion

The GitHub link:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store