Count of integers in an Array whose length is a multiple of K

Given an array arr of N elements and an integer K, the task is to count all the elements whose length is a multiple of K.

Examples:

Input: arr[]={1, 12, 3444, 544, 9}, K = 2
Output: 2
Explanation:
There are 2 numbers whose digit count is multiple of 2 {12, 3444}.

Input: arr[]={12, 345, 2, 68, 7896}, K = 3
Output: 1
Explanation:
There is 1 number whose digit count is multiple of 3 {345}.

Approach:

  1. Traverse the numbers in the array one by one
  2. Count the digits of every number in the array
  3. Check if it’s digit count is a multiple of K or not.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find
// digit count of numbers
int digit_count(int x)
{
    int sum = 0;
    while (x) {
        sum++;
        x = x / 10;
    }
    return sum;
}
  
// Function to find the count of numbers
int find_count(vector<int> arr, int k)
{
  
    int ans = 0;
    for (int i : arr) {
  
        // Get the digit count of each element
        int x = digit_count(i);
  
        // Check if the digit count
        // is divisible by K
        if (x % k == 0)
  
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
  
    return ans;
}
  
// Driver code
int main()
{
    vector<int> arr
        = { 12, 345, 2, 68, 7896 };
    int K = 2;
  
    cout << find_count(arr, K);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
  
class GFG{
   
// Function to find
// digit count of numbers
static int digit_count(int x)
{
    int sum = 0;
    while (x > 0) {
        sum++;
        x = x / 10;
    }
    return sum;
}
   
// Function to find the count of numbers
static int find_count(int []arr, int k)
{
   
    int ans = 0;
    for (int i : arr) {
   
        // Get the digit count of each element
        int x = digit_count(i);
   
        // Check if the digit count
        // is divisible by K
        if (x % k == 0)
   
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
   
    return ans;
}
   
// Driver code
public static void main(String[] args)
{
    int []arr = { 12, 345, 2, 68, 7896 };
    int K = 2;
   
    System.out.print(find_count(arr, K));
   
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of above approach
  
# Function to find
# digit count of numbers
def digit_count(x):
    sum = 0
    while (x):
        sum += 1
        x = x // 10
    return sum
  
# Function to find the count of numbers
def find_count(arr,k):
    ans = 0
    for i in arr:
        # Get the digit count of each element
        x = digit_count(i)
  
        # Check if the digit count
        # is divisible by K
        if (x % k == 0):
            # Increment the count
            # of required numbers by 1
            ans += 1
  
    return ans
  
# Driver code
if __name__ == '__main__':
    arr  =  [12, 345, 2, 68, 7896]
    K = 2
  
    print(find_count(arr, K))
  
# This code is contributed by Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
  
using System;
  
public class GFG{
  
// Function to find
// digit count of numbers
static int digit_count(int x)
{
    int sum = 0;
    while (x > 0) {
        sum++;
        x = x / 10;
    }
    return sum;
}
  
// Function to find the count of numbers
static int find_count(int []arr, int k)
{
  
    int ans = 0;
    foreach (int i in arr) {
  
        // Get the digit count of each element
        int x = digit_count(i);
  
        // Check if the digit count
        // is divisible by K
        if (x % k == 0)
  
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
  
    return ans;
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 12, 345, 2, 68, 7896 };
    int K = 2;
  
    Console.Write(find_count(arr, K));
  
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

3

Time complexity:- O(N*M), where N is the size of array, and M is the digit count of the largest number in the array.
Space complexity:- O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.