Count of factors of length K in the number itself
Given an integer N and a value K, the task is to find the number of factors of length K that are present in the number itself.
Examples:
Input: N = 120, K = 2
Output: 2
Explanation: 12 (120), 20 (120). Both are factors of 120.
Input: N = 21, K = 2
Output: 1
Approach: The problem can be solved greedily using the following idea:
Find all the K-length numbers that can be generated from the number and check how many of them are factors of N.
Follow the steps mentioned below to implement the idea:
- Iterate the number.
- Initialize the start index with 0.
- Slice the number for K size from the start index.
- Then check if the sliced part of the number is a factor of the given number.
- If it is a factor, increment the counter.
- Else, do nothing.
- Increment the start index to the next index.
- Continue the above process until the start index is N-K.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int countFactorsinNum( int num, int k)
{
string str = to_string(num);
int N = str.size();
int ctr = 0;
int i = 0;
while (i <= N - k) {
string s2 = str.substr(i, k);
int val = stoi(s2);
if (val != 0 && num % val == 0) {
ctr++;
}
i = i + 1;
}
return ctr;
}
int main()
{
int N = 120, K = 2;
cout << countFactorsinNum(N, K) << "\n" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int countFactorsinNum( int num, int k)
{
String str = Integer.toString(num);
int N = str.length();
int ctr = 0 ;
int i = 0 ;
while (i <= N - k) {
String s2 = str.substring(i, i + k);
int val = Integer.parseInt(s2);
if (val != 0 && num % val == 0 ) {
ctr++;
}
i = i + 1 ;
}
return ctr;
}
public static void main(String[] args)
{
int N = 120 , K = 2 ;
System.out.println(countFactorsinNum(N, K));
}
}
|
Python3
def countFactorsinNum(num, k):
str_num = str (num)
N = len (str_num)
ctr = 0
for i in range (k):
s2 = str_num[i:i + k]
val = int (s2)
if val ! = 0 and num % val = = 0 :
ctr + = 1
return ctr
N = 120
K = 2
print (countFactorsinNum(N, K))
|
Javascript
function countFactorsinNum(num, k)
{
let str = num.toString();
let N = str.length;
let ctr = 0;
let i = 0;
while (i <= N - k) {
let s2 = str.substring(i, k);
let val = parseInt(s2);
if (val != 0 && num % val == 0) {
ctr++;
}
i = i + 1;
}
return ctr;
}
let N = 120, K = 2;
console.log(countFactorsinNum(N, K));
|
C#
using System;
using System.Collections.Generic;
public class Gfg {
static int countFactorsinNum( int num, int k)
{
string str = num.ToString();
int N = str.Length;
int ctr = 0;
int i = 0;
while (i <= N - k) {
string s2 = str.Substring(i, k);
int val = int .Parse(s2);
if (val != 0 && num % val == 0) {
ctr++;
}
i = i + 1;
}
return ctr;
}
public static void Main( string [] args)
{
int N = 120, K = 2;
Console.WriteLine(countFactorsinNum(N, K)+ "\n" );
}
}
|
Time Complexity: O(d) where d is the number of digits
Auxiliary Space: O(1)
Related Articles:
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...