Posted on by Kalkicode
Code Array

Count the number of array elements whose digit sum are equal to K

The problem addressed here is about counting the number of array elements whose digit sum is equal to a given value K. The task requires analyzing an array of integers, calculating the sum of digits for each integer, and then counting how many of these sums match the given value K.

For instance, given an array of integers [17, 123, 16, 60, 11, 23, 42] and K being 6, we need to find the count of array elements whose digit sums are equal to 6.

Problem Statement

The problem involves counting how many integers in an array have a digit sum equal to a given value K.

Explanation with Example

Let's consider the example provided: arr = [17, 123, 16, 60, 11, 23, 42] and K is 6.

For each integer in the array, we calculate the sum of its digits:

  • 17 → Digit sum: 1 + 7 = 8
  • 123 → Digit sum: 1 + 2 + 3 = 6
  • 16 → Digit sum: 1 + 6 = 7
  • 60 → Digit sum: 6 + 0 = 6
  • 11 → Digit sum: 1 + 1 = 2
  • 23 → Digit sum: 2 + 3 = 5
  • 42 → Digit sum: 4 + 2 = 6

Among these, 123, 60, and 42 have digit sums equal to 6, so the count is 3.

Idea to Solve the Problem

To solve this problem, we can break down the process into a few key steps:

  1. Define a function to calculate the sum of digits for an integer.
  2. Iterate through the array and calculate the digit sum for each integer.
  3. Count how many times the digit sum matches the given value K.

Pseudocode

Function digitSum(number):
    n = number (convert to positive if negative)
    result = 0
    while n is not 0:
        result = result + (n % 10)
        n = n / 10
    return result

Function countKDigitSum(arr, n, k):
    count = 0
    for i from 0 to n - 1:
        if digitSum(arr[i]) is k:
            count = count + 1
    print count

Main:
    arr = [17, 123, 16, 60, 11, 23, 42]
    n = length of arr
    k = 6
    countKDigitSum(arr, n, k)

Algorithm Explanation

  1. The digitSum function calculates the sum of digits for an integer, ensuring it works for negative integers as well.
  2. The countKDigitSum function iterates through the array and counts how many times the digit sum matches the given value K.
  3. The Main section initializes the array, finds its length, and calls the countKDigitSum function.

Code Solution

// C Program for
// Count the number of array elements whose digit sum are equal to K
#include <stdio.h>

int digitSum(int number)
{
	int n = number;
	if (n < 0)
	{
		// Convert negative number to positive number
		n = -n;
	}
	int result = 0;
	while (n != 0)
	{
		// Adding last digit of a given number
		result = result + (n % 10);
		// Remove last digit
		n = n / 10;
	}
	return result;
}
void countKDigitSum(int arr[], int n, int k)
{
	int count = 0;
	for (int i = 0; i < n; ++i)
	{
		if (digitSum(arr[i]) == k)
		{
			// Increase counter
			count++;
		}
	}
	printf("\n %d", count);
}
int main()
{
	int arr[] = {
		17 , 123 , 16 , 60 , 11 , 23 , 42
	};
	// Get the number of elements
	int n = sizeof(arr) / sizeof(arr[0]);
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	countKDigitSum(arr, n, 6);
	return 0;
}

Output

 3
/*
    Java program for
    Count the number of array elements whose digit sum are equal to K
*/
public class Counting
{
	public int digitSum(int number)
	{
		int n = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		int result = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	public void countKDigitSum(int[] arr, int n, int k)
	{
		int count = 0;
		for (int i = 0; i < n; ++i)
		{
			if (digitSum(arr[i]) == k)
			{
				// Increase counter
				count++;
			}
		}
		System.out.print("\n " + count);
	}
	public static void main(String[] args)
	{
		Counting task = new Counting();
		int[] arr = {
			17 , 123 , 16 , 60 , 11 , 23 , 42
		};
		// Get the number of elements
		int n = arr.length;
		// k = 6
		// ----------
		// 1 + 2 + 3 = 6
		// 6 + 0   = 6
		// 4 + 2   = 4
		// [123,60,42]
		// Result = 3
		task.countKDigitSum(arr, n, 6);
	}
}

Output

 3
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting
{
	public: int digitSum(int number)
	{
		int n = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		int result = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	void countKDigitSum(int arr[], int n, int k)
	{
		int count = 0;
		for (int i = 0; i < n; ++i)
		{
			if (this->digitSum(arr[i]) == k)
			{
				// Increase counter
				count++;
			}
		}
		cout << "\n " << count;
	}
};
int main()
{
	Counting *task = new Counting();
	int arr[] = {
		17 , 123 , 16 , 60 , 11 , 23 , 42
	};
	// Get the number of elements
	int n = sizeof(arr) / sizeof(arr[0]);
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	task->countKDigitSum(arr, n, 6);
	return 0;
}

Output

 3
// Include namespace system
using System;
/*
    Csharp program for
    Count the number of array elements whose digit sum are equal to K
*/
public class Counting
{
	public int digitSum(int number)
	{
		int n = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		int result = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	public void countKDigitSum(int[] arr, int n, int k)
	{
		int count = 0;
		for (int i = 0; i < n; ++i)
		{
			if (this.digitSum(arr[i]) == k)
			{
				// Increase counter
				count++;
			}
		}
		Console.Write("\n " + count);
	}
	public static void Main(String[] args)
	{
		Counting task = new Counting();
		int[] arr = {
			17 , 123 , 16 , 60 , 11 , 23 , 42
		};
		// Get the number of elements
		int n = arr.Length;
		// k = 6
		// ----------
		// 1 + 2 + 3 = 6
		// 6 + 0   = 6
		// 4 + 2   = 4
		// [123,60,42]
		// Result = 3
		task.countKDigitSum(arr, n, 6);
	}
}

Output

 3
package main
import "fmt"
/*
    Go program for
    Count the number of array elements whose digit sum are equal to K
*/
func digitSum(number int) int {
	var n int = number
	if n < 0 {
		// Convert negative number to positive number
		n = -n
	}
	var result int = 0
	for (n != 0) {
		// Adding last digit of a given number
		result = result + (n % 10)
		// Remove last digit
		n = n / 10
	}
	return result
}
func countKDigitSum(arr[] int, n int, k int) {
	var count int = 0
	for i := 0 ; i < n ; i++ {
		if digitSum(arr[i]) == k {
			// Increase counter
			count++
		}
	}
	fmt.Print("\n ", count)
}
func main() {

	var arr = [] int {17 , 123 , 16 , 60 , 11 , 23 , 42}
	// Get the number of elements
	var n int = len(arr)
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	countKDigitSum(arr, n, 6)
}

Output

 3
<?php
/*
    Php program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting
{
	public	function digitSum($number)
	{
		$n = $number;
		if ($n < 0)
		{
			// Convert negative number to positive number
			$n = -$n;
		}
		$result = 0;
		while ($n != 0)
		{
			// Adding last digit of a given number
			$result = $result + ($n % 10);
			// Remove last digit
			$n = (int)($n / 10);
		}
		return $result;
	}
	public	function countKDigitSum($arr, $n, $k)
	{
		$count = 0;
		for ($i = 0; $i < $n; ++$i)
		{
			if ($this->digitSum($arr[$i]) == $k)
			{
				// Increase counter
				$count++;
			}
		}
		echo("\n ".$count);
	}
}

function main()
{
	$task = new Counting();
	$arr = array(17, 123, 16, 60, 11, 23, 42);
	// Get the number of elements
	$n = count($arr);
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	$task->countKDigitSum($arr, $n, 6);
}
main();

Output

 3
/*
    Node JS program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting
{
	digitSum(number)
	{
		var n = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		var result = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = parseInt(n / 10);
		}
		return result;
	}
	countKDigitSum(arr, n, k)
	{
		var count = 0;
		for (var i = 0; i < n; ++i)
		{
			if (this.digitSum(arr[i]) == k)
			{
				// Increase counter
				count++;
			}
		}
		process.stdout.write("\n " + count);
	}
}

function main()
{
	var task = new Counting();
	var arr = [17, 123, 16, 60, 11, 23, 42];
	// Get the number of elements
	var n = arr.length;
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	task.countKDigitSum(arr, n, 6);
}
main();

Output

 3
#    Python 3 program for
#    Count the number of array elements whose digit sum are equal to K
class Counting :
	def digitSum(self, number) :
		n = number
		if (n < 0) :
			#  Convert negative number to positive number
			n = -n
		
		result = 0
		while (n != 0) :
			#  Adding last digit of a given number
			result = result + (n % 10)
			#  Remove last digit
			n = int(n / 10)
		
		return result
	
	def countKDigitSum(self, arr, n, k) :
		count = 0
		i = 0
		while (i < n) :
			if (self.digitSum(arr[i]) == k) :
				#  Increase counter
				count += 1
			
			i += 1
		
		print("\n ", count, end = "")
	

def main() :
	task = Counting()
	arr = [17, 123, 16, 60, 11, 23, 42]
	#  Get the number of elements
	n = len(arr)
	#  k = 6
	#  ----------
	#  1 + 2 + 3 = 6
	#  6 + 0   = 6
	#  4 + 2   = 4
	#  [123,60,42]
	#  Result = 3
	task.countKDigitSum(arr, n, 6)

if __name__ == "__main__": main()

Output

  3
#    Ruby program for
#    Count the number of array elements whose digit sum are equal to K
class Counting 
	def digitSum(number) 
		n = number
		if (n < 0) 
			#  Convert negative number to positive number
			n = -n
		end

		result = 0
		while (n != 0) 
			#  Adding last digit of a given number
			result = result + (n % 10)
			#  Remove last digit
			n = n / 10
		end

		return result
	end

	def countKDigitSum(arr, n, k) 
		count = 0
		i = 0
		while (i < n) 
			if (self.digitSum(arr[i]) == k) 
				#  Increase counter
				count += 1
			end

			i += 1
		end

		print("\n ", count)
	end

end

def main() 
	task = Counting.new()
	arr = [17, 123, 16, 60, 11, 23, 42]
	#  Get the number of elements
	n = arr.length
	#  k = 6
	#  ----------
	#  1 + 2 + 3 = 6
	#  6 + 0   = 6
	#  4 + 2   = 4
	#  [123,60,42]
	#  Result = 3
	task.countKDigitSum(arr, n, 6)
end

main()

Output

 3
/*
    Scala program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting()
{
	def digitSum(number: Int): Int = {
		var n: Int = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		var result: Int = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	def countKDigitSum(arr: Array[Int], n: Int, k: Int): Unit = {
		var count: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (digitSum(arr(i)) == k)
			{
				// Increase counter
				count += 1;
			}
			i += 1;
		}
		print("\n " + count);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Counting = new Counting();
		var arr: Array[Int] = Array(17, 123, 16, 60, 11, 23, 42);
		// Get the number of elements
		var n: Int = arr.length;
		// k = 6
		// ----------
		// 1 + 2 + 3 = 6
		// 6 + 0   = 6
		// 4 + 2   = 4
		// [123,60,42]
		// Result = 3
		task.countKDigitSum(arr, n, 6);
	}
}

Output

 3
import Foundation;
/*
    Swift 4 program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting
{
	func digitSum(_ number: Int) -> Int
	{
		var n: Int = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		var result: Int = 0;
		while (n  != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	func countKDigitSum(_ arr: [Int], _ n: Int, _ k: Int)
	{
		var count: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (self.digitSum(arr[i]) == k)
			{
				// Increase counter
				count += 1;
			}
			i += 1;
		}
		print("\n ", count, terminator: "");
	}
}
func main()
{
	let task: Counting = Counting();
	let arr: [Int] = [17, 123, 16, 60, 11, 23, 42];
	// Get the number of elements
	let n: Int = arr.count;
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	task.countKDigitSum(arr, n, 6);
}
main();

Output

  3
/*
    Kotlin program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting
{
	fun digitSum(number: Int): Int
	{
		var n: Int = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		var result: Int = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	fun countKDigitSum(arr: Array < Int > , n: Int, k: Int): Unit
	{
		var count: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (this.digitSum(arr[i]) == k)
			{
				// Increase counter
				count += 1;
			}
			i += 1;
		}
		print("\n " + count);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Counting = Counting();
	val arr: Array < Int > = arrayOf(17, 123, 16, 60, 11, 23, 42);
	// Get the number of elements
	val n: Int = arr.count();
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	task.countKDigitSum(arr, n, 6);
}

Output

 3

This output indicates that there are 3 elements in the array whose digit sums are equal to K (6).

Time Complexity Analysis

  • The digitSum function has a time complexity of O(log n), where n is the input number, as it involves dividing the number by 10 in each iteration of the loop.
  • The countKDigitSum function has a time complexity of O(m * log n), where m is the number of elements in the array and n is the maximum value among the array elements. This is because it calls the digitSum function for each array element.
  • Thus, the overall time complexity of the given code is dominated by the countKDigitSum function, which is O(m * log n).

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment