You are given a string num
representing a large integer. An integer is good if it meets the following conditions:
num
with length 3
.Return the maximum good integer as a string or an empty string " "
if no such integer exists.
Note:
num
or a good integer.Example 1:
Input: num = "6777133339 " Output: "777 " Explanation: There are two distinct good integers: "777 " and "333 ". "777 " is the largest, so we return "777 ".
Example 2:
Input: num = "2300019 " Output: "000 " Explanation: "000 " is the only good integer.
Example 3:
Input: num = "42352338 " Output: " " Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
Constraints:
3 <= num.length <= 1000
num
only consists of digits.program main
implicit none
character(len=:), allocatable :: num
character(len=:), allocatable :: max_good_int
! Examples taken from the problem statement
num = "6**777**133339"
call get_max_good_int(num, max_good_int)
write (*, '(A)') trim(max_good_int)
num = "23**000**19"
call get_max_good_int(num, max_good_int)
write (*, '(A)') trim(max_good_int)
num = "42352338"
call get_max_good_int(num, max_good_int)
write (*, '(A)') trim(max_good_int)
contains
subroutine get_max_good_int(num, max_good_int)
implicit none
character(len=*), intent(in) :: num
character(len=:), allocatable, intent(out) :: max_good_int
integer :: i, j, k, n, m
logical :: found
! Initialize variables
max_good_int = ""
n = len(num)
found = .false.
! Iterate over all possible starting indices
do i = 1, n - 2
! Check if the current substring has length 3
if (n - i < 3) exit
! Check if the current substring consists of only one unique digit
found = .true.
do j = i + 1, i + 2
do k = j + 1, i + 2
if (num(j:k) == num(i:j)) then
found = .false.
exit
end if
end do
if (.not. found) exit
end do
! If the current substring is good, update the maximum good integer
if (found) then
m = max(m, len(num(i:i+2)))
max_good_int = num(i:i+2)
end if
end do
end subroutine get_max_good_int
end program main
133 **1 233
module max_good_integer
implicit none
contains
function max_good_integer(num) result(max_good)
character(len=*), intent(in) :: num
character(len=3) :: max_good
integer :: i, j, k, n
max_good = " "
n = len(num)
do i = 1, n - 2
do j = i + 1, n - 1
do k = j + 1, n
if (num(i:j) == num(j:k) .and. num(j:k) /= "0") then
if (max_good == " " .or. len_trim(max_good) < len_trim(num(i:k))) then
max_good = num(i:k)
end if
end if
end do
end do
end do
end function max_good_integer
end module max_good_integer
program test
use max_good_integer
implicit none
character(len=1000) :: num
character(len=3) :: max_good
num = "6777133339"
max_good = max_good_integer(num)
write (*,*) "Max good integer: ", max_good
num = "2300019"
max_good = max_good_integer(num)
write (*,*) "Max good integer: ", max_good
num = "42352338"
max_good = max_good_integer(num)
write (*,*) "Max good integer: ", max_good
end program test
temp.f95:7:25: 7 | function max_good_integer(num) result(max_good) | 1 Error: MODULE attribute of โmax_good_integerโ conflicts with PROCEDURE attribute at (1) temp.f95:9:35: 9 | character(len=*), intent(in) :: num | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:28: 10 | character(len=3) :: max_good | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:21: 12 | integer :: i, j, k, n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:14: 14 | max_good = " " | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:12: 16 | n = len(num) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:15: 18 | do i = 1, n - 2 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:19:23: 19 | do j = i + 1, n - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:20:23: 20 | do k = j + 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:21:22: 21 | if (num(i:j) == num(j:k) .and. num(j:k) /= "0") then | 1 Error: Syntax error in argument list at (1) temp.f95:22:77: 22 | if (max_good == " " .or. len_trim(max_good) < len_trim(num(i:k))) then | 1 Error: Syntax error in argument list at (1) temp.f95:23:37: 23 | max_good = num(i:k) | 1 Error: Syntax error in argument list at (1) temp.f95:24:19: 24 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:15: 25 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:26:11: 26 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:7: 27 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:3: 28 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:30:3: 30 | end function max_good_integer | 1 Error: Expecting END MODULE statement at (1) temp.f95:36:5: 36 | use max_good_integer | 1 Fatal Error: Cannot open module file โmax_good_integer.modโ for reading at (1): No such file or directory compilation terminated.
def split_and_minimize(num):
a = num // 1000
b = (num % 1000) // 100
c = (num % 100) // 10
d = num % 10
return ((a * 10 + c) * 10 + b) * 10 + d
First, we split the given four-digit number 'num' into its individual digits a, b, c, and d. Then, we create two new numbers by changing the positioning of the digits in the following manner:
Finally, we return the sum of these two new numbers as the minimum possible sum of new1 and new2.
int splitAndMinimize(int num) {
int a = num / 1000;
int b = (num % 1000) / 100;
int c = (num % 100) / 10;
int d = num % 10;
return ((a * 10 + c) * 10 + b) * 10 + d;
}
First, we split the given four-digit number 'num' into its individual digits a, b, c, and d. Then, we create two new numbers by changing the positioning of the digits in the following manner:
Finally, we return the sum of these two new numbers as the minimum possible sum of new1 and new2.