Dada una array arr[] de tamaño N , la tarea es contar el número de elementos de la array cuya suma de dígitos es igual a K.
Ejemplos:
Entrada: arr[] = {23, 54, 87, 29, 92, 62}, K = 11
Salida: 2
Explicación:
29 = 2 + 9 = 11
92 = 9 + 2 = 11Entrada: arr[]= {11, 04, 57, 99, 98, 32}, K = 18
Salida: 1
Enfoque: siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos N , para almacenar el tamaño de la array.
- Inicialice una variable, digamos count , para almacenar los elementos que tengan una suma de dígitos igual a K .
- Declare una función, sumOfDigits() para calcular la suma de los dígitos de un número .
- Recorra la array arr[] y para cada elemento de la array, verifique si la suma de los dígitos es igual a K o no. Si se encuentra que es cierto, entonces incremente el conteo en 1 .
- Imprime el valor de count como la respuesta requerida.
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 calculate the // sum of digits of the number N int sumOfDigits(int N) { // Stores the sum of digits int sum = 0; while (N != 0) { sum += N % 10; N /= 10; } // Return the sum return sum; } // Function to count array elements int elementsHavingDigitSumK(int arr[], int N, int K) { // Store the count of array // elements having sum of digits K int count = 0; // Traverse the array for (int i = 0; i < N; ++i) { // If sum of digits is equal to K if (sumOfDigits(arr[i]) == K) { // Increment the count count++; } } // Print the count cout << count; } // Driver Code int main() { // Given array int arr[] = { 23, 54, 87, 29, 92, 62 }; // Given value of K int K = 11; // Size of the array int N = sizeof(arr) / sizeof(arr[0]); // Function call to count array elements // having sum of digits equal to K elementsHavingDigitSumK(arr, N, K); return 0; }
Java
// Java program for the above approach public class GFG { // Function to calculate the // sum of digits of the number N static int sumOfDigits(int N) { // Stores the sum of digits int sum = 0; while (N != 0) { sum += N % 10; N /= 10; } // Return the sum return sum; } // Function to count array elements static void elementsHavingDigitSumK(int[] arr, int N, int K) { // Store the count of array // elements having sum of digits K int count = 0; // Traverse the array for (int i = 0; i < N; ++i) { // If sum of digits is equal to K if (sumOfDigits(arr[i]) == K) { // Increment the count count++; } } // Print the count System.out.println(count); } // Driver code public static void main(String args[]) { // Given array int[] arr = { 23, 54, 87, 29, 92, 62 }; // Given value of K int K = 11; // Size of the array int N = arr.length; // Function call to count array elements // having sum of digits equal to K elementsHavingDigitSumK(arr, N, K); } } // This code is contributed by AnkThon
Python3
# Python3 program for the above approach # Function to calculate the # sum of digits of the number N def sumOfDigits(N) : # Stores the sum of digits sum = 0 while (N != 0) : sum += N % 10 N //= 10 # Return the sum return sum # Function to count array elements def elementsHavingDigitSumK(arr, N, K) : # Store the count of array # elements having sum of digits K count = 0 # Traverse the array for i in range(N): # If sum of digits is equal to K if (sumOfDigits(arr[i]) == K) : # Increment the count count += 1 # Print the count print(count) # Driver Code # Given array arr = [ 23, 54, 87, 29, 92, 62 ] # Given value of K K = 11 # Size of the array N = len(arr) # Function call to count array elements # having sum of digits equal to K elementsHavingDigitSumK(arr, N, K) # This code is contributed by souravghosh0416.
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function to calculate the // sum of digits of the number N static int sumOfDigits(int N) { // Stores the sum of digits int sum = 0; while (N != 0) { sum += N % 10; N /= 10; } // Return the sum return sum; } // Function to count array elements static void elementsHavingDigitSumK(int[] arr, int N, int K) { // Store the count of array // elements having sum of digits K int count = 0; // Traverse the array for (int i = 0; i < N; ++i) { // If sum of digits is equal to K if (sumOfDigits(arr[i]) == K) { // Increment the count count++; } } // Print the count Console.WriteLine(count); } // Driver code static void Main() { // Given array int[] arr = { 23, 54, 87, 29, 92, 62 }; // Given value of K int K = 11; // Size of the array int N = arr.Length; // Function call to count array elements // having sum of digits equal to K elementsHavingDigitSumK(arr, N, K); } } // This code is contributed by divyeshrabadiya07.
Javascript
<script> // JavaScript program for the above approach // Function to calculate the // sum of digits of the number N function sumOfDigits(N) { // Stores the sum of digits let sum = 0; while (N != 0) { sum += N % 10; N = parseInt(N / 10, 10); } // Return the sum return sum; } // Function to count array elements function elementsHavingDigitSumK(arr, N, K) { // Store the count of array // elements having sum of digits K let count = 0; // Traverse the array for (let i = 0; i < N; ++i) { // If sum of digits is equal to K if (sumOfDigits(arr[i]) == K) { // Increment the count count++; } } // Print the count document.write(count); } // Given array let arr = [ 23, 54, 87, 29, 92, 62 ]; // Given value of K let K = 11; // Size of the array let N = arr.length; // Function call to count array elements // having sum of digits equal to K elementsHavingDigitSumK(arr, N, K); </script>
Producción:
2
Complejidad de tiempo: O(N * logN)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por patelajeet y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA