Find the K-Beauty of a Number solution and explanation
9 more videos on this problem
2269. Find the K-Beauty of a Number (Leetcode Easy)Programming Live with Larry · 3:47 · 2K views
2269. Find the K-Beauty of a Number Leetcode Problem | Solution with Explanation | Coding BlocksCoding Blocks · 5:29 · 1K views
2269 Find the K Beauty of a Number | Zero to FAANG | Solution | Leetcode | Striver | WE2Programmers Zone · 12:41 · 1K views
Find the K-Beauty of a Number | 2269 LeetCode | Strings | Maths | Leetcode Biweekly Contest 78CodeWithSunny · 7:23 · 776 views
Leetcode Biweekly contest 78 Easy - Find the K Beauty of a NumberPrakhar Agrawal · 5:13 · 335 views
Find the K-Beauty of a Number(Leetcode :2269)Umyo Thein · 3:32 · 304 views
2269. Find the K-Beauty of a Number || 2270. Number of Ways to Split Array || Leetcode Biweekly 78BinaryMagic · 12:49 · 234 views
8 3 Sliding Window - 2269 Find the K Beauty of a NumberCoding BeTounsi · 7:33 · 132 views
Find the K-Beauty of a Number #java #array #arraylist #java #javainterview #leetcodeCodeVia · 6:18 · 91 views
Leetcode 2269. Find the K-Beauty of a NumberJeezy Leets · 6:23 · 80 views
The problem
The k-beauty of an integer num is defined as the number of substrings of num when it is read as a string that meet the following conditions:
- It has a length of
k. - It is a divisor of
num.
Given integers num and k, return the k-beauty of num.
Note:
- Leading zeros are allowed.
0is not a divisor of any value.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: num = 240, k = 2 Output: 2 Explanation: The following are the substrings of num of length k: - "24" from "240": 24 is a divisor of 240. - "40" from "240": 40 is a divisor of 240. Therefore, the k-beauty is 2.
Example 2:
Input: num = 430043, k = 2 Output: 2 Explanation: The following are the substrings of num of length k: - "43" from "430043": 43 is a divisor of 430043. - "30" from "430043": 30 is not a divisor of 430043. - "00" from "430043": 0 is not a divisor of 430043. - "04" from "430043": 4 is not a divisor of 430043. - "43" from "430043": 43 is a divisor of 430043. Therefore, the k-beauty is 2.
Constraints:
1 <= num <= 1091 <= k <= num.length(takingnumas a string)
Approach overview
Problem Overview: You are given an integer num and an integer k. The k-beauty of the number is the count of substrings of length k that, when converted to integers, divide num without remainder. Substrings with value 0 are ignored because division by zero is undefined.
Approach 1: Direct Substring Calculation and Check (O(n * k) time, O(1) space)
Convert the number to a string and iterate through every substring of length k. For each index i, extract s[i:i+k], convert it to an integer, and check two conditions: the value is not zero and num % value == 0. Increment a counter whenever both conditions hold. The conversion from substring to integer costs O(k), so with roughly n substrings the total time becomes O(n * k). Space usage remains O(1) because only a few variables are maintained. This approach is simple and easy to implement, making it a good baseline.
Approach 2: Sliding Window Approach (O(n) time, O(1) space)
The direct approach repeatedly converts substrings to integers, which is unnecessary work. A more efficient strategy uses a sliding window of length k that maintains the current substring value as a number. Start by building the integer value for the first k digits. Then slide the window one digit at a time: remove the leftmost digit contribution and append the new rightmost digit. This update takes constant time using basic math operations. At each step, check if the current window value is non‑zero and divides num. Because each digit enters and leaves the window exactly once, the total runtime becomes O(n). Only a few integers are stored, so space complexity stays O(1).
Recommended for interviews: The sliding window approach is what interviewers typically expect. It demonstrates that you recognize repeated work in substring parsing and optimize it by maintaining the numeric value incrementally. The direct substring approach is still useful to explain first because it clearly models the problem and shows you understand the constraints before optimizing.
Approach 1: Sliding Window Approach
The Sliding Window approach can be used to efficiently extract all possible substrings of length k and check if they divide the number num. The key idea is to maintain a window of size k and slide it over the number represented as a string.
def k_beauty(num: int, k: int) -> int:
str_num = str(num)
count = 0
for i in range(len(str_num) - k + 1):
sub_num = int(str_num[i:i+k])
if sub_num != 0 and num % sub_num == 0:
count += 1
return count
# Example usage
num = 430043
k = 2
print(k_beauty(num, k))public class KBeauty {
public static int kBeauty(int num, int k) {
String strNum = Integer.toString(num);
int count = 0;
int n = strNum.length();
for (int i = 0; i <= n - k; i++) {
int subNum = Integer.parseInt(strNum.substring(i, i + k));
if (subNum != 0 && num % subNum == 0) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int num = 240;
int k = 2;
System.out.println(kBeauty(num, k));
}
}#include <iostream>
#include <string>
int kBeauty(int num, int k) {
std::string strNum = std::to_string(num);
int count = 0;
for (size_t i = 0; i + k <= strNum.size(); ++i) {
int subNum = std::stoi(strNum.substr(i, k));
if (subNum != 0 && num % subNum == 0) {
count++;
}
}
return count;
}
int main() {
int num = 430043;
int k = 2;
std::cout << kBeauty(num, k);
return 0;
}function kBeauty(num, k) {
let strNum = num.toString();
let count = 0;
for (let i = 0; i <= strNum.length - k; i++) {
let subNum = parseInt(strNum.substring(i, i + k), 10);
if (subNum !== 0 && num % subNum === 0) {
count++;
}
}
return count;
}
// Example usage
let num = 430043;
let k = 2;
console.log(kBeauty(num, k));#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int kBeauty(int num, int k) {
char strNum[12];
sprintf(strNum, "%d", num);
int length = strlen(strNum);
int count = 0;
for (int i = 0; i <= length - k; i++) {
char subStr[k+1];
strncpy(subStr, &strNum[i], k);
subStr[k] = '\0';
int subNum = atoi(subStr);
if (subNum != 0 && num % subNum == 0) {
count++;
}
}
return count;
}
int main() {
int num = 240;
int k = 2;
printf("%d", kBeauty(num, k));
return 0;
}using System;
class KBeauty {
public static int KBeauty(int num, int k) {
string strNum = num.ToString();
int count = 0;
for (int i = 0; i <= strNum.Length - k; i++) {
int subNum = int.Parse(strNum.Substring(i, k));
if (subNum != 0 && num % subNum == 0) {
count++;
}
}
return count;
}
static void Main(string[] args) {
int num = 240;
int k = 2;
Console.WriteLine(KBeauty(num, k));
}
}The Python solution uses slicing to obtain each substring of length k, converting each to an integer and checking divisor condition, similar to processes in other languages.
Time complexity: O(n*k).
Space complexity: O(1) additional space used.
Approach 2: Direct Substring Calculation and Check
This approach directly calculates and iterates over each possible substring of length k and checks its divisibility by converting it to an integer. It uses a straightforward iteration process without additional string building steps.
def k_beauty(num: int, k: int) -> int:
str_num = str(num)
count = 0
for i in range(len(str_num) - k + 1):
sub_num = int(str_num[i:i+k])
if sub_num != 0 and num % sub_num == 0:
count += 1
return count
# Example usage
num = 240
k = 2
print(k_beauty(num, k))public class KBeauty {
public static int kBeauty(int num, int k) {
String strNum = Integer.toString(num);
int count = 0;
for (int i = 0; i <= strNum.length() - k; i++) {
int substrValue = 0;
for (int j = 0; j < k; j++) {
substrValue = substrValue * 10 + (strNum.charAt(i + j) - '0');
}
if (substrValue != 0 && num % substrValue == 0) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int num = 240;
int k = 2;
System.out.println(kBeauty(num, k));
}
}#include <iostream>
#include <string>
int kBeauty(int num, int k) {
std::string strNum = std::to_string(num);
int count = 0;
for (size_t i = 0; i <= strNum.size() - k; ++i) {
int substrValue = 0;
for (size_t j = 0; j < k; ++j) {
substrValue = substrValue * 10 + (strNum[i + j] - '0');
}
if (substrValue != 0 && num % substrValue == 0) {
count++;
}
}
return count;
}
int main() {
int num = 430043;
int k = 2;
std::cout << kBeauty(num, k);
return 0;
}function kBeauty(num, k) {
const strNum = num.toString();
let count = 0;
for (let i = 0; i <= strNum.length - k; i++) {
let subNum = 0;
for (let j = 0; j < k; j++) {
subNum = subNum * 10 + Number(strNum[i + j]);
}
if (subNum !== 0 && num % subNum === 0) {
count++;
}
}
return count;
}
// Example usage
let num = 240;
let k = 2;
console.log(kBeauty(num, k));#include <stdio.h>
#include <string.h>
int kBeauty(int num, int k) {
char strNum[12];
snprintf(strNum, sizeof(strNum), "%d", num);
int length = strlen(strNum);
int count = 0;
for (int i = 0; i <= length - k; i++) {
int substrValue = 0;
for (int j = 0; j < k; j++) {
substrValue = substrValue * 10 + (strNum[i + j] - '0');
}
if (substrValue != 0 && num % substrValue == 0) {
count++;
}
}
return count;
}
int main() {
int num = 240;
int k = 2;
printf("%d", kBeauty(num, k));
return 0;
}using System;
class KBeauty {
public static int KBeauty(int num, int k) {
string strNum = num.ToString();
int count = 0;
for (int i = 0; i <= strNum.Length - k; i++) {
int substrValue = 0;
for (int j = 0; j < k; j++) {
substrValue = substrValue * 10 + (strNum[i + j] - '0');
}
if (substrValue != 0 && num % substrValue == 0) {
count++;
}
}
return count;
}
static void Main(string[] args) {
int num = 430043;
int k = 2;
Console.WriteLine(KBeauty(num, k));
}
}Python uses slicing to acquire the substrings, immediately parsing and validating them for divisibility, similar to the other languages.
Time complexity: O(n*k).
Space complexity: O(1) additional space.
Approach 3: Enumeration
We can convert num to a string s, then enumerate all substrings of s with length k, convert them to an integer t, and check if t is divisible by num. If it is, we increment the answer.
The time complexity is O(log num times k), and the space complexity is O(log num + k).
function divisorSubstrings(num: number, k: number): number {
let ans = 0;
const s = num.toString();
for (let i = 0; i < s.length - k + 1; ++i) {
const t = parseInt(s.substring(i, i + k));
if (t !== 0 && num % t === 0) {
++ans;
}
}
return ans;
}func divisorSubstrings(num int, k int) int {
ans := 0
s := strconv.Itoa(num)
for i := 0; i < len(s)-k+1; i++ {
t, _ := strconv.Atoi(s[i : i+k])
if t > 0 && num%t == 0 {
ans++
}
}
return ans
}Approach 4: Sliding Window
We can maintain a sliding window of length k. Initially, the window contains the lowest k digits of num. Then, for each iteration, we move the window one digit to the right, update the number in the window, and check if the number in the window is divisible by num. If it is, we increment the answer.
The time complexity is O(log num), and the space complexity is O(1).
function divisorSubstrings(num: number, k: number): number {
let [x, p, t] = [0, 1, num];
for (; k > 0; k--) {
const v = t % 10;
t = Math.floor(t / 10);
x = p * v + x;
p *= 10;
}
let ans = x !== 0 && num % x === 0 ? 1 : 0;
for (p = Math.floor(p / 10); t > 0; t = Math.floor(t / 10)) {
x = Math.floor(x / 10);
x = p * (t % 10) + x;
ans += x !== 0 && num % x === 0 ? 1 : 0;
}
return ans;
}func divisorSubstrings(num int, k int) (ans int) {
x, p, t := 0, 1, num
for ; k > 0; k-- {
v := t % 10
t /= 10
x = p*v + x
p *= 10
}
if x != 0 && num%x == 0 {
ans++
}
for p /= 10; t > 0; t /= 10 {
x /= 10
v := t % 10
x = p*v + x
if x != 0 && num%x == 0 {
ans++
}
}
return
}Complexity comparison
| Approach | Complexity |
|---|---|
| Sliding Window Approach | Time complexity: O(n*k). |
| Direct Substring Calculation and Check | Time complexity: O(n*k). |
| Enumeration | – |
| Sliding Window | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Substring Calculation and Check | O(n * k) | O(1) | Best for quick implementation or when constraints are small and clarity matters more than optimization. |
| Sliding Window | O(n) | O(1) | Preferred for large inputs or interviews since it avoids repeated substring parsing. |
Frequently asked questions
Is Find the K-Beauty of a Number easy or hard?
Find the K-Beauty of a Number is classified as an Easy problem. The logic involves basic string traversal and divisibility checks. The challenge is recognizing the sliding window optimization that avoids repeatedly converting substrings to integers.
Find the K-Beauty of a Number Python/Java solution
Most implementations convert the number to a string and iterate through substrings of length k. Python, Java, C++, and similar languages can check each substring, convert it to an integer, and verify whether it divides the original number. The optimized version updates the value using sliding window arithmetic for O(n) performance.
How to solve Find the K-Beauty of a Number in O(n)?
Convert the number to a string and maintain a sliding window of length k. Track the numeric value of the current window and update it when the window moves by removing the leftmost digit and adding the new digit. At each step, check if the value is non-zero and divides the original number.
What is the best approach for Find the K-Beauty of a Number?
The sliding window approach is the most efficient solution. It maintains the numeric value of the current k-length substring while moving across the digits, avoiding repeated substring-to-integer conversions. This reduces the time complexity to O(n) with O(1) extra space.
Is Find the K-Beauty of a Number asked at Google/Amazon/Meta?
Problems involving digit manipulation, substring extraction, and sliding window logic frequently appear in interviews at large tech companies. While this exact question may vary, the same techniques—string processing and window-based iteration—are common in coding interviews at companies like Amazon and Google.
What data structure is used in Find the K-Beauty of a Number?
The solution mainly uses string processing and simple integer arithmetic. The optimized version relies on the sliding window technique rather than a complex data structure. Only a few variables are required to track the current window value and the count of valid substrings.
What is the time complexity of Find the K-Beauty of a Number?
The optimal sliding window solution runs in O(n) time where n is the number of digits in the number. A simpler approach that extracts each substring and converts it to an integer takes O(n * k) time because each substring conversion costs O(k). Both approaches use O(1) extra space.
