Count array elements having sum of digits equal to K

29 Jan, 2022
Given an array arr[] of size N, the task is to count the number of array elements whose sum of digits is equal to K.
Examples:
Input: arr[] = {23, 54, 87, 29, 92, 62}, K = 11Output: 2Explanation: 29 = 2 + 9 = 1192 = 9 + 2 = 11
Input: arr[]= {11, 04, 57, 99, 98, 32}, K = 18Output: 1
Approach: Follow the steps below to solve the problem:
Initialize a variable, say N, to store the size of the array.
Initialize a variable, say count, to store the elements having sum of digits equal to K.
Declare a function, sumOfDigits() to calculate the sum of digits of a number.
Traverse the array arr[] and for each array element, check if the sum of digits is equal to K or not. If found to be true, then increment count by 1.
Print the value of count as the required answer.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to calculate the// sum of digits of the number Nint 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 elementsint 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 Codeint 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 program for the above approachpublic 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 program for the above approach # Function to calculate the# sum of digits of the number Ndef 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 elementsdef 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 arrayarr = [ 23, 54, 87, 29, 92, 62 ] # Given value of KK = 11 # Size of the arrayN = len(arr) # Function call to count array elements# having sum of digits equal to KelementsHavingDigitSumK(arr, N, K) # This code is contributed by souravghosh0416.
// C# program for the above approachusing 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.
<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>
2
 
Time Complexity: O(N * logN)Auxiliary Space: O(1)
divyeshrabadiya07
ankthon
souravghosh0416
simmytarika5
suresh07
saurabh1990aror
Technical Scripter 2020
Arrays
Mathematical
Technical Scripter
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Search, insert and delete in an unsorted array
Window Sliding Technique
Find duplicates in O(n) time and O(1) extra space | Set 1
Chocolate Distribution Problem
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Coin Change | DP-7