The C language finds an algorithm to parse a particular element in an array

  • 2020-05-09 19:00:27
  • OfStack

        problem description: an int array with unlimited data requires that all such Numbers a[i] be found with the number on the left less than or equal to it and the number on the right greater than or equal to it. Can you do this with just one extra array and a little more space?
          idea: if you can use two auxiliary arrays, then it is relatively simple to define the arrays Min and Max, where Min[i] represents the minimum value after a[i] (including a[i]) and Max[i] represents the maximum value of elements before a[i]. With these two auxiliary arrays, for a[i], if it is greater than Max[i-1] and less than Min[i+1], then it meets the requirement.
        but they want to use only one extra array. Actually, Max array can be omitted, and it can be evaluated at the same time. This is because Max[i] is calculated from left to right, while it is judged from left to right. All you need to do is save 1 of the current maximum using the 1 variable Max. The code implementations of the two methods are given below.
          reference code:


// The functionality   :   To find elements  
// Function parameters   :  pArray Pointing to an array, len Is the number of elements in the array  
// The return value   :   There is no  
void FindElements_Solution1(int *pArray, int len) 
{ 
  if(pArray == NULL || len <= 0 ) 
    return ; 
 
  int *pMin = new int[len]; 
  int *pMax = new int[len]; 
  int i; 
 
  pMax[0] = pArray[0]; 
  for(i = 1; i < len; i++)    // Calculated from the i An auxiliary array of previous maximum values  
    pMax[i] = (pMax[i-1] >= pArray[i])? pMax[i-1]: pArray[i]; 
  pMin[len-1] = pArray[len-1]; 
  for(i = len - 2; i >= 0; i--) // Calculated from the i An auxiliary array of starting minimum values  
    pMin[i] = (pMin[i+1] <= pArray[i])? pMin[i+1]: pArray[i]; 
 
  if(pArray[0] <= pMin[0])   // Check the first 1 Whether the element satisfies the condition  
    cout<<pArray[0]<<' '; 
  for(i = 1; i < len - 1; i++) 
  { 
    if(pArray[i] >= pMax[i-1] && pArray[i] <=pMin[i+1]) // The elements that satisfy this relationship satisfy the requirements  
      cout<<pArray[i]<<' '; 
  } 
  if(pArray[len-1] >= pMax[len-1]) // Check the first len Whether the element satisfies the condition  
    cout<<pArray[i]; 
  cout<<endl; 
 
  delete [] pMin; 
  delete [] pMax; 
  pMin = pMax = NULL; 
} 


void FindElements_Solution2(int *pArray, int len) 
{ 
  if(pArray == NULL || len <= 0 ) 
    return ; 
 
  int *pMin = new int[len]; 
  int Max; 
  int i; 
 
  Max = pArray[0]; 
  pMin[len-1] = pArray[len-1]; 
  for(i = len - 2; i >= 0; i--) // Calculated from the i An auxiliary array of starting minimum values  
    pMin[i] = (pMin[i+1] <= pArray[i])? pMin[i+1]: pArray[i]; 
 
  if(pArray[0] <= pMin[0])   // Check the first 1 Whether the element satisfies the condition  
    cout<<pArray[0]<<' '; 
 
  for(i = 1; i < len - 1; i++) 
  { 
    if(pArray[i] >= Max && pArray[i] <=pMin[i+1]) // The elements that satisfy this relationship satisfy the requirements  
      cout<<pArray[i]<<' '; 
    Max = (Max < pArray[i])? pArray[i]: Max; // Update current maximum  
  } 
  if(pArray[len-1] >= Max) // Check the first len Whether the element satisfies the condition  
    cout<<pArray[i]; 
  cout<<endl; 
 
  delete [] pMin; 
  pMin = NULL; 
} 

Find two Numbers in an array that appear only once.
  problem description: every number in an integer array appears twice except two. Please write a program to find these two Numbers that appear only once. The required time complexity is O(n) and space complexity is O(1).
        idea: if only one number appears once, and the others appear twice, then all the Numbers can be performed once, because the result of the same number is 0. If you have two Numbers that only happen once, and the other Numbers happen twice. What to do? The book "the beauty of programming" provides a method, that is, all the Numbers do an xor operation once, get a number, and then take a non-zero bit of the number as a filter bit, the array is divided into two parts, at this time only once the number will be divided into different parts. Now the problem is that it only happens once, so you can do the xor for each part.
        reference code:


// The functionality   :   Find two that only appear in the array 1 Time of digital  
// Function parameters   :  arr Is the source array, len Is the number of array elements, result To store the results   
// The return value   :    There is no  
void FindIsolateTwo(int *arr, int len, int *result) 
{ 
  int i, all = 0, flag = 1; 
 
  for(i = 0; i < len ; i++) // All Numbers are different or  
    all ^= arr[i]; 
 
  while(!(all&flag)) // Search for filter bits  
    flag <<= 1; 
 
  result[0] = result[1] = 0; 
  for(i = 0; i < len; i++) // Distinguish by filtering bits  
  { 
    if(flag&arr[i]) 
      result[0] ^= arr[i]; 
    else 
      result[1] ^= arr[i]; 
  } 
} 


Related articles: