Given an array arr[] of size N where every element is in the range from 0 to n-1. Rearrange the given array so that the transformed array arr^{T}[i] becomes arr[arr[i]].
NOTE: arr and arr^{T} are both same variables, representing the array before and after transformation respectively.
Example 1:
Input:
N = 2
arr[] = {1,0}
Output: 0 1
Explanation: 
arr[arr[0]] = arr[1] = 0
arr[arr[1]] = arr[0] = 1
So, arr^{T} becomes {0, 1}
Example 2:
Input:
N = 5
arr[] = {4,0,2,1,3}
Output: 3 4 2 0 1
Explanation: 
arr[arr[0]] = arr[4] = 3
arr[arr[1]] = arr[0] = 4
arr[arr[2]] = arr[2] = 2
arr[arr[3]] = arr[1] = 0
arr[arr[4]] = arr[3] = 1
and so on
So, arrT becomes {3, 4, 2, 0, 1}
Your Task:
You don't need to read input or print anything. The task is to complete the function arrange() which takes arr and N as input parameters and rearranges the elements in the array in-place. 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= Arr[i] < N

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Data structures

Mathematics

Starter code: #User function Template for python3

##Complete this code

class Solution:
    #Function to rearrange an array so that arr[i] becomes arr[arr[i]]
    #with O(1) extra space.
    def arrange(self,arr, n): 
        #Your code here

Solutions:
class Solution:

	def arrange(self, arr, n):
		for i in range(0, n):
			arr[i] = arr[i] + n * (arr[arr[i]] % n)
		for i in range(0, n):
			arr[i] = arr[i] // n


class Solution:

	def arrange(self, arr, n):
		for i in range(n):
			arr[i] += arr[arr[i]] % n * n
		for i in range(n):
			arr[i] = int(arr[i] / n)


class Solution:

	def arrange(self, arr, n):
		p = [-1] * n
		for i in range(n):
			p[i] = arr[arr[i]]
		for i in range(n):
			arr[i] = p[i]


class Solution:

	def arrange(self, arr, n):
		for i in range(n):
			arr[i] = arr[i] + n * (arr[arr[i]] % n)
		for i in range(n):
			arr[i] = arr[i] // n
		return arr


class Solution:

	def arrange(self, arr, n):
		res = []
		for i in range(n):
			res.append(arr[arr[i]])
		for i in range(n):
			arr[i] = res[i]


class Solution:

	def arrange(self, array, n):
		for i in range(n):
			x = array[i]
			y = array[x]
			array[i] = x + y % n * n
		for i in range(n):
			array[i] //= n
		return array


class Solution:

	def arrange(self, arr, n):
		for i in range(n):
			if n == 1:
				arr[i] = arr[i] + arr[arr[i]] * n
			else:
				arr[i] = arr[i] + arr[arr[i]] % n * n
		for i in range(n):
			arr[i] = int(arr[i] / n)


class Solution:

	def arrange(self, arr, n):
		arr1 = arr[:]
		for i in range(n):
			arr[i] = arr1[arr1[i]]


class Solution:

	def arrange(self, arr, n):
		l = []
		for i in range(n):
			a = arr[arr[i]]
			l.append(a)
		for i in range(n):
			arr[i] = l[i]


class Solution:

	def arrange(self, arr, n):
		for i in range(n):
			old_value = arr[i] % n
			arr[i] = n * (arr[arr[i]] % n) + old_value
		for i in range(n):
			arr[i] = arr[i] // n


class Solution:

	def arrange(self, arr, n):
		i = 0
		for num in arr:
			arr[i] += arr[num] % n * n
			i += 1
		i = 0
		while i < n:
			arr[i] //= n
			i += 1
		return arr


class Solution:

	def arrange(self, arr, n):
		arr[:] = [arr[elem] for elem in arr]


class Solution:

	def arrange(self, arr, n):
		temp = []
		for i in range(n):
			x = arr[arr[i]]
			temp.append(x)
		for i in range(n):
			arr[i] = temp[i]


class Solution:

	def arrange(self, arr, n):
		x = list(arr)
		i = 0
		while i < n:
			arr[i] = x[x[i]]
			i += 1


