int countDigits(int n){
// Write your code here.
int cnt=0;
int original= n;
while(n>0){
int digit =n%10;
if(digit !=0 && original % digit==0)
cnt= cnt+1;
n=n/10;
}
return cnt;
}
Problem of the day
You are given a number ’n’.
Find the number of digits of ‘n’ that evenly divide ‘n’.
A digit evenly divides ‘n’ if it leaves no remainder when dividing ‘n’.
Input: ‘n’ = 336
Output: 3
Explanation:
336 is divisible by both ‘3’ and ‘6’. Since ‘3’ occurs twice it is counted two times.
Note:
You don’t need to print anything. Just implement the given function.
The first line of input contains an integer ‘n’.
Return an integer as described in the problem statement.
You don’t need to print anything, it has already been taken care of, just complete the given function.
35
1
35 is only divisible by ‘5’, hence answer is 1.
373
0
There’s no digit in 373 that evenly divides it. Hence the output is 0.
Try to solve this in O(log(n))
1 <= ‘n’ <= 10^9
Time Limit: 1 sec
Think about how to iterate through each digit of the number 'n' and check whether it divides 'n'.
Approach:
We will iterate through all the digits of the number and increment the answer if the current digit divides the number. Let a ‘k’ digit number be d_k-1 … d_1 d_0, where d_0 to d_k-1 represent its individual digits.
We can iterate through all the digits by repeating the above steps until cur > 0. At each iteration we will increment ‘answer’ if d_0 divides ‘n’.
The steps are as follows:
function countDigits(int n)
O(log(n)), where ‘n’ is the given number.
We are iterating through all the digits of ‘n’ and there are log(n) such digits.
Hence, the time complexity is O(log(n)).
O(1).
We are not using any extra space.
Hence, the space complexity is O(1).
Interview problems
Count Digits Using C++
int countDigits(int n){
// Write your code here.
int cnt=0;
int original= n;
while(n>0){
int digit =n%10;
if(digit !=0 && original % digit==0)
cnt= cnt+1;
n=n/10;
}
return cnt;
}
Interview problems
Count digits which evenly divides number ...
public class Solution {
public static int countDigits(int n){
int ct=0;
int orgNum = n;
while(n>0){
int dig = n%10;
if(dig !=0 && orgNum%dig ==0){
ct++;
}
n/=10;
}
return ct;
}
}
Interview problems
Basic Solution
int orig = n;
int ct =0;
while(n!=0){
int ld = n%10;
if( ld!=0 && orig%ld==0){
ct++;
}
n=n/10;
}
return ct;
Interview problems
Solution for count digits which evenly divides number
public class Solution {
public static int countDigits(int n){
int count = 0;
String str = Integer.toString(n);
for(int i=0;i<str.length();i++){
int digit = str.charAt(i) - '0';
if(digit!=0 && n % digit == 0) count++;
}
return count;
}
}
Interview problems
Count digits which evenly divides number
def countDigits(n: int) -> int:
count = 0
num = n
for i in range(len(str(n))):
divisor = num % 10
if divisor > 0 and n % divisor == 0:
count += 1
num //= 10
return countInterview problems
Count Digits
int countDigits(int n){
int orignal = n;
int count = 0;
while(n>0){
int lastdigit = n%10;
if(lastdigit!=0 && orignal%lastdigit == 0){
count++;
}
n = n/10;
}
return count;
}
Interview problems
Count digits which divides the number equally
int countDigits(int n){
// Write your code here.
int count=0, a=n;
if(n==0)
return 1;
while(a>0)
{
int l=a%10;
if (l != 0 && n % l == 0 ) {
count = count + 1;
}
a=a/10;
}
return count;
}
Interview problems
C++ Easy Solution Easily understandable :
int countDigits(int n) {
int count = 0;
for (int temp = n; temp > 0; temp /= 10) {
int ld = temp % 10;
if (ld != 0 && n % ld == 0) {
count++;
}
}
return count;
}
Interview problems
Easy Solution
public class Solution {
public static int countDigits(int n){
int count=0;
int lastdigit=0;
int m=n; //declare this otherwise get error
while(n>0){
lastdigit=n%10;
if(lastdigit!=0){ //don't merge this if condition with given below.
if(m%lastdigit==0){
count++;
}
}
n=n/10;
}
return count;
}
}
Interview problems
Easy Solution
int countDigits(int n){
// Write your code here
int cnt=0;
int temp=n;
while(temp>0){
int x = temp%10;
if(x==0) cnt+=0;
else if(n%x==0) cnt++;
temp /=10;
}
return cnt;
}