You are given a 0-indexed string num of length n consisting of digits.
Return true if for every index i in the range 0 <= i < n, the digit i occurs num[i] times in num, otherwise return false.
Example 1:
Input: num = "1210 " Output: true Explanation: num[0] = '1'. The digit 0 occurs once in num. num[1] = '2'. The digit 1 occurs twice in num. num[2] = '1'. The digit 2 occurs once in num. num[3] = '0'. The digit 3 occurs zero times in num. The condition holds true for every index in "1210 ", so return true.
Example 2:
Input: num = "030 " Output: false Explanation: num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num. num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num. num[2] = '0'. The digit 2 occurs zero times in num. The indices 0 and 1 both violate the condition, so return false.
Constraints:
n == num.length1 <= n <= 10num consists of digits.program main
implicit none
character(len=10) :: num
integer :: n
logical :: result
! Example 1
num = "1210"
n = len(trim(num))
result = checkOccurrences(num, n)
if (result) then
print *, "Example 1: True"
else
print *, "Example 1: False"
end if
! Example 2
num = "030"
n = len(trim(num))
result = checkOccurrences(num, n)
if (result) then
print *, "Example 2: True"
else
print *, "Example 2: False"
end if
contains
function checkOccurrences(num, n) result(res)
implicit none
character(len=*), intent(in) :: num
integer, intent(in) :: n
logical :: res
integer :: i, count
do i = 0, n - 1
count = 0
do j = 1, n
if (num(j:j) == char(48 + i)) then
count = count + 1
end if
end do
if (count /= num(i+1:i+1)) then
res = .false.
return
end if
end do
res = .true.
end function checkOccurrences
end program main
temp.f95:39:16:
39 | do j = 1, n
| 1
Error: Symbol βjβ at (1) has no IMPLICIT type
temp.f95:44:16:
44 | if (count /= num(i+1:i+1)) then
| 1
Error: Operands of comparison operator β/=β at (1) are INTEGER(4)/CHARACTER(*)
module e99_every_digit_occurs_n_times
implicit none
interface
function is_every_digit_occurs_n_times(num) result(res)
implicit none
character(len=*), intent(in) :: num
logical :: res
end function is_every_digit_occurs_n_times
end interface
contains
function is_every_digit_occurs_n_times(num) result(res)
implicit none
character(len=*), intent(in) :: num
integer :: i, j, n, digit
logical :: res
n = len(num)
res = .true.
do i = 0, n - 1
digit = ichar(num(i + 1:i + 1)) - ichar('0')
do j = 0, n - 1
if (j /= i .and. num(j + 1:j + 1) == num(i + 1:i + 1)) then
res = .false.
exit
end if
end do
if (.not. res) exit
end do
end function is_every_digit_occurs_n_times
end module e99_every_digit_occurs_n_times
program test_e99_every_digit_occurs_n_times
use e99_every_digit_occurs_n_times
implicit none
logical :: res
res = is_every_digit_occurs_n_times("1210")
if (res) then
write (*,*) "Passed!"
else
write (*,*) "Failed!"
end if
res = is_every_digit_occurs_n_times("030")
if (res) then
write (*,*) "Passed!"
else
write (*,*) "Failed!"
end if
res = is_every_digit_occurs_n_times("123456789")
if (res) then
write (*,*) "Passed!"
else
write (*,*) "Failed!"
end if
res = is_every_digit_occurs_n_times("0123456789")
if (res) then
write (*,*) "Passed!"
else
write (*,*) "Failed!"
end if
end program test_e99_every_digit_occurs_n_times
temp.f95:17:43:
7 | function is_every_digit_occurs_n_times(num) result(res)
| 2
......
17 | function is_every_digit_occurs_n_times(num) result(res)
| 1
Error: Procedure βis_every_digit_occurs_n_timesβ at (1) is already defined at (2)
temp.f95:18:21:
18 | implicit none
| 1
Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1)
temp.f95:19:43:
19 | character(len=*), intent(in) :: num
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:20:33:
20 | integer :: i, j, n, digit
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:21:22:
21 | logical :: res
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:23:20:
23 | n = len(num)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:24:20:
24 | res = .true.
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:26:23:
26 | do i = 0, n - 1
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:27:36:
27 | digit = ichar(num(i + 1:i + 1)) - ichar('0')
| 1
Error: Syntax error in argument list at (1)
temp.f95:28:27:
28 | do j = 0, n - 1
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:29:43:
29 | if (j /= i .and. num(j + 1:j + 1) == num(i + 1:i + 1)) then
| 1
Error: Syntax error in argument list at (1)
temp.f95:30:33:
30 | res = .false.
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:31:24:
31 | exit
| 1
Error: EXIT statement at (1) is not within a construct
temp.f95:32:19:
32 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:33:15:
33 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:34:31:
34 | if (.not. res) exit
| 1
Error: EXIT statement at (1) is not within a construct
temp.f95:35:11:
35 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:37:7:
37 | end function is_every_digit_occurs_n_times
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:42:9:
42 | use e99_every_digit_occurs_n_times
| 1
Fatal Error: Cannot open module file βe99_every_digit_occurs_n_times.modβ for reading at (1): No such file or directory
compilation terminated.
def rearrange_array(nums):
nums.sort()
for i in range(1, len(nums) - 1, 2):
nums[i], nums[i + 1] = nums[i + 1], nums[i]
return nums
The algorithm starts by sorting the given integer array nums in ascending order. Then, iterates through the sorted array using a for-loop, swapping the numbers at the even index with the number immediately after it. The next step in the loop is done by incrementing the iterator by 2 (i.e., i += 2). The algorithm continues until the entire array is rearranged. Finally, the rearranged array is returned.
#include <vector>
#include <algorithm>
using namespace std;
vector<int> rearrangeArray(vector<int>& nums) {
sort(nums.begin(), nums.end());
for (int i = 1; i < nums.size() - 1; i += 2) {
swap(nums[i], nums[i + 1]);
}
return nums;
}
The algorithm starts by sorting the given integer array nums in ascending order. Then, iterates through the sorted array using a for-loop, swapping the numbers at the even index with the number immediately after it. The next step in the loop is done by incrementing the iterator by 2 (i.e., i += 2). The algorithm continues until the entire array is rearranged. Finally, the rearranged array is returned.