Dada una array arr[] . La tarea es contar los elementos de la array que son divisibles por su producto de dígitos o por la suma de dígitos.
Ejemplo:
Entrada: arr[] = {123, 25, 36, 7}
Salida: 2
Explicación: Los siguientes son los elementos que siguen las condiciones dadas La
suma de los dígitos de 123 es 6 y el producto de los dígitos también es 6.
Dado que 123 no es divisible por 6, por lo que no se cuenta.
La suma de los dígitos de 36 es 9 y el producto de los dígitos es 18. Dado que 36 es divisible por ambos, se considera como respuesta.
De manera similar, la suma de dígitos y el producto de dígitos de 7 es 7 en sí mismo, por lo tanto, también se considera.
Por lo tanto, 2 es la respuesta final.Entrada: arr[] = {10, 22, 15}
Salida: 2
Explicación: La suma de los dígitos en 10 es 1, y 10 es divisible por 1, por lo que lo consideramos. El producto de dígitos de 15 es 5, y 15 es divisible por 5, lo consideramos en nuestra salida.
Enfoque: Este problema está basado en la observación. Siga los pasos a continuación para resolver el problema dado.
- Declare una variable, digamos count = 0 , para almacenar el número de elementos que satisfacen las condiciones dadas.
- Para cada número en la array arr[] , encuentre la suma de sus dígitos y el producto de sus dígitos.
- Comprueba si el número es divisible por su suma de dígitos o por el producto de dígitos.
- En caso afirmativo, agréguelo a su variable de conteo.
- Devuelve el conteo como la respuesta final.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program for above approach #include <iostream> using namespace std; // Function to find the number of elements // in arr[] that follows the given conditions int solve(int arr[], int n) { // To count the number of elements // that follow the given conditions int count = 0; // Iterate through the array arr[] for (int i = 0; i < n; i++) { int p = arr[i]; int sum = 0, prod = 1; while (p > 0) { int r = p % 10; sum += r; prod *= r; p /= 10; } // Check if condition satisfies or not if ((sum > 0 && arr[i] % sum == 0) || (prod > 0 && arr[i] % prod == 0)) count++; } // Return count as the final answer return count; } // Driver Code int main() { int arr[] = { 123, 25, 36, 7 }; int N = sizeof(arr) / sizeof(arr[0]); // Function Call cout << solve(arr, N) << endl; return 0; }
Java
// Java program for above approach import java.util.*; public class GFG { // Function to find the number of elements // in arr[] that follows the given conditions static int solve(int[] arr, int n) { // To count the number of elements // that follow the given conditions int count = 0; // Iterate through the array arr[] for (int i = 0; i < n; i++) { int p = arr[i]; int sum = 0, prod = 1; while (p > 0) { int r = p % 10; sum += r; prod *= r; p /= 10; } // Check if condition satisfies or not if ((sum > 0 && arr[i] % sum == 0) || (prod > 0 && arr[i] % prod == 0)) count++; } // Return count as the final answer return count; } // Driver Code public static void main(String args[]) { int[] arr = { 123, 25, 36, 7 }; int N = arr.length; // Function Call System.out.println(solve(arr, N)); } } // This code is contributed by Samim Hossain Mondal.
Python3
# python3 program for above approach # Function to find the number of elements # in arr[] that follows the given conditions def solve(arr, n): # To count the number of elements # that follow the given conditions count = 0 # Iterate through the array arr[] for i in range(0, n): p = arr[i] sum = 0 prod = 1 while (p > 0): r = p % 10 sum += r prod *= r p //= 10 # Check if condition satisfies or not if ((sum > 0 and arr[i] % sum == 0) or (prod > 0 and arr[i] % prod == 0)): count += 1 # Return count as the final answer return count # Driver Code if __name__ == "__main__": arr = [123, 25, 36, 7] N = len(arr) # Function Call print(solve(arr, N)) # This code is contributed by rakeshsahni
C#
// C# program for above approach using System; class GFG { // Function to find the number of elements // in arr[] that follows the given conditions static int solve(int[] arr, int n) { // To count the number of elements // that follow the given conditions int count = 0; // Iterate through the array arr[] for (int i = 0; i < n; i++) { int p = arr[i]; int sum = 0, prod = 1; while (p > 0) { int r = p % 10; sum += r; prod *= r; p /= 10; } // Check if condition satisfies or not if ((sum > 0 && arr[i] % sum == 0) || (prod > 0 && arr[i] % prod == 0)) count++; } // Return count as the final answer return count; } // Driver Code public static void Main() { int[] arr = { 123, 25, 36, 7 }; int N = arr.Length; // Function Call Console.Write(solve(arr, N)); } } // This code is contributed by ukasp.
Javascript
<script> // Javascript program for above approach // Function to find the number of elements // in arr[] that follows the given conditions function solve(arr, n) { // To count the number of elements // that follow the given conditions let count = 0; // Iterate through the array arr[] for (let i = 0; i < n; i++) { let p = arr[i]; let sum = 0, prod = 1; while (p > 0) { let r = p % 10; sum += r; prod *= r; p = Math.floor(p / 10); } // Check if condition satisfies or not if ((sum > 0 && arr[i] % sum == 0) || (prod > 0 && arr[i] % prod == 0)) count++; } // Return count as the final answer return count; } // Driver Code let arr = [123, 25, 36, 7 ]; let N = arr.length; // Function Call document.write(solve(arr, N)); // This code is contributed by gfgking. </script>
2
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por arnavjha07 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA