Minimum length Unsorted Subarray, sorting which makes the array sorted | GeeksforGeeks

preview_player
Показать описание


This video is contributed by Shubham Kumar

Please Like, Comment and Share the Video among your friends.

Also, Subscribe if you haven't already! :)
Рекомендации по теме
Комментарии
Автор

He's so invested in reading it out that he even pronounced changed to chang-ed. Wow whatever the reason seems to be feel like we should empower more videos like this

snipegams
Автор

with space complexity can have better solution it keep two array of min and max element till any index from l to r and r to L?

SachinSingh-lbqz
Автор

class Solution{
public:
vector<int> printUnsorted(int arr[], int n)
{
int s, e, max, min;
vector<int>v;
// step 1(a) of above algo
for (s = 0; s < n-1; s++)
{
if (arr[s] > arr[s+1])
break;
}
if (s == n-1)
{
v.push_back(0);
v.push_back(0);
return v;
}

// step 1(b) of above algo
for(e = n - 1; e > 0; e--)
{
if(arr[e] < arr[e-1])
break;
}

// step 2(a) of above algo
max = arr[s]; min = arr[s];
for(int i = s + 1; i <= e; i++)
{
if(arr[i] > max)
max = arr[i];
if(arr[i] < min)
min = arr[i];
}

// step 2(b) of above algo
for( int i = 0; i < s; i++)
{
if(arr[i] > min)
{
s = i;
break;
}
}

// step 2(c) of above algo
for( int i = n -1; i >= e+1; i--)
{
if(arr[i] < max)
{
e = i;
break;
}
}

v.push_back(s);
v.push_back(e);
return v;
}
};

wecan
Автор

Thanks for reading the solution, I cannot read texts.

guruprakash
Автор

Is this called expaination or reading ppts? I wonder!

abijeetshyam
Автор

Fails for arrays with repeated element.
A : [ 1, 1, 10, 10, 15, 10, 15, 10, 10, 15, 10, 15 ]

siddharthachatterjee