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:
k
.num
.Given integers num
and k
, return the k-beauty of num
.
Note:
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)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
temp.f95:17:40: 17 | function k_beauty(num, k) result(beauty) | 1 Error: Function result ‘beauty’ at (1) has no IMPLICIT type temp.f95:20:22: 20 | character(len=len(num)) :: str | 1 Error: ‘string’ argument of ‘len’ intrinsic at (1) must be CHARACTER temp.f95:25:23: 25 | str = trim(adjustl(num)) | 1 Error: In call to ‘adjustl’ at (1), type mismatch in argument ‘string’; pass ‘INTEGER(4)’ to ‘CHARACTER(*,0)’
!-------------------------------------------------------------------------------
!
! 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
temp.f95:36:14: 36 | num_str = num | 1 Error: Cannot convert INTEGER(4) to CHARACTER(10) at (1)
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.
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.