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)



Similar Reads

Count of numbers in range [L, R] having sum of digits of its square equal to square of sum of digits
Given two integers L and R, the task is to find the count of numbers in range [L, R] such that the sum of digits of its square is equal to the square of sum of its digits, Example: Input: L = 22, R = 22Output: 1Explanation: 22 is only valid number in this range as sum of digits of its square = S(22*22) = S(484) = 16 and square of sum of its digits
11 min read
Count pairs in an array having sum of elements with their respective sum of digits equal
Given an array arr[] consisting of N positive integers, the task is to count the number of pairs in the array, say (a, b) such that sum of a with its sum of digits is equal to sum of b with its sum of digits. Examples: Input: arr[] = {1, 1, 2, 2}Output: 2Explanation:Following are the pairs that satisfy the given criteria: (1, 1): The difference bet
8 min read
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
Given three positive integers N, A, and B. The task is to count the numbers of length N containing only digits A and B and whose sum of digits also contains the digits A and B only. Print the answer modulo 109 + 7.Examples: Input: N = 3, A = 1, B = 3 Output: 1 Possible numbers of length 3 are 113, 131, 111, 333, 311, 331 and so on... But only 111 i
15 min read
Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
Given an array A[] consisting of N integers, the task is to split the array A[] into subsets having equal sum and of length equal to elements in array B[]. Examples: Input: A[] = {17, 13, 21, 20, 50, 29}, B[] = {2, 3, 1} Output: 21 29 17 13 20 50 Input: A[] = { 1, 2, 3, 4, 5, 6}, B[] = { 2, 2, 2} Output: 1 6 2 5 3 4 Approach: Follow the steps below
8 min read
Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
Given a range [L, R] and two positive integers N and M. The task is to count the numbers in the range containing only non-zero digits whose sum of digits is equal to N and the number is divisible by M.Examples: Input: L = 1, R = 100, N = 8, M = 2 Output: 4 Only 8, 26, 44 and 62 are valid numbers Input: L = 1, R = 200, N = 4, M = 11 Output: 2 Only 2
14 min read
Count of N-digit numbers having equal count of distinct odd and even digits
Given a positive integer N, the task is to count the number of N-digit numbers such that the count of distinct odd and distinct even digits in the number is the same. Examples: Input: N = 2Output : 45Explanation:For a 2-digit number, in order to satisfy the condition, the first digit can be even and second digit odd, or the second digit can be odd
15 min read
Count of binary strings of length N having equal count of 0's and 1's and count of 1's &ge; count of 0's in each prefix substring
Given an integer N, the task is to find the number of possible binary strings of length N with an equal frequency of 0's and 1's in which frequency of 1's are greater or equal to the frequency of 0's in every prefix substring. Examples: Input: N = 2Output: 1Explanation:All possible binary strings of length 2 are {"00", "01", "10" and "11"}. Out of
7 min read
Count number of integers in given range with adjacent digits different and sum of digits equal to M
Given integers T, A, and B, the task for this problem is to find numbers in the range [A, B] such that the adjacent digits of the number are different and the sum of digits is equal to T. ( A ? B ? 1018) Examples: Input: T = 5, A = 1, B = 100Output: 6Explanation: 5, 14, 23, 32, 41, and 50 are valid integers that are in between the range 1 to 100 an
19 min read
Count N-digit numbers having sum of digits equal to a Prime Number
Given a positive integer N, the task is to count the number of N-digit numbers whose sum of digits is a prime number. Examples: Input: N = 1Output: 4Explanation: [2, 3, 5, 7] are single digit numbers whose sum of digits is equal to a prime number. Input : N = 2Output : 33 Naive Approach: The simplest approach to solve the given problem is to genera
18 min read
Numbers with sum of digits equal to the sum of digits of its all prime factor
Given a range, the task is to find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum.Examples: Input: l = 2, r = 10 Output: 5 2, 3, 4, 5 and 7 are such numbers Input: l = 15, r = 22 Output: 3 17, 19 and 22 are such numbers As, 17 and 19 are already prime. Prime Factors
13 min read