Given an array of positive integers nums
, return the maximum possible sum of an ascending subarray in nums
.
A subarray is defined as a contiguous sequence of numbers in an array.
A subarray [numsl, numsl+1, ..., numsr-1, numsr]
is ascending if for all i
where l <= i < r
, numsi < numsi+1
. Note that a subarray of size 1
is ascending.
Example 1:
Input: nums = [10,20,30,5,10,50] Output: 65 Explanation: [5,10,50] is the ascending subarray with the maximum sum of 65.
Example 2:
Input: nums = [10,20,30,40,50] Output: 150 Explanation: [10,20,30,40,50] is the ascending subarray with the maximum sum of 150.
Example 3:
Input: nums = [12,17,15,13,10,11,12] Output: 33 Explanation: [10,11,12] is the ascending subarray with the maximum sum of 33.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
program max_ascending_subarray_sum
implicit none
integer, parameter :: n = 6
integer, dimension(n) :: nums
integer :: i, j, k, l, r, max_sum
! Examples
nums = reshape([10, 20, 30, 5, 10, 50], shape=[n])
write(*, '(a, i0)') 'Example 1: ', max_ascending_subarray_sum(nums)
nums = reshape([10, 20, 30, 40, 50], shape=[n])
write(*, '(a, i0)') 'Example 2: ', max_ascending_subarray_sum(nums)
nums = reshape([12, 17, 15, 13, 10, 11, 12], shape=[n])
write(*, '(a, i0)') 'Example 3: ', max_ascending_subarray_sum(nums)
contains
function max_ascending_subarray_sum(nums) result(max_sum)
implicit none
integer, intent(in) :: nums(:)
integer :: max_sum, current_sum, i, j
max_sum = 0
do i = 1, size(nums) - 1
current_sum = 0
do j = i, size(nums)
if (current_sum >= 0 .and. nums(j) > nums(j-1)) then
current_sum = current_sum + nums(j)
else
exit
end if
max_sum = max(max_sum, current_sum)
end do
end do
end function max_ascending_subarray_sum
end program max_ascending_subarray_sum
temp.f95:10:66: 10 | write(*, '(a, i0)') 'Example 1: ', max_ascending_subarray_sum(nums) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:12:66: 12 | write(*, '(a, i0)') 'Example 2: ', max_ascending_subarray_sum(nums) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:14:66: 14 | write(*, '(a, i0)') 'Example 3: ', max_ascending_subarray_sum(nums) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:18:61: 18 | function max_ascending_subarray_sum(nums) result(max_sum) | 1 Error: PROGRAM attribute of ‘max_ascending_subarray_sum’ conflicts with PROCEDURE attribute at (1) temp.f95:19:21: 19 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:20:38: 20 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:21:45: 21 | integer :: max_sum, current_sum, i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:23:19: 23 | max_sum = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:24:32: 24 | do i = 1, size(nums) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:25:27: 25 | current_sum = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:32: 26 | do j = i, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:27:68: 27 | if (current_sum >= 0 .and. nums(j) > nums(j-1)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:28:55: 28 | current_sum = current_sum + nums(j) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:20: 29 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:30:24: 30 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:31:19: 31 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:32:51: 32 | max_sum = max(max_sum, current_sum) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:33:15: 33 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:34:11: 34 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:35:7: 35 | end function max_ascending_subarray_sum | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:11:19: 11 | nums = reshape([10, 20, 30, 40, 50], shape=[n]) | 1 Error: Without padding, there are not enough elements in the intrinsic RESHAPE source at (1) to match the shape
module max_ascending_subarray_sum
implicit none
private
public :: max_ascending_subarray_sum
contains
function max_ascending_subarray_sum(nums) result(max_sum)
implicit none
integer, intent(in) :: nums(:)
integer :: max_sum, i, j, sum
max_sum = 0
do i = 1, size(nums)
sum = 0
do j = i, size(nums)
if (nums(j) >= nums(j-1)) then
sum = sum + nums(j)
else
exit
end if
end do
if (sum > max_sum) then
max_sum = sum
end if
end do
end function max_ascending_subarray_sum
end module max_ascending_subarray_sum
program test_max_ascending_subarray_sum
use max_ascending_subarray_sum
implicit none
integer :: nums(6)
nums = [10, 20, 30, 5, 10, 50]
write (*,*) 'Maximum ascending subarray sum in [10, 20, 30, 5, 10, 50] is', &
max_ascending_subarray_sum(nums)
nums = [10, 20, 30, 40, 50]
write (*,*) 'Maximum ascending subarray sum in [10, 20, 30, 40, 50] is', &
max_ascending_subarray_sum(nums)
nums = [12, 17, 15, 13, 10, 11, 12]
write (*,*) 'Maximum ascending subarray sum in [12, 17, 15, 13, 10, 11, 12] is', &
max_ascending_subarray_sum(nums)
end program test_max_ascending_subarray_sum
temp.f95:4:40: 4 | public :: max_ascending_subarray_sum | 1 Error: PUBLIC attribute applied to MODULE max_ascending_subarray_sum at (1) temp.f95:8:39: 8 | function max_ascending_subarray_sum(nums) result(max_sum) | 1 Error: MODULE attribute of ‘max_ascending_subarray_sum’ conflicts with PROCEDURE attribute at (1) temp.f95:9:21: 9 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:10:38: 10 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:37: 11 | integer :: max_sum, i, j, sum | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:19: 13 | max_sum = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:28: 14 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:19: 15 | sum = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:32: 16 | do j = i, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:17:46: 17 | if (nums(j) >= nums(j-1)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:18:39: 18 | sum = sum + nums(j) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:20: 19 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:20:24: 20 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:21:19: 21 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:15: 22 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:35: 23 | if (sum > max_sum) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:24:29: 24 | max_sum = sum | 1 Error: Unexpected assignment statement in CONTAINS section 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 function max_ascending_subarray_sum | 1 Error: Expecting END MODULE statement at (1) temp.f95:31:9: 31 | use max_ascending_subarray_sum | 1 Fatal Error: Cannot open module file ‘max_ascending_subarray_sum.mod’ for reading at (1): No such file or directory compilation terminated.
def concatenated_binary(n: int) -> int:
result = 0
mod = 1000000007
for i in range(1, n + 1):
length = len(bin(i)) - 2
result = ((result << length) % mod + i) % mod
return result
The algorithm iterates through all integers from 1 to `n`. For each integer, it first calculates its binary length (i.e. number of bits) using log base 2. This can be done using logarithm internally with floating point representation, followed by flooring and adding 1. In C++, you can use `log2`, in Java you can do `Math.log(i) / Math.log(2)`, and in JavaScript – `Math.log2`.
Then, the result is shifted left by the calculated length (integer arithmetic is mostly used), and the current integer is added to the result. The whole operation is performed under modulo 109 + 7 to prevent overflow. In the end, the result is returned.
#include <bits/stdc++.h>
using namespace std;
int concatenatedBinary(int n) {
long long result = 0;
for (int i = 1; i <= n; ++i) {
int length = log2(i) + 1;
result = ((result << length) % 1000000007 + i) % 1000000007;
}
return result;
}
The algorithm iterates through all integers from 1 to `n`. For each integer, it first calculates its binary length (i.e. number of bits) using log base 2. This can be done using logarithm internally with floating point representation, followed by flooring and adding 1. In C++, you can use `log2`, in Java you can do `Math.log(i) / Math.log(2)`, and in JavaScript – `Math.log2`.
Then, the result is shifted left by the calculated length (integer arithmetic is mostly used), and the current integer is added to the result. The whole operation is performed under modulo 109 + 7 to prevent overflow. In the end, the result is returned.