Given an array arr
, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1
.
After doing so, return the array.
Example 1:
Input: arr = [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] Explanation: - index 0 --> the greatest element to the right of index 0 is index 1 (18). - index 1 --> the greatest element to the right of index 1 is index 4 (6). - index 2 --> the greatest element to the right of index 2 is index 4 (6). - index 3 --> the greatest element to the right of index 3 is index 4 (6). - index 4 --> the greatest element to the right of index 4 is index 5 (1). - index 5 --> there are no elements to the right of index 5, so we put -1.
Example 2:
Input: arr = [400] Output: [-1] Explanation: There are no elements to the right of index 0.
Constraints:
1 <= arr.length <= 104
1 <= arr[i] <= 105
program main
implicit none
integer, parameter :: n = 6
integer, dimension(n) :: arr
integer :: i
! Example 1
arr = [17, 18, 5, 4, 6, 1]
call solve(arr)
write (*, '(A)') 'Example 1:'
do i = 1, size(arr)
write (*, '(I2)', advance='no') arr(i)
end do
write (*, *) ''
! Example 2
arr = [400]
call solve(arr)
write (*, '(A)') 'Example 2:'
do i = 1, size(arr)
write (*, '(I2)', advance='no') arr(i)
end do
write (*, *) ''
contains
subroutine solve(arr)
implicit none
integer, intent(inout) :: arr(:)
integer :: i, j
do i = 1, size(arr) - 1
j = maxloc(arr(i+1:size(arr)), dim=1) + i - 1
arr(i) = arr(j)
end do
arr(size(arr)) = -1
end subroutine solve
end program main
temp.f95:18:4: 18 | arr = [400] | 1 Error: Different shape for array assignment at (1) on dimension 1 (6 and 1)
module greatest_right_side
implicit none
contains
function greatest_right_side(arr) result(res)
integer, intent(in) :: arr(:)
integer :: res(size(arr))
integer :: i
do i = 1, size(arr) - 1
res(i) = max(arr(i), arr(i+1:))
end do
res(size(arr)) = -1
end function greatest_right_side
end module greatest_right_side
program test_greatest_right_side
use greatest_right_side
implicit none
integer, parameter :: arr1(6) = [17, 18, 5, 4, 6, 1]
integer, parameter :: arr2(1) = [400]
integer :: res1(6)
integer :: res2(1)
res1 = greatest_right_side(arr1)
res2 = greatest_right_side(arr2)
write (*,*) res1
write (*,*) res2
end program test_greatest_right_side
temp.f95:7:28: 7 | function greatest_right_side(arr) result(res) | 1 Error: MODULE attribute of ‘greatest_right_side’ conflicts with PROCEDURE attribute at (1) temp.f95:9:29: 9 | integer, intent(in) :: arr(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:20: 10 | integer :: res(size(arr)) | 1 Error: Symbol ‘arr’ is used before it is typed at (1) temp.f95:11:12: 11 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:23: 13 | do i = 1, size(arr) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:3: 15 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:17:19: 17 | res(size(arr)) = -1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:3: 19 | end function greatest_right_side | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:5: 25 | use greatest_right_side | 1 Fatal Error: Cannot open module file ‘greatest_right_side.mod’ for reading at (1): No such file or directory compilation terminated.
def kConcatenationMaxSum(arr, k):
M = 10**9 + 7
s = sum(arr)
max_sum = max_ending_here = 0
for i in range(len(arr) * min(2, k)):
max_ending_here = max(arr[i % len(arr)], max_ending_here + arr[i % len(arr)])
max_sum = max(max_sum, max_ending_here)
return 0 if k == 1 else (((max_sum - max_ending_here) % M) * (k - 2) % M + max_ending_here) % M
int kConcatenationMaxSum(vector<int>& arr, int k) {
int M = 1e9 + 7;
long long sum = 0, maxSum = 0, curMax = 0;
for (int i = 0; i < arr.size(); i++) {
sum += arr[i];
curMax = max(arr[i], curMax + arr[i]);
maxSum = max(maxSum, curMax);
}
if (k == 1) return maxSum % M;
long long twoConcatenation = 0, twoConcatenationMax = 0;
for (int i = 0; i < 2 * arr.size(); i++) {
twoConcatenation = max((long long)arr[i % arr.size()], twoConcatenation + arr[i % arr.size()]);
twoConcatenationMax = max(twoConcatenationMax, twoConcatenation);
}
return max({maxSum, twoConcatenationMax + (k - 2) * max(0LL, sum)}) % M;
}