Skip to content
Related Articles

Related Articles

Count array elements having sum of digits equal to K
  • Difficulty Level : Hard
  • Last Updated : 04 Mar, 2021
GeeksforGeeks - Summer Carnival Banner

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 = 11
Output: 2
Explanation: 
29 = 2 + 9 = 11
92 = 9 + 2 = 11

Input: arr[]= {11, 04, 57, 99, 98, 32}, K = 18
Output: 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++




// 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]);
 
    // Funtion 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;
   
    // Funtion 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
 
    # Prthe 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)
 
# Funtion 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;
   
    // Funtion call to count array elements
    // having sum of digits equal to K
    elementsHavingDigitSumK(arr, N, K);
  }
}
 
// This code is contributed by divyeshrabadiya07.

 
 

Output: 
2

 

Time Complexity: O(N * logN)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :