# (AC) C solution

• #define min(a,b) (a<b)?a:b
#define max(a,b) (a>b)?a:b
bool swap(int* t1,int* t2);
void sort(int* nums,int left,int right,int* min_left,int* max_right,int* flag);
int findUnsortedSubarray(int* nums, int numsSize) {
int min_idx=0;
int max_idx=numsSize-1;
int val_min=nums[0];
int val_max=nums[numsSize-1];
int left=0;
int right=numsSize-1;
int a=10000,b=-1;
int lflag=false,rflag=false;
printf("min_idx=%d max_idx=%d\n",min_idx,max_idx);
//[1,2,3,4]
while(left<=right){
for(int i=left;i<=right;i++)
{
if(nums[i]>val_max){
val_max=nums[i];
max_idx=i;
}
if(nums[i]<val_min){
val_min=nums[i];
min_idx=i;
}
}
printf("min_idx=%d max_idx=%d\n",min_idx,max_idx);
if(left!=min_idx){
a=min(left,a);
b=max(min_idx,b);
swap(&nums[left],&nums[min_idx]);
lflag=true;
}
left++;
min_idx=left;
val_min=nums[min_idx];
if(right!=max_idx){
a=min(max_idx,a);
b=max(right,b);
swap(&nums[right],&nums[max_idx]);
rflag=true;
}
right--;
max_idx=right;
val_max=nums[max_idx];
if(lflag && rflag)
break;
}
printf("a=%d b=%d\n",a,b);
return (a!=10000 && b!=-1)?(b-a+1):0;
}
bool swap(int* t1,int* t2)
{
if(*t1==*t2)return false;
*t1^=*t2;
*t2^=*t1;
*t1^=*t2;
return true;
}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.