Count array elements having sum of digits equal to K Last Updated : 29 Jan, 2022 Comments Improve Suggest changes Like Article Like 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 = 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++ // 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) Comment More infoAdvertise with us Next Article Count array elements having sum of digits equal to K patelajeet Follow Improve Article Tags : Mathematical Technical Scripter DSA Arrays Technical Scripter 2020 +1 More Practice Tags : ArraysMathematical Similar Reads 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 sati 8 min read Count frequency of digit K in given Array Given an array arr[] of integers of size N and a single digit integer K. The task is to find the total count of occurrences of the digit K in the array Examples: Input: arr[] = {15, 66, 26, 91}, K = 6Output: 3Explanation: Occurrences of 6 in each array elements are: 0, 2, 1, 0 respectively.Therefore 5 min read Count array elements whose all distinct digits appear in K Given an array arr[] consisting of N positive integers and a positive integer K, the task is to find the count of array elements whose distinct digits are a subset of the digits of K. Examples: Input: arr[] = { 1, 12, 1222, 13, 2 }, K = 12Output: 4Explanation: Distinct Digits of K are { 1, 2 } Disti 15 min read Minimum count of elements that sums to a given number Given infinite number of elements of form 10^n and 25*100^n ( n >= 0 ). The task is to find the minimum count of elements chosen such that there sum is equal to K. Examples: Input : K = 48 Output : 6 elements chosen are: (1 + 1 + 1 + 10 + 10 + 25)Input : 69 Output : 9 elements chosen are: (1 + 1 7 min read Count of integers in given range having their last K digits are equal Given a range from L to R and an integer K, the task is to count the number of integers in the given range such that their last K digits are equal. Example: Input: L = 49, R = 101, K=2Output: 6Explanation: There are 6 possible integers t.e., 55, 66, 77, 88, 99 and 100 such that their last K(i.e., 2) 6 min read Count number of pairs in array having sum divisible by K | SET 2 Given an array A[] and positive integer K, the task is to count the total number of pairs in the array whose sum is divisible by K.Examples: Input : A[] = {2, 2, 1, 7, 5, 3}, K = 4 Output : 5 There are five pairs possible whose sum Is divisible by '4' i.e., (2, 2), (1, 7), (7, 5), (1, 3) and (5, 3)I 6 min read Count pairs in array whose sum is divisible by K Given an array A[] and positive integer K, the task is to count the total number of pairs in the array whose sum is divisible by K. Note: This question is a generalized version of this Examples: Input : A[] = {2, 2, 1, 7, 5, 3}, K = 4 Output : 5 Explanation : There are five pairs possible whose sum 10 min read Count the number of elements in an array which are divisible by k Given an array of integers. The task is to calculate the count of a number of elements which are divisible by a given number k. Examples: Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 3 Output: 3 Numbers which are divisible by k are { 6, 12, 18 } Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 2 Output: 5 6 min read Count of n digit numbers whose sum of digits equals to given sum Given two integers n and sum, the task is to find the count of all n digit numbers with sum of digits equal to sum. Note: Leading 0's are not counted as digits. If there exist no n digit number with sum of digits equal to given sum, print -1.Example: Input: n = 2, sum= 2Output: 2Explanation: The num 15+ min read Count subsequences of Array having single digit integer sum K Given an array arr[] and integer K, the task is to count the number of subsequences of the array such that after adding all the elements of that subsequences their single digit integer sum is exactly K. Note: Single digit integer sum is obtained by replacing a number with its digit sum until the num 8 min read Like