Find the K-Beauty of a Number

🏠 ⬅️ ➡️

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.
  • 0 is 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 <= 109
  • 1 <= k <= num.length (taking num as a string)

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    ! Solves the "K-Beauty" problem
    implicit none

    integer :: num, k, beauty

    num = 240
    k = 2
    print '(I0)', k_beauty(num, k)

    num = 430043
    k = 2
    print '(I0)', k_beauty(num, k)

contains

function k_beauty(num, k) result(beauty)
    implicit none
    integer, intent(in) :: num, k
    character(len=len(num)) :: str
    integer :: i, j, div
    logical :: found

    beauty = 0
    str = trim(adjustl(num))
    do i = 1, len(str)-k+1
        found = .false.
        do j = i+k-1, len(str)
            if (str(i:j) == adjustl(str(i:j))) then
                div = 0
                do while (div < num)
                    if (mod(num, div) == 0) then
                        found = .true.
                        exit
                    end if
                    div = div + 1
                end do
                if (found) then
                    beauty = beauty + 1
                    exit
                end if
            end if
        end do
    end do
end function k_beauty

end program main
Compiled
Executed
Correct
!-------------------------------------------------------------------------------
!
! This file is part of the Fortran-by-Example repository
!
! https://github.com/steve-smith/fortran-by-example
!
! Copyright (c) 2018 Steve Smith
! Licensed under the MIT License (MIT)
!
!-------------------------------------------------------------------------------

program k_beauty

  implicit none

  integer :: num, k, beauty

  ! Read input
  read *, num, k

  ! Calculate k-beauty
  beauty = k_beauty_calc(num, k)

  ! Output result
  write (*,*) beauty

contains

  function k_beauty_calc(num, k) result(beauty)
    implicit none
    integer, intent(in) :: num, k
    integer :: beauty, i, j, div
    character(len=10) :: num_str

    beauty = 0
    num_str = num

    ! Loop through each substring of length k
    do i = 1, len(num_str) - k + 1
      ! Convert substring to integer
      read (num_str(i:i+k-1),*) div

      ! Check if divisor
      if (num / div == 0) then
        beauty = beauty + 1
      end if
    end do

  end function k_beauty_calc

end program k_beauty
🌐 Data from online sources
def num_elements_with_smaller_and_greater(nums):
    n = len(nums)
    res = 0
    for i in range(n):
        has_smaller = has_greater = False
        for j in range(n):
            if i == j:
                continue
            if nums[j] > nums[i]:
                has_greater = True
            if nums[j] < nums[i]:
                has_smaller = True
            if has_smaller and has_greater:
                res += 1
                break
    return res

The algorithm iterates through the input array nums. For each element, it checks if there are both smaller and greater elements in the array. To do this, it uses two boolean variables (hasSmaller and hasGreater). The inner loop iterates through the array again to compare each pair of distinct elements.

If a pair of elements that satisfies the condition (one is smaller and the other is greater) is found, the result is incremented by 1, and the inner loop is broken to avoid redundant checks for the current element.

After iterating through the entire array, the final result is returned.

🌐 Data from online sources
int numElementsWithSmallerAndGreater(const std::vector<int>& nums) {
    int n = nums.size();
    int res = 0;
    for (int i = 0; i < n; i++) {
        bool hasSmaller = false, hasGreater = false;
        for (int j = 0; j < n; j++) {
            if (i == j) continue;
            if (nums[j] > nums[i]) hasGreater = true;
            if (nums[j] < nums[i]) hasSmaller = true;
            if (hasSmaller && hasGreater) {
                res++;
                break;
            }
        }
    }
    return res;
}

The algorithm iterates through the input array nums. For each element, it checks if there are both smaller and greater elements in the array. To do this, it uses two boolean variables (hasSmaller and hasGreater). The inner loop iterates through the array again to compare each pair of distinct elements.

If a pair of elements that satisfies the condition (one is smaller and the other is greater) is found, the result is incremented by 1, and the inner loop is broken to avoid redundant checks for the current element.

After iterating through the entire array, the final result is returned.