Created
August 4, 2015 20:15
-
-
Save viveksyngh/a660869052d2af37c64f to your computer and use it in GitHub Desktop.
Rearrange a given array so that Arr[i] becomes Arr[Arr[i]] with O(1) extra space.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def arrange(self, A): | |
for i in range(len(A)) : | |
A[i] += (A[A[i]]%len(A)) * len(A) | |
for i in range(len(A)) : | |
A[i] = A[i]/len(A) | |
# Rearrange a given array so that Arr[i] becomes Arr[Arr[i]] with O(1) extra space. | |
# Input: arr[] = {3, 2, 0, 1} | |
# Output: arr[] = {1, 0, 3, 2} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment