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++
#include <bits/stdc++.h>
using namespace std;
int sumOfDigits( int N)
{
int sum = 0;
while (N != 0) {
sum += N % 10;
N /= 10;
}
return sum;
}
int elementsHavingDigitSumK( int arr[], int N, int K)
{
int count = 0;
for ( int i = 0; i < N; ++i) {
if (sumOfDigits(arr[i]) == K) {
count++;
}
}
cout << count;
}
int main()
{
int arr[] = { 23, 54, 87, 29, 92, 62 };
int K = 11;
int N = sizeof (arr) / sizeof (arr[0]);
elementsHavingDigitSumK(arr, N, K);
return 0;
}
|
Java
public class GFG
{
static int sumOfDigits( int N)
{
int sum = 0 ;
while (N != 0 ) {
sum += N % 10 ;
N /= 10 ;
}
return sum;
}
static void elementsHavingDigitSumK( int [] arr, int N, int K)
{
int count = 0 ;
for ( int i = 0 ; i < N; ++i)
{
if (sumOfDigits(arr[i]) == K)
{
count++;
}
}
System.out.println(count);
}
public static void main(String args[])
{
int [] arr = { 23 , 54 , 87 , 29 , 92 , 62 };
int K = 11 ;
int N = arr.length;
elementsHavingDigitSumK(arr, N, K);
}
}
|
Python3
def sumOfDigits(N) :
sum = 0
while (N ! = 0 ) :
sum + = N % 10
N / / = 10
return sum
def elementsHavingDigitSumK(arr, N, K) :
count = 0
for i in range (N):
if (sumOfDigits(arr[i]) = = K) :
count + = 1
print (count)
arr = [ 23 , 54 , 87 , 29 , 92 , 62 ]
K = 11
N = len (arr)
elementsHavingDigitSumK(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int sumOfDigits( int N)
{
int sum = 0;
while (N != 0) {
sum += N % 10;
N /= 10;
}
return sum;
}
static void elementsHavingDigitSumK( int [] arr, int N, int K)
{
int count = 0;
for ( int i = 0; i < N; ++i) {
if (sumOfDigits(arr[i]) == K) {
count++;
}
}
Console.WriteLine(count);
}
static void Main()
{
int [] arr = { 23, 54, 87, 29, 92, 62 };
int K = 11;
int N = arr.Length;
elementsHavingDigitSumK(arr, N, K);
}
}
|
Javascript
<script>
function sumOfDigits(N)
{
let sum = 0;
while (N != 0) {
sum += N % 10;
N = parseInt(N / 10, 10);
}
return sum;
}
function elementsHavingDigitSumK(arr, N, K)
{
let count = 0;
for (let i = 0; i < N; ++i) {
if (sumOfDigits(arr[i]) == K) {
count++;
}
}
document.write(count);
}
let arr = [ 23, 54, 87, 29, 92, 62 ];
let K = 11;
let N = arr.length;
elementsHavingDigitSumK(arr, N, K);
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...