📌  相关文章
📜  计算具有等于K的数字之和的数组元素

📅  最后修改于: 2021-04-18 02:40:40             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是计算位数之和等于K的数组元素的数量。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个变量,例如N ,以存储数组的大小。
  • 初始化一个变量,例如count ,以存储位数等于K的元素。
  • 声明一个函数sumOfDigits()以计算数字的总和。
  • 遍历数组arr [] ,对于每个数组元素,检查位数之和是否等于K。如果发现为真,则将count递增1
  • count的值打印为必需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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]);
 
    // Funtion 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;
   
    // Funtion 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
 
    # Prthe 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)
 
# Funtion 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;
   
    // Funtion call to count array elements
    // having sum of digits equal to K
    elementsHavingDigitSumK(arr, N, K);
  }
}
 
// This code is contributed by divyeshrabadiya07.


输出:
2

时间复杂度: O(N * logN)
辅助空间: O(1)