class Solution:
	import math

	def arrange(self, arr, n):
		for i in range(0, n):
			arr[i] = arr[i] + arr[arr[i]] % n * n
		for i in range(n):
			arr[i] = math.floor(arr[i] / n)
		return arr


class Solution:

	def arrange(self, arr, n):
		for i in range(n):
			if arr[i] < n:
				arr[i] += arr[arr[i]] % n * n
		for i in range(n):
			arr[i] = arr[i] // n


class Solution:

	def arrange(self, arr, n):
		for i in range(n):
			x = arr[i]
			y = arr[x]
			arr[i] = x + y % n * n
		for i in range(n):
			arr[i] = arr[i] // n


class Solution:

	def arrange(self, arr, n):
		l = []
		for i in range(0, n):
			m = arr[i]
			l.append(arr[m])
		for i in range(0, n):
			arr[i] = l[i]


class Solution:

	def arrange(self, arr, n):
		a = []
		for i in range(n):
			a.append(arr[arr[i]])
		arr.clear()
		for j in range(n):
			arr.append(a[j])
		return arr


class Solution:

	def arrange(self, arr, n):
		for i in range(n):
			a = arr[i]
			b = arr[a]
			arr[i] = a + b % n * n
		for i in range(n):
			arr[i] = int(arr[i] // n)
		return arr


class Solution:

	def arrange(self, arr, n):
		for i in range(len(arr)):
			arr.append(arr[arr[i]])
		del arr[0:n]


class Solution:

	def arrange(self, arr, n):
		a = []
		for i in arr:
			a.append(arr[i])
		arr[0:n] = a


class Solution:

	def arrange(self, arr, n):
		for i in range(n):
			if arr[i] < i:
				orig_value = arr[arr[i]] % n
				arr[i] = arr[i] + orig_value * n
			else:
				arr[i] = arr[i] + arr[arr[i]] * n
		for i in range(n):
			arr[i] = arr[i] // n
		return arr


class Solution:

	def arrange(self, arr, n):
		m = [i for i in arr]
		for i in range(len(arr)):
			arr[i] = m[m[i]]


class Solution:

	def arrange(self, arr, n):
		lst = []
		for i in range(n):
			k = arr[arr[i]]
			lst.append(k)
		for i in range(n):
			arr[i] = lst[i]
		return arr


class Solution:

	def arrange(self, arr, n):
		lis = []
		for i in range(0, n):
			lis.append(arr[arr[i]])
		arr[:] = lis[:]
		return lis


class Solution:

	def arrange(self, arr, n):
		lst = []
		j = 0
		for i in range(n):
			k = arr[i]
			lst.append(arr[k])
		for i in lst:
			arr[j] = i
			j = j + 1


class Solution:

	def arrange(self, arr, n):
		b = arr.copy()
		for i in range(0, n):
			temp = b[i]
			arr[i] = b[temp]
		return arr


class Solution:

	def arrange(self, arr, n):
		M = 1000000007
		for i in range(n):
			temp = arr[arr[i]] % M
			arr[i] = temp * M + arr[i] % M
		for i in range(n):
			arr[i] = arr[i] // M
		return arr


class Solution:

	def arrange(self, l, n):
		for i in range(0, n):
			l[i] = l[i] + l[l[i]] % n * n
		for i in range(0, n):
			l[i] = int(l[i] / n)
		return l


class Solution:

	def arrange(self, a, n):
		for i in range(n):
			a[i] += a[a[i]] % n * n
		for i in range(n):
			a[i] = int(a[i] / n)


class Solution:

	def arrange(self, arr, n):
		x = []
		for i in range(n):
			v = arr[i]
			c = arr[v]
			x.append(c)
		arr.clear()
		arr[:] = x


class Solution:

	def arrange(self, a, n):
		mx = max(a)
		mx += 1
		for i in range(n):
			a[i] += a[a[i]] % mx * mx
		for i in range(n):
			a[i] //= mx
		return a


class Solution:

	def arrange(self, arr, n):
		mx = 0
		for i in range(n):
			mx = max(mx, arr[i])
		mx = mx + 1
		for i in range(n):
			orig_elem = arr[i] % mx
			multiplier = arr[orig_elem] % mx
			arr[i] = multiplier * mx + arr[i]
		for i in range(n):
			arr[i] = arr[i] // mx