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

📅  最后修改于: 2021-09-04 13:03:24             🧑  作者: 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


Javascript


输出:
4

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live