📌  相关文章
📜  第一个数组中的元素数大于第二个数组中的元素数,每个元素仅考虑一次

📅  最后修改于: 2021-05-19 18:33:33             🧑  作者: Mango

给定两个大小为N的排序数组。任务是在第一个数组中找到最大数量的元素,这些元素严格大于第二个数组中的元素,这样一个元素只能被考虑一次。

例子:

方法:

  1. 索引0开始一个一个地比较两个数组的元素。
  2. 如果ARR1的索引处的元素ARR2的指标处比元件大于增加答案和两个阵列的由1的索引
  3. 如果ARR1的索引处的元素是更小或ARR2的索引以等于元件然后
    增加arr1的索引。
  4. 重复上述步骤,直到任何数组的索引都到达最后一个元素。
  5. 打印答案

下面是上述方法的实现:

C++
// C++ program for the above approach
  
#include 
using namespace std;
  
// Function to find greater elements
void findMaxElements(
    int arr1[], int arr2[], int n)
{
    // Index counter for arr1
    int cnt1 = 0;
    // Index counter for arr2
    int cnt2 = 0;
    // To store the maximum elements
    int maxelements = 0;
  
    while (cnt1 < n && cnt2 < n) {
  
        // If element is greater,
        // update maxelements and counters
        // for both the arrays
        if (arr1[cnt1] > arr2[cnt2]) {
            maxelements++;
            cnt1++;
            cnt2++;
        }
        else {
            cnt1++;
        }
    }
  
    // Print the maximum elements
    cout << maxelements << endl;
}
  
int main()
{
    int arr1[] = { 10, 15, 20, 25, 30, 35 };
    int arr2[] = { 12, 14, 26, 32, 34, 40 };
  
    int n = sizeof(arr1) / sizeof(arr1[0]);
  
    findMaxElements(arr1, arr2, n);
  
    return 0;
}


Java
// Java program for the above approach 
class Main{     
      
// Function to find greater elements     
static void findmaxelements(int arr1[], int arr2[], int n)         
{ 
    // Index counter for arr1 
    int cnt1 = 0;
      
    // Index counter for arr1 
    int cnt2 = 0;
      
    // To store the maximum elements 
    int maxelements = 0;         
          
    while(cnt1 < n && cnt2 < n)     
    {
              
        // If element is greater, 
        // update maxelements and counters 
        // for both the arrays 
        if(arr1[cnt1] > arr2[cnt2])     
        {     
            maxelements++;     
            cnt1++;     
            cnt2++;     
        }     
        else
        {     
            cnt1++;     
        }     
    }     
      
    // Print the maximum elements 
    System.out.println(maxelements);         
}
  
// Driver Code    
public static void main(String[] args)
{
          
    int arr1[] = { 10, 15, 20, 25, 30, 35 };         
    int arr2[] = { 12, 14, 26, 32, 34, 40 };
          
    findmaxelements(arr1, arr2, arr1.length);     
}     
}     
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
  
# Function to find greater elements         
def findmaxelements(arr1, arr2, n):     
      
    # Index counter for arr1     
    cnt1 = 0
      
    # Index counter for arr2
    cnt2 = 0
      
    # To store the maximum elements
    maxelements = 0    
      
    # If element is greater, 
    # update maxelements and counters 
    # for both the arrays 
    while cnt1 < n and cnt2 < n :
          
        if arr1[cnt1] > arr2[cnt2] :     
            maxelements += 1        
            cnt1 += 1    
            cnt2 += 1
              
        else :     
            cnt1 += 1
      
    # Print the maximum elements 
    print(maxelements)
      
# Driver Code    
arr1 = [ 10, 15, 20, 25, 30, 35 ]     
arr2 = [ 12, 14, 26, 32, 34, 40 ] 
  
findmaxelements(arr1, arr2, len(arr1))
  
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach 
using System; 
  
class GFG{ 
      
// Function to find greater elements     
static void findmaxelements(int[] arr1, 
                            int[] arr2, int n)     
{ 
      
    // Index counter for arr1 
    int cnt1 = 0; 
      
    // Index counter for arr1 
    int cnt2 = 0; 
      
    // To store the maximum elements 
    int maxelements = 0;         
          
    while(cnt1 < n && cnt2 < n) 
    { 
              
        // If element is greater, update 
        // maxelements and counters for
        // both the arrays 
        if(arr1[cnt1] > arr2[cnt2]) 
        { 
            maxelements++; 
            cnt1++; 
            cnt2++; 
        } 
        else
        { 
            cnt1++; 
        } 
    } 
      
    // Print the maximum elements 
    Console.Write(maxelements);
} 
  
// Driver Code 
static public void Main(string[] args) 
{ 
          
    int[] arr1 = { 10, 15, 20, 25, 30, 35 };         
    int[] arr2 = { 12, 14, 26, 32, 34, 40 }; 
          
    findmaxelements(arr1, arr2, arr1.Length); 
} 
} 
  
// This code is contributed by rutvik_56


输出:
4

时间复杂度: O(N) ,其中N是数组的长度。
空间复杂度: O(1)