Find Digits – HackerRank Solution

In this post, we will solve Find Digits HackerRank Solution. This problem (Find Digits) is a part of HackerRank Algorithms series.

Task

An integer d is a divisor of an integer n if the remainder of n % d =0.

Given an integer, for each digit that makes up the integer determine whether it is a divisor. Count the number of divisors occurring within the integer.

Example

n = 124

Check whether 12 and 4 are divisors of 124. All 3 numbers divide evenly into 124 so return 3.
n = 111

Check whether 11, and 1 are divisors of 111. All 3 numbers divide evenly into 111 so return 3.
n = 10

Check whether 1 and 0 are divisors of 101 is, but 0 is not. Return 1.

Function Description

Complete the findDigits function in the editor below.

findDigits has the following parameter(s):

  • int n: the value to analyze

Returns

  • int: the number of digits in n that are divisors of n

Input Format

The first line is an integer, t, the number of test cases.
The t subsequent lines each contain an integer, n.

Constraints

  • 1 <= t <= 15
  • 0 < n < 109

Sample Input

2
12
1012

Sample Output

2
3

Explanation

The number 12 is broken into two digits, 1 and 2. When 12 is divided by either of those two digits, the remainder is 0 so they are both divisors.

The number 1012 is broken into four digits, 101, and 21012 is evenly divisible by its digits 11, and 2, but it is not divisible by 0 as division by zero is undefined.

Solution – Find Digits – HackerRank Solution

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int getR(string n, int q) {
    if(!q) return 0;
    int r = 0;
    for(int i = 0;i < n.length();++i) {
        r *= 10;
        r += (n[i] - '0');
        r %= q;
    }
    if(!r) return 1;
    return 0;
}


int main() { 
    int T;
    string n;
    int res = 0;
    
    cin >> T;
    while(T--) {
        cin >> n;
        
        res = 0;
        for(int i = 0;i < n.length();++i)
            res += getR(n, n[i] - '0');
        
        cout << res << endl;
    }
    
    return 0;
}

Python

#!/bin/python3

import sys

def findDigits(n):
    res = 0
    for dig in n:
        if dig != '0' and int(n) % int(dig) == 0:
            res += 1
        
    return res

if __name__ == "__main__":
    t = int(input().strip())
    for a0 in range(t):
        n = input().strip()
        result = findDigits(n)
        print(result)

Java

import java.util.*;
class Solution
{
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int t,ans,d;
        long n,m;
        t=in.nextInt();
        while(t-->0)
        {
            ans=0;
            n=in.nextLong();
            m=n;
            while(m!=0)
            {
                d=(int)m%10;
                m=m/10;
                if(d==0)
                continue;
                if(n%d==0)
                ans++;
            }
            System.out.println(ans);
        }
    }
}

Note: This problem (Find Digits) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *