w3resource

C Exercises: Find the minimum length of subarray such that, sorting this subarray makes the whole array sorted

C Array: Exercise-91 with Solution

An unsorted array of a specific size is given. Write a program in C to find the minimum length of a subarray such that sorting this subarray makes the whole array sorted.

Sample Solution:

C Code:

#include<stdio.h> 
   
void findUnsortedSubArr (int arr1[], int arr_size) 
{ 
  int m = 0, en = arr_size-1, i, max, min;    
   
  for (m = 0; m < arr_size-1; m++) 
  { 
    if (arr1[m] > arr1[m+1]) 
      break; 
  } 
  if (m == arr_size-1) 
  { 
    printf("The given array is sorted."); 
    return; 
  } 
   
  for(en = arr_size - 1; en > 0; en--) 
  { 
    if(arr1[en] < arr1[en-1]) 
      break; 
  } 
   
  max = arr1[m]; min = arr1[m]; 
  for(i = m + 1; i <= en; i++) 
  { 
    if(arr1[i] > max) 
      max = arr1[i]; 
    if(arr1[i] < min) 
      min = arr1[i]; 
  } 
   
  for( i = 0; i < m; i++) 
  { 
    if(arr1[i] > min) 
    {   
      m = i; 
      break; 
    }       
  }  
   
  for( i = arr_size -1; i >= en+1; i--) 
  { 
    if(arr1[i] < max) 
    { 
      en = i; 
      break; 
    }  
  }   
  printf("The minimum length of unsorted subarray which makes the given array sorted ");
   printf("\nlies between the indeces %d and %d", m, en); 
  return; 
} 
   
int main() 
{ 
  int arr1[] = {10, 12, 15, 17, 28, 32, 42, 18, 56, 59, 67}; 
  int arr_size = sizeof(arr1)/sizeof(arr1[0]); 
  int i;
   //------------- print original array ------------------	
	printf("The given array is:  \n");
	for(i = 0; i < arr_size; i++)
		{
			printf("%d  ", arr1[i]);
		}
	printf("\n");
//-----------------------------------------------------------  
  findUnsortedSubArr(arr1, arr_size); 
  return 0; 
} 

Sample Output:

The given array is:  
10  12  15  17  28  32  42  18  56  59  67  
The minimum length of unsorted subarray which makes the given array sorted 
lies between the indeces 4 and 7

Pictorial Presentation:

C Exercises: Find the minimum length of subarray such that,sorting this subarray makes the whole array sorted

Flowchart:

Flowchart: Find the minimum length of subarray such that,sorting this subarray makes the whole array sorted

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Given an array of size n such that every element is in the range from 0 to n-1. Write a program in C to rearrange the given array so that arr[i] becomes arr[arr[i]].
Next: Write a program in C that checks whether the elements in an unsorted array appears consecutively or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What does i = (i, ++i, 1) + 1; do?

In the expression (i, ++i, 1), the comma used is the comma operator

the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).

Because it discards its first operand, it is generally only useful where the first operand has desirable side effects. If the side effect to the first operand does not takes place, then the compiler may generate warning about the expression with no effect.

So, in the above expression, the leftmost i will be evaluated and its value will be discarded. Then ++i will be evaluated and will increment i by 1 and again the value of the expression ++i will be discarded, but the side effect to i is permanent. Then 1 will be evaluated and the value of the expression will be 1.

It is equivalent to:

i;          // Evaluate i and discard its value. This has no effect.
++i;        // Evaluate i and increment it by 1 and discard the value of expression ++i
i = 1 + 1;  

Note that the above expression is perfectly valid and does not invoke undefined behavior because there is a sequence point between the evaluation of the left and right operands of the comma operator.

Ref : https://bit.ly/3saxONC





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook