Dada una array no ordenada arr[] y un entero K , la tarea es contar las ocurrencias de K en la array dada utilizando el método Divide and Conquer .
Ejemplos:
Entrada: arr[] = {1, 1, 2, 2, 2, 2, 3}, K = 1
Salida: 2Entrada: arr[] = {1, 1, 2, 2, 2, 2, 3}, K = 4
Salida: 0
Enfoque: La idea es dividir la array en dos partes de igual tamaño y contar el número de ocurrencias de K en cada mitad y luego sumarlas.
- Divida la array en dos partes hasta que solo quede un elemento en la array.
- Compruebe si un solo elemento en la array es K o no. Si es K , devuelve 1 , de lo contrario , 0 .
- Sume los valores devueltos para cada uno de los elementos para encontrar la ocurrencia de K en toda la array.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implrmrntation of the approach #include <iostream> using namespace std; // Function to return the frequency of x // in the subarray arr[low...high] int count(int arr[], int low, int high, int x) { // If the subarray is invalid or the // element is not found if ((low > high) || (low == high && arr[low] != x)) return 0; // If there's only a single element // which is equal to x if (low == high && arr[low] == x) return 1; // Divide the array into two parts and // then find the count of occurrences // of x in both the parts return count(arr, low, (low + high) / 2, x) + count(arr, 1 + (low + high) / 2, high, x); } // Driver code int main() { int arr[] = { 30, 1, 42, 5, 56, 3, 56, 9 }; int n = sizeof(arr) / sizeof(int); int x = 56; cout << count(arr, 0, n - 1, x); return 0; }
Java
// Java implrmrntation of the approach class GFG { // Function to return the frequency of x // in the subarray arr[low...high] static int count(int arr[], int low, int high, int x) { // If the subarray is invalid or the // element is not found if ((low > high) || (low == high && arr[low] != x)) return 0; // If there's only a single element // which is equal to x if (low == high && arr[low] == x) return 1; // Divide the array into two parts and // then find the count of occurrences // of x in both the parts return count(arr, low, (low + high) / 2, x) + count(arr, 1 + (low + high) / 2, high, x); } // Driver code public static void main(String args[]) { int arr[] = { 30, 1, 42, 5, 56, 3, 56, 9 }; int n = arr.length; int x = 56; System.out.print(count(arr, 0, n - 1, x)); } }
Python3
# Python3 implrmrntation of the approach # Function to return the frequency of x # in the subarray arr[low...high] def count(arr, low, high, x): # If the subarray is invalid or the # element is not found if ((low > high) or (low == high and arr[low] != x)): return 0; # If there's only a single element # which is equal to x if (low == high and arr[low] == x): return 1; # Divide the array into two parts and # then find the count of occurrences # of x in both the parts return count(arr, low, (low + high) // 2, x) +\ count(arr, 1 + (low + high) // 2, high, x); # Driver code if __name__ == '__main__': arr = [ 30, 1, 42, 5, 56, 3, 56, 9]; n = len(arr); x = 56; print(count(arr, 0, n - 1, x)); # This code is contributed by PrinciRaj1992
C#
// C# implrmrntation of the approach using System; class GFG { // Function to return the frequency of x // in the subarray arr[low...high] static int count(int []arr, int low, int high, int x) { // If the subarray is invalid or the // element is not found if ((low > high) || (low == high && arr[low] != x)) return 0; // If there's only a single element // which is equal to x if (low == high && arr[low] == x) return 1; // Divide the array into two parts and // then find the count of occurrences // of x in both the parts return count(arr, low, (low + high) / 2, x) + count(arr, 1 + (low + high) / 2, high, x); } // Driver code public static void Main() { int []arr = { 30, 1, 42, 5, 56, 3, 56, 9 }; int n = arr.Length; int x = 56; Console.Write(count(arr, 0, n - 1, x)); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implrmrntation of the approach // Function to return the frequency of x // in the subarray arr[low...high] function count(arr, low, high, x) { // If the subarray is invalid or the // element is not found if ((low > high) || (low == high && arr[low] != x)) return 0; // If there's only a single element // which is equal to x if (low == high && arr[low] == x) return 1; // Divide the array into two parts and // then find the count of occurrences // of x in both the parts return count(arr, low, Math.floor((low + high) / 2), x) + count(arr, 1 + Math.floor((low + high) / 2), high, x); } // Driver code let arr = [30, 1, 42, 5, 56, 3, 56, 9]; let n = arr.length; let x = 56; document.write(count(arr, 0, n - 1, x)); // This code is contributed by _saurabh_jaiswal </script>
Producción:
2
Complejidad de tiempo: O (NlogN)
Publicación traducida automáticamente
Artículo escrito por mohan_mohadikar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA