Count Digits Geeks For Geeks
Problem Link:
https://practice.geeksforgeeks.org/problems/count-digits5716/1
Solution:
class Solution{
static int evenlyDivides(int n){
int num=n;
int count=0;
int a=0;
while(n!=0){
a=n%10;
if(a!=0){
if(num%a==0){
++count;
}
}
n=n/10;
}
// System.out.println(count);
return count;
}
}
TIME:O(logn)[GFG Time :0.1/1.2]
SPACE:O(1)[No Storage only constants]
AUXILLARY SPACE:O(1)[No Additional Space is Used]
TOTAL TEST CASES:285
APPROACH:
Here we will see every digit by taking number%10 whether it's dividing the number or not.
If yes we add count else no increase in the value of the count.
Here we have applied the condition of a!=0 inside loop to make sure that division does not occur by 0.
ex if the number is 1002 then the first digit is 2 since it divides 1002 so the count value increases to 1.
Now next digit is 0 which does not divide the number and there will be the case of 1002/0 which will throw an error. So in a way to avoid such cases we took a!=0.
"Thanks For Reading.😇"
"Share Further To Increase Knowledge Treasure.😊"
Comments
Post a Comment