## 9811633187a

BAN USER- 1of 1 vote

AnswersGiven-an-array-of-length-n-having-integers-0-to-n-1-in-unsorted-order-we-have-to-modify-this-array-such-that-the-value-at-a-n-becomes-a-a-n-for-example-if-a-0-contains-5-then-a-0-will-have-value-a-5-and-so-on-condition-is-that-this-should-take-O-n-time-complexity

- 9811633187a in India for hr| Report Duplicate | Flag | PURGE

Adobe Field Sales Algorithm - -1of 1 vote

Answershttps://www.quora.com/Given-an-array-of-length-n-having-integers-0-to-n-1-in-unsorted-order-we-have-to-modify-this-array-such-that-the-value-at-a-n-becomes-a-a-n-for-example-if-a-0-contains-5-then-a-0-will-have-value-a-5-and-so-on-condition-is-that-this-should-take-O-n-time-complexity

- 9811633187a in India for hr| Report Duplicate | Flag | PURGE

Adobe Field Sales Algorithm

hmmm my mistake . but here you are using a recursive function that means extra stack space in memory. moreover can u give the algo for what u wrote

- 9811633187a November 17, 2015Hey..

1 st the time complexity is n^2. as the loop will run n*n times in ur program.

second the output will not be correct .

here is the simple iteration when i analyse ur program:-

{213540}

1st iteration = {3,1,3,5,4,0} here the value 2 is lost which should be at a[5] in the output .

So a wrong program and wrong logic

Sorry for the inconvenience . My question is following :

Suppose we have an array of length n. The array has distinct values from 0,1,2....n-1 in any order(sorted or unsorted). for example lets take the array as {2,1,3,5,4,0}.

we have to modify it such that a[i]=a[a[i]] which means a[0] becomes a[a[0]] and so on..in the following example the output should be:-

a[0] = 2 so a[0]=a[2]=3

a[1] = 1 so a[1]=a[1] = 1

a[2] = 3 so a[2] = a[3] = 5

.

.

and so on.

we will get the following output:

{3,1,5,0,4,2}. got it?

3 things are to be noted here

a) values of elements lies in the set {0 to n-1} and positions are also 0 to n-1 obviously.

b) Time complexity = O(n)

c) No extra array or any DS should be used .

Solve it if you can .

Nice try ..but the code is wrong .

At second time the loop gives p=q=2.

So it will keep on swapping the value p[2] with p[2] itself which is useless.

example:=

input array={2,4,1,3,0}

at first iteraqtion it will give = {1,4,2,3,0}

now what????????

p[2] with p[2]??

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

nothing you are right . I think this is the perfect solution thanks a lot.

- 9811633187a November 17, 2015