Rearrange Array so that Arr[i] becomes Arr[Arr[i]] with O(1) extra space #InterviewBit Code C++

preview_player
Показать описание
Rearrange a given array so that Arr[i] becomes Arr[Arr[i]] with O(1) extra space.

Example:

Input : [1, 0]
Return : [0, 1]
Let's say N = size of the array. Then, the following holds true :

All elements in the array are in the range [0, N-1]
N * N does not overflow for a signed integer
Рекомендации по теме
Комментарии
Автор

Awesome explanation! I have started solving InterviewBit problems, hopefully u have ciovered them all <3

siddharthb
Автор

for the people why she is taking n as parameter of dividing or taking modulo because as the question suggested the element is less than n so the number should be in between .might clear

piyushsrivastava
Автор

plz continue creating such great sols..thanks

niranjanaware
Автор

Thanks for explanation...Thanks my dear sister.

venkatanagaprathapyelugula
Автор

I'm searching a good video for array explanation and I really find good content. Thank you so much for sharing this. 😀

AmanRaj-uxcw
Автор

Great explanation! Immediately subscribed :)

anshikaaa
Автор

Hey Alisha Great Explanation but have one doubt that its like we have to mug up this approach so if i try to solve this after 1 week i definitely forget it so how to remember these type of questions?

neerajgarg
Автор

can you give me explanation on this= sum(arr, n-1)+arr[n-1], what exactly going on here

SachinNagareiGTB
Автор

Thanks Alisha....what an easy gave me learning without wasting much time

dspeaks
Автор

Muskuraiye ...you have a new subscriber 😁

asad_mirza
Автор

new approach .... SC O(1)... TC O(n) ....

void arrange(long long arr[], int n) {

for(int i=0;i<n;i++)
{
if(arr[i]<0)
{
continue;
}

int ind=i;
int temp=arr[i];
int j;
while(arr[ind]>=0)
{
j=arr[ind];
int val=arr[j];
if(val<0)
{
break;
}

arr[ind]=val-1e6;

ind=j;
}

arr[ind]=temp-1e6;

}

for(int i=0;i<n;i++)
{
arr[i]=arr[i]+1e6;
}

return ;
}

billgatesbisht
Автор

Best explanation ever, thanks, please keep making such awesome videos! learning a lot

HarendraKumar-yrgt
Автор

What is the intuition behind this approach.

avishjain
Автор

clean and clear explanation thanks for good content..

harshitsharma
Автор

Useful video...explanation is understandable

reshmah
Автор

i thought why there's need to put modulo n in arr[arr[i]]%n *n since all the numbers in arr are less than n
but
now i realized as we multiply no's are changing at arr[i] so we have to first modulo n then just do multiply with n

rohankumarshah
Автор

How to get logic these type of question 😢

learncreative
Автор

thanks and keep making the such awesome videos.

rahultomar
Автор

Should have focused on the mathematical intuition

suyashpurwar
Автор

Why should we take %n before multiplying with n?

lakshmipriya