Count array elements having sum of digits equal to K
Last Updated :
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 = 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>
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
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