Dadas dos arrays ordenadas de tamaño N . La tarea es encontrar el número máximo de elementos en la primera array que sean estrictamente mayores que los elementos de la segunda array, de modo que un elemento pueda considerarse solo una vez.
Ejemplos:
Entrada: arr1[] = { 20, 30, 50 }, arr2[] = { 25, 40, 60 }
Salida: 2
Explicación:
Máximo 2 elementos 30 (30 > 25) y 50 (50 > 40) de la array arr1 es mayor que ar2.
Entrada: arr1[] = { 10, 15, 20, 25, 30, 35 }, arr2[] = { 12, 14, 26, 32, 34, 40 }
Salida: 4
Explicación:
Máximo 4 elementos 15 (15 > 12 ), 20 (20 > 14), 30 (30 > 26) y 35 (35 > 34) de arr1 es mayor que arr2.
Acercarse:
- Compare los elementos de ambas arrays del índice 0 uno por uno.
- Si el elemento en el índice de arr1 es mayor que el elemento en el índice de arr2, aumente la respuesta y el índice de ambas arrays en 1.
- Si el elemento en el índice de arr1 es menor o igual que el elemento en el índice de arr2,
aumente el índice de arr1. - Repita los pasos anteriores hasta que el índice de cualquier array llegue al último elemento.
- Imprime la respuesta
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> 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
<script> // Javascript program for the above approach // Function to find greater elements function findMaxElements( arr1, arr2, n) { // Index counter for arr1 var cnt1 = 0; // Index counter for arr2 var cnt2 = 0; // To store the maximum elements var 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 document.write( maxelements ); } var arr1 = [10, 15, 20, 25, 30, 35]; var arr2 = [12, 14, 26, 32, 34, 40]; var n = arr1.length; findMaxElements(arr1, arr2, n); // This code is contributed by rrrtnx. </script>
4
Complejidad de tiempo: O(N) , donde N es la longitud de la array.
Complejidad del espacio: O(1)
Publicación traducida automáticamente
Artículo escrito por divyeshrabadiya07 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA