Open In App

Count array elements having sum of digits equal to K

Last Updated : 29 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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]);
 
    // 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>


Output: 

2

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads