Dado un arreglo A[] de N enteros, la tarea es generar un arreglo B[] tal que B[i] contenga el conteo de índices j en A[] tal que j < i y A[j] % A[i ] = 0
Ejemplos:
Entrada: arr[] = {3, 5, 1}
Salida: 0 0 2
Explicación:
3 y 5 no dividen ningún elemento a
su izquierda pero 1 divide 3 y 5.
Entrada: arr[] = {8, 1, 28 , 4, 2, 6, 7}
Salida: 0 1 0 2 3 0 1
Enfoque ingenuo: este enfoque ya se analiza aquí . Pero la complejidad de este enfoque es O(N 2 ).
Enfoque eficiente:
- Podemos decir que si el número A divide a un número B, entonces A es un factor de B. Entonces, necesitamos encontrar el número de elementos anteriores cuyo factor es el elemento actual.
- Mantendremos una array de conteo que contiene el conteo del factor de cada elemento.
- Ahora, itere sobre la array y para cada elemento
- Haga que la respuesta del elemento actual sea igual a contar [arr[i]] y
- Incremente la frecuencia de cada factor de arr[i] en la array de conteo.
- Haga que la respuesta del elemento actual sea igual a contar [arr[i]] y
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print the // elements of the array void printArr(int arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; } // Function to increment the count // for each factor of given val void IncrementFactors(int count[], int val) { for (int i = 1; i * i <= val; i++) { if (val % i == 0) { if (i == val / i) { count[i]++; } else { count[i]++; count[val / i]++; } } } } // Function to generate and print // the required array void generateArr(int A[], int n) { int B[n]; // Find max element of array int maxi = *max_element(A, A + n); // Create count array of maxi size int count[maxi + 1] = { 0 }; // For every element of the array for (int i = 0; i < n; i++) { // Count[ A[i] ] denotes how many // previous elements are there whose // factor is the current element. B[i] = count[A[i]]; // Increment in count array for // factors of A[i] IncrementFactors(count, A[i]); } // Print the generated array printArr(B, n); } // Driver code int main() { int arr[] = { 8, 1, 28, 4, 2, 6, 7 }; int n = sizeof(arr) / sizeof(arr[0]); generateArr(arr, n); return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG{ // Utility function to print the // elements of the array static void printArr(int arr[], int n) { for(int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Function to increment the count // for each factor of given val static void IncrementFactors(int count[], int val) { for(int i = 1; i * i <= val; i++) { if (val % i == 0) { if (i == val / i) { count[i]++; } else { count[i]++; count[val / i]++; } } } } // Function to generate and print // the required array static void generateArr(int A[], int n) { int []B = new int[n]; // Find max element of array int maxi = Arrays.stream(A).max().getAsInt(); // Create count array of maxi size int count[] = new int[maxi + 1]; // For every element of the array for(int i = 0; i < n; i++) { // Count[ A[i] ] denotes how many // previous elements are there whose // factor is the current element. B[i] = count[A[i]]; // Increment in count array for // factors of A[i] IncrementFactors(count, A[i]); } // Print the generated array printArr(B, n); } // Driver code public static void main(String[] args) { int arr[] = { 8, 1, 28, 4, 2, 6, 7 }; int n = arr.length; generateArr(arr, n); } } // This code is contributed by Amit Katiyar
Python3
# Python3 implementation of the approach # Utility function to print # elements of the array def printArr(arr, n): for i in range(n): print(arr[i], end = " ") # Function to increment the count # for each factor of given val def IncrementFactors(count, val): i = 1 while(i * i <= val): if (val % i == 0): if (i == val // i): count[i] += 1 else: count[i] += 1 count[val // i] += 1 i += 1 # Function to generate and print # the required array def generateArr(A, n): B = [0] * n # Find max element of arr maxi = max(A) # Create count array of maxi size count = [0] * (maxi + 1) # For every element of the array for i in range(n): # Count[ A[i] ] denotes how many # previous elements are there whose # factor is the current element. B[i] = count[A[i]] # Increment in count array for # factors of A[i] IncrementFactors(count, A[i]) # Print the generated array printArr(B, n) # Driver code arr = [ 8, 1, 28, 4, 2, 6, 7 ] n = len(arr) generateArr(arr, n) # This code is contributed by code_hunt
C#
// C# implementation of the approach using System; using System.Linq; class GFG{ // Utility function to print the // elements of the array static void printArr(int []arr, int n) { for(int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Function to increment the count // for each factor of given val static void IncrementFactors(int []count, int val) { for(int i = 1; i * i <= val; i++) { if (val % i == 0) { if (i == val / i) { count[i]++; } else { count[i]++; count[val / i]++; } } } } // Function to generate and print // the required array static void generateArr(int []A, int n) { int []B = new int[n]; // Find max element of array int maxi = A.Max(); // Create count array of maxi size int []count = new int[maxi + 1]; // For every element of the array for(int i = 0; i < n; i++) { // Count[ A[i] ] denotes how many // previous elements are there whose // factor is the current element. B[i] = count[A[i]]; // Increment in count array for // factors of A[i] IncrementFactors(count, A[i]); } // Print the generated array printArr(B, n); } // Driver code public static void Main(String[] args) { int []arr = { 8, 1, 28, 4, 2, 6, 7 }; int n = arr.Length; generateArr(arr, n); } } // This code is contributed by Amit Katiyar
Javascript
<script> // Javascript implementation of the approach // Utility function to print the // elements of the array function printArr(arr, n) { for (let i = 0; i < n; i++) document.write(arr[i] + " "); } // Function to increment the count // for each factor of given val function IncrementFactors(count, val) { for (let i = 1; i * i <= val; i++) { if (val % i == 0) { if (i == parseInt(val / i)) { count[i]++; } else { count[i]++; count[parseInt(val / i)]++; } } } } // Function to generate and print // the required array function generateArr(A, n) { let B = new Array(n); // Find max element of array let maxi = Math.max(...A); // Create count array of maxi size let count = new Array(maxi + 1).fill(0); // For every element of the array for (let i = 0; i < n; i++) { // Count[ A[i] ] denotes how many // previous elements are there whose // factor is the current element. B[i] = count[A[i]]; // Increment in count array for // factors of A[i] IncrementFactors(count, A[i]); } // Print the generated array printArr(B, n); } // Driver code let arr = [ 8, 1, 28, 4, 2, 6, 7 ]; let n = arr.length; generateArr(arr, n); </script>
Producción:
0 1 0 2 3 0 1
Complejidad de tiempo: O(N * root(MaxElement))
Publicación traducida automáticamente
Artículo escrito por Chandan_Agrawal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA