This blog site is dedicated for articles on problem solving using C, C++, data structures, algorithms. Besides this few technical articles are also presented.
Mar 7, 2022
Minimum length unsorted array
#include
using namespace std;
void printArray(int a[], int len)
{
for(int i=0;i a[i+1])
{
left = i;
break;
}
}
if(left == -1)
{
cout << "arary is sorted";
return;
}
for(int i=n-1; i>left; i--)
{
if(a[i] < a[i-1])
{
right = i;
break;
}
}
int min=a[left];
int max=a[left];
for(int i=left+1; i<=right; i++)
{
if(a[i] < min)
min = a[i];
if(a[i] > max)
max = a[i];
}
for(int i=0;i min)
{
left = i;
break;
}
}
for(int i=n; i>right-1; i--)
{
if(a[i] < max)
{
right = i;
break;
}
}
cout << "left =" << left << " right=" << right << endl;
}
int main() {
// your code goes here
int a[] = {10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60};
cout << "array - ";
printArray(a, sizeof(a)/sizeof(int));
minLengthUnsortedSubArray(a, sizeof(a)/sizeof(int));
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